简体   繁体   中英

How to create PHP two column table with values from the database?

I want to create a table of names with two columns where names are taken from the database but I don't know how.. I need help.

Names: James, John, Paul, Peter

Here's my code:

<?php
$con = mysql_connect("localhost","root","");
if(!$con){
echo "Unable to connect DB";
}else{
    $db = mysql_select_db("persons",$con);
    echo "Connected";
}
echo "<table border='1'>";
$count = 0;
$sql = "SELECT * FROM tbl_names";
$q = mysql_query($sql);
while($res = mysql_fetch_array($q)){
$count++;
    echo "<tr>";
        for($i=1;$i<=2;$i++){
                echo "<td>{$res['id']}{$res['title']}</td>";
        }
    echo "</tr>";   
}
echo "</table>";
?>

I want the output to be like this:

+-------+-------+
| James | John  |
+-------+-------+
| Paul  | Peter |
+-------+-------+

But my code return:

+-------+-------+
| James | Jame  |
+-------+-------+
| John  | John  |
+-------+-------+
| Paul  | Paul  |
+-------+-------+
| Peter | Peter |
+-------+-------+

I need your help.

echo "<tr>";
while($res = mysql_fetch_array($q)){
    $count++;
    if (!($count % 2)){ echo "</tr><tr>"; }
    echo "<td>{$res['id']}{$res['title']}</td>";
}
echo "</tr>";

a function

function sqlArr($sql){
  $ret = array();
  $res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
  if ($res) {
    while($row = mysql_fetch_array($res)){
      $ret[] = $row;
    }
  }
  return $ret;
}

a code

mysql_connect("localhost","root","");
mysql_select_db("persons");
$data = sqlArr("SELECT * FROM tbl_names");
$data = array_chunk($data,2);

a template

<table border='1'>
<? foreach ($data as $row): ?>
  <tr>
  <? foreach ($row as $cell): ?>
    <td><?=$cell['id']?><?=$cell['title']?></td>
  <? endforeach ?>
  </tr>
<? endforeach ?>
</table>

well, if there's no relation and the table is used only for layout:

echo '<div class="container">';
while($res = mysql_fetch_array($q)){
    echo '<div class="item">'.  $res['id'] . $res['title'] . '</div>';
}
echo '</div>';

and in css:

.container { width: 400px; float: left; }
.container .item { width: 50%; float: left; height: someFixedHeight; }
// or 200px

anyways, it's my preference to use tables only for displaying actual tables and avoid using them for layout. you can do anything you want with div's (or in this case you can also use ul and li. Of course it's not a must but normally it requires less HTML and for SEO the html-content ratio is something to consider. if you don't want fixed heights you can wrap each row as with the td/tr examples above.

while($res = mysql_fetch_array($q)){
$count++;
    echo "<tr>";
        foreach($res as $val){
                echo "<td>{$val}</td>";
        }
    echo "</tr>";   
}
while ($res = mysql_fetch_array($q)){

    if ($count % 2 == 0) {
        echo "<tr>";
    }

    echo "<td>{$res['id']}{$res['title']}</td>";

    if ($count % 2 == 0) {
        echo "</tr>";
    }

    $count++;
}

edit: I should reload more often.

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