1

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.

6
  • 2
    Do you mean table and not array? Commented Sep 4, 2011 at 14:41
  • 2
    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 Commented Sep 4, 2011 at 14:53
  • @vascowhite: yes sir, it's actually a table not an array Commented Sep 4, 2011 at 22:56
  • @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. Commented Sep 4, 2011 at 22:58
  • 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 Commented Sep 4, 2011 at 23:13

5 Answers 5

3
echo "<tr>";
while($res = mysql_fetch_array($q)){
    $count++;
    if (!($count % 2)){ echo "</tr><tr>"; }
    echo "<td>{$res['id']}{$res['title']}</td>";
}
echo "</tr>";
Sign up to request clarification or add additional context in comments.

3 Comments

Not quite. This will lead to an empty <tr></tr>.
@str: where did you get that?
if (1 % 2) in the first iteration.
1

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.

Comments

1

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>

1 Comment

when I test the code , it returns to an error statement: Notice: Undefined variable: cell in C:\wamp\www\tests\rows.php on line 23
0
while($res = mysql_fetch_array($q)){
$count++;
    echo "<tr>";
        foreach($res as $val){
                echo "<td>{$val}</td>";
        }
    echo "</tr>";   
}

Comments

0
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.

1 Comment

sir, though the code returns no error but the output is in 1 column only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.