开发者

How do I display two rows from sql side-by-side using php?

My code below displays 10 entries, but it displays it in 10 rows and 1 column. I would like for it to display 10 entries, but in 2 column开发者_运维知识库s and 5 rows. Can anyone help? Thanks in advance.

< ?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>'

?>


<?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
?>

Please note that the mod logic will only work if the starting value of $i is '1'. This code will create a table with 2 columns.


Not tested, but basically you can call $row = mysql_fetch_array() in the loop as well to grab another row.

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>";
}


Use modulo for echo "< tr>"

$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>'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜