如何使用php并排显示sql中的两行?

时间:2011-06-02 21:17:16

标签: php sql

下面的代码显示了10个条目,但它以10行1列显示。我想它显示10个条目,但在2列和5行。有人可以帮忙吗?提前谢谢。

< ?php mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error());

$result = mysql_query("SELECT * FROM Members LIMIT 0, 10") 
or die(mysql_error());  

echo '<table>';
echo '<tr>';
while($row = mysql_fetch_array( $result )) {


if ($row['Approved']=='No')
{
    continue;
}
else{

    echo '<td>';
echo "ID Number: ".$row['id'];
echo "<br/>";
    echo '<a href="original/'.sha1($row['Username']).'.jpg"><img src="'.$row['Pic'].'"></a>';
    echo "<br/>";
    echo '<hr>';
    echo '<tr>';

}
}
echo '</table>'

$result = mysql_query("SELECT * FROM Members LIMIT 0, 10") or die(mysql_error()); echo '<table>'; echo '<tr>'; while($row = mysql_fetch_array( $result )) { if ($row['Approved']=='No') { continue; } else{ echo '<td>'; echo "ID Number: ".$row['id']; echo "<br/>"; echo '<a href="original/'.sha1($row['Username']).'.jpg"><img src="'.$row['Pic'].'"></a>'; echo "<br/>"; echo '<hr>'; echo '<tr>'; } } echo '</table>'

3 个答案:

答案 0 :(得分:0)

未经测试,但基本上你可以在循环中调用$ row = mysql_fetch_array()以获取另一行。

while($row = mysql_fetch_array( $result ))
{
    echo "<tr>";
    echo "<td>";

    echo "ID Number: ".$row['id'];
    echo "<br/>";
    echo '<a href="original/'.sha1($row['Username']).'.jpg"><img src="'.$row['Pic'].'"></a>';
    echo "<br/>";
    echo '<hr>';

    echo "</td>";

    $row = mysql_fetch_array( $result );
    if($row)
    {
        echo "ID Number: ".$row['id'];
        echo "<br/>";
        echo '<a href="original/'.sha1($row['Username']).'.jpg"><img src="'.$row['Pic'].'"></a>';
        echo "<br/>";
        echo '<hr>';
    }
    else
    {
        // Empty cell
        echo "<td>&nbsp;</td>";
    }

    echo "</tr>";
}

答案 1 :(得分:0)

使用modulo for echo“&lt; tr&gt;”

$result = mysql_query("SELECT * FROM Members LIMIT 0, 10") 
or die(mysql_error());  

$i = 0;

echo '<table>';
echo '<tr>';
while($row = mysql_fetch_array( $result )) {


if ($row['Approved']=='No')
{
    continue;
}
else{

    echo '<td>';
echo "ID Number: ".$row['id'];
echo "<br/>";
    echo '<a href="original/'.sha1($row['Username']).'.jpg"><img src="'.$row['Pic'].'"></a>';
    echo "<br/>";
    echo '<hr>';
  if ($i % 5)
    echo '</tr>';
$i++;
}
echo '</table>'

答案 2 :(得分:0)

<?php

mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error());

$result = mysql_query("SELECT * FROM Members LIMIT 0, 10") or die(mysql_error());  

$i = 1;  //The following logic only works if $i starts at '1'.
$numofcols = 2; //Represents the number of columns you want in the table.

echo '<table>'; //Open table.

while($row = mysql_fetch_array( $result )) {

  if ($row['Approved']=='No'){
    continue;
  }
  else{

    //If it's the beginning of a row...
    if( $i % $numofcols == 1 ){
      echo '<tr>'; //Open row
    }


    //Table Cell.
    echo '<td>'; //Open Cell
    echo 'ID Number: '.$row['id'];
    echo '<br/> <a href="original/'.sha1($row['Username']).'.jpg"><img src="'.$row['Pic'].'"></a> <br/>';
    echo '</td>'; //Close Cell

    //If we have already placed enough cells, close this row.
    if( $i % $numofcols == 0) {
      echo '</tr>'; //Close Row.
    }

    //Now that we've made a table-cell, lets increment our counter.
    $i = $i + 1;

  }
}

//So we make sure to close our rows if there are any orphaned cells
if( ($i % $numofcols) > 0){
 echo '</tr>';
}
echo '</table>' //Close Table
?>

请注意,mod逻辑仅在$ i的起始值为“1”时有效。 此代码将创建一个包含2列的表。