Organizational Research By

Surprising Reserch Topic

how to create php 2 column table with values from the database


how to create php 2 column table with values from the database  using -'php,mysql,table'

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

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 guys.Thanks.
    

asked Oct 19, 2015 by virendra.bajaj
0 votes
6 views



Related Hot Questions

5 Answers

0 votes

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

echo '
'; while($res = mysql_fetch_array($q)){ echo '
'. $res['id'] . $res['title'] . '
'; } echo '
';

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.

answered Oct 19, 2015 by rajeshujade
0 votes
echo "";
while($res = mysql_fetch_array($q)){
    $count++;
    if (!($count % 2)){ echo ""; }
    echo "{$res['id']}{$res['title']}";
}
echo "";
answered Oct 19, 2015 by rajnipancholi
0 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

answered Oct 19, 2015 by vimaldas2005
0 votes
while($res = mysql_fetch_array($q)){
$count++;
    echo "";
        foreach($res as $val){
                echo "{$val}";
        }
    echo "";   
}
answered Oct 19, 2015 by pradip.bhoge
0 votes
while ($res = mysql_fetch_array($q)){

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

    echo "{$res['id']}{$res['title']}";

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

    $count++;
}

edit: I should reload more often.

answered Oct 19, 2015 by sachin valanju

...