1
votes

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.

5
Do you mean table and not array? - vascowhite
why would you put James and john in the same row if i may ask? is there a connection between them? you should use tables for presenting lists etc, but if you only want to make a nice layout for the page i'd recommend using <div> instead. you can use float: left and set the width to 50% for example - galchen
@vascowhite: yes sir, it's actually a table not an array - Dan
@galchen: the way the names being arranged is base on their ID's in the database, James is 1, John is 2, Paul is 3 and Peter is 4. - Dan
still, is it just a design thing to put them in the same row? or is there a relation? because if you just want to place them one next to another there's a better way (less html). btw, it has nothing to do with the actual question, but more with the habit of designing correct html pages - galchen

5 Answers

1
votes

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.

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

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>
0
votes
while($res = mysql_fetch_array($q)){
$count++;
    echo "<tr>";
        foreach($res as $val){
                echo "<td>{$val}</td>";
        }
    echo "</tr>";   
}
0
votes
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.