簡體   English   中英

如何使用 php 並排顯示 sql 中的兩行?

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

我下面的代碼顯示了 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>'

?>

未經測試,但基本上您也可以在循環中調用 $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>";
}

對 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>'
<?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 列的表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM