从右到左输出两列中的MySql数据

时间:2014-02-14 06:00:28

标签: php mysql database

从右侧开始的两列中需要从mySql表输出数据记录,即记录1和1。 2应该出现在第二列而3& 4应显示在第一列中。这两列都应该能够显示任意数量的记录,而不仅仅是这里显示的两个记录。

mySql表:

1. fname     lname     age     address
2. fname     lname     age     address
3. fname     lname     age     address
4. fname     lname     age     address

使用的代码:

<?php
$con = mysql_connect("localhost","mysql_user","mysql_password");
if (!$con)
   {
   die('Could not connect: ' . mysql_error());
   }

mysql_select_db("test", $con);
?>

<style type='text/css'>
#div1, #div2 {float:left; margin-left:50px; width:300px;}
</style>

<?php
$result = mysql_query("SELECT * FROM member");
$rowcount = mysql_num_rows($result);

echo "<div id='div1'>";
$i = 0;

while($row = mysql_fetch_array($result))
   { 
      echo "Sr. No." . " " . $row['srno'] . " " . $row['fname'] . " " . $row['lname'] . " " . $row['age'] . " " . $row['address'];
   $i++;
   echo "<br/>";
   echo "<br/>";

      if ($i == floor($rowcount / 2)) {

   echo "</div><div id='div2'>";  
    }
   }
echo "</div>";

mysql_close($con);
?>

以上代码的输出:

Sr. No. 1. fname   lname   age   address               Sr. No. 3. fname   lname   age   address
Sr. No. 2. fname   lname   age   address               Sr. No. 4. fname   lname   age   address

需要输出:

Sr. No. 3. fname   lname   age   address               Sr. No. 1. fname   lname   age   address
Sr. No. 4. fname   lname   age   address               Sr. No. 2. fname   lname   age   address

1 个答案:

答案 0 :(得分:0)

MySQL解决方案:

您的select语句中需要ORDER BY FIELD( ...

SELECT * FROM member ORDER BY FIELD( id, 3,4,1,2 )

此声明按以下顺序生成您的记录。

3. fname     lname     age     address
4. fname     lname     age     address
1. fname     lname     age     address
2. fname     lname     age     address

请参阅