简体   繁体   中英

output while($row = mysqli_fetch_array($result)) in columns

I have a simple question but i am missing something. I want the output of this query to be in 4 columns:

$result = mysqli_query($con,"SELECT * FROM countries");
echo "<table>";
 while($row = mysqli_fetch_array($result))
   {     echo "<tr>";
  for ($x=1; $x<=4; $x++);
{
      echo "<tr><th></th>";
          echo "<td>";
                        echo "<a href='http://localhost/loc.php?loc=" .$row['countryID']. "'>"          ; echo $row['country'] ; echo "</a> ";
            echo  "</td>";
echo "</tr>";
}
  }
echo "</table>";

I only get 1 column with all the countries or 1 get every country 4 times? what am i missing or doing wrong. it bin a long time for me php.

而不是使用Select * From ...您需要使用Select column1, column2, column3...

I think that you print too much "<tr>" tag

try this code:

$result = mysqli_query($con,"SELECT * FROM countries");
echo "<table>";
while($row = mysqli_fetch_array($result)) {     
  echo "<tr>";
  for ($x=1; $x<=4; $x++) {
    echo "<td>";
    echo "<a href='http://localhost/loc.php?loc=" .$row['countryID']. "'>"; 
    echo $row['country'] ;
    echo "</a> ";
    echo  "</td>";
  }
  echo "</tr>";
}
echo "</table>";

Try this out.

<table>
    <tr>
        <th>Country</th>
    </tr>
    <?php $result = mysqli_query($con,"SELECT * FROM countries");
    while($row = mysqli_fetch_array($result)):
    ?>
    <tr>
        <td> <a href="http://localhost/loc.php?loc=<?php echo $row['countryID']?>"><?php  echo $row['country']; ?></a> </td>
    </tr>
    <?php endwhile;?>

</table>
    <?php $result->close();?>
<?php

// this should be placed in some Controller
$result = mysqli_query($con, "SELECT `id`, `name` FROM `countries` LIMIT 4");
$countries = array();
while ($row = mysqli_fetch_array($result)) {
    $countries[] = $row;
}

?>

<!--and this in some view-->
<table>
    <tr>
        <?php for ($i = 0; $i < 4; $i++): ?>
        <td>
            <?php if (isset($countries[$i])): ?>
            <a href="http://localhost/loc.php?loc=<?php echo $countries[$i]['id']; ?>" title=""><?php echo $countries[$i]['name']; ?></a>
            <?php else: ?>
            &nbsp;
            <?php endif; ?>
        </td>
        <?php endfor; ?>
    </tr>
</table>

Anyway, you should not mix up HTML and PHP code. If it's possible try to use some templating language.

<?php
$result = mysqli_query($link, "SELECT `countryID`, `country` FROM `mydb`.`countries`");
echo '<table>
';
$loop = 0;
while($row = mysqli_fetch_array($result)) {
  if ($loop == 0) {
    echo '  <tr>
    ';
  }
  if ($loop == 5) {
    echo '  </tr>
    ';
    $loop = 0;
  }
  else {
    echo '    <td><a href="http://localhost/loc.php?loc=' .$row['countryID']. '">' . $row['country'] . '</td>
    ';
    ++$loop;
  }
  echo "  </tr>";
}
echo "</table>";
?>

Note the use of backticks in sql, to esure you don't have trouble with reserved word.

Note also the exchange of single and double quotes in the echoes. Firstly, double quotes are the convention in html. Secondly, php runs faster. "php will parse this text," 'whereas this text will be read "as is."'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM