• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

PHP and MySQL

Hello!

What's the easiest way of displaying records from a mysql db in a table like below ?

  +--------------------+--------------------+--------------------+
  |  Category1         |  Category2         |  Category3         |
  +--------------------+--------------------+--------------------+
  |  Category4         |  Category5         |  Category6         |
  +--------------------+--------------------+--------------------+
  |  Category7         |  Category8         |  Category9         |
  +--------------------+--------------------+--------------------+

What I have is a database table with a number of categories that I'd like displayed in a 3-column table with unlimited rows.... Hope this makes sense... I'm guessing you'd use some kind of counter ??
0
Julian Matz
Asked:
Julian Matz
  • 3
  • 2
1 Solution
 
dr_dedoCommented:
<?
$con = mysql_connect("localhost","root",""); //<-- connect to db
$con = mysql_select_db("test");                
$result = mysql_query("select cat as category from table1") or die(mysql_error());  // <--- ur query
echo "<table border=2 width=100%><tr>";  //<----- draw a table tag
for ($f=0;$f<mysql_num_fields($result);$f++){   //<--- loop through the field names and print them
     echo "<td>".mysql_field_name($result,$f)."</td>"; //<--- loop through the field names and print them
}
echo "</tr>";
while ($row = mysql_fetch_array($result)){ //<--- loop through the field names and print them
     echo "<tr><td>".$row[$f]."</td>";
     echo ($row = mysql_fetch_array($result))? "td>".$row[$f]."</td>": '' ;
     echo ($row = mysql_fetch_array($result))? "td>".$row[$f]."</td>": '' ;
     echo "</tr>";
}
echo "</table>";
?>

this will type the field name as category
0
 
Julian MatzAuthor Commented:
Thanks for your help! I'm not really getting it though...

I'm building a FAQ database and want to display the categories on the first page along with a short description... I don't need to display the filed names... Something like below, except instead of having one column, I'd like to do it with 3 columns maybe...

echo "<table style='width:100%;border:0;'>\n";

$result = mysql_query("SELECT * FROM kb_categories ORDER BY sort,title", $connection);
while ($row = mysql_fetch_assoc($result)) {
 echo "<tr><td>&bull; ".stripslashes($row['title'])."</td></tr>\n";
 echo "<tr><td>".stripslashes($row['description'])."</td></tr>\n";
}
mysql_free_result($result);

echo "</table>\n";


I'd also add links to the category title and display the number of entries per category, but I can do that later... Can the above be done using 3 columns instead of 1? I might be able to do it if you could maybe explain how the counter would work ??

Thanks again for your help!
0
 
Julian MatzAuthor Commented:
"filed name" should be --> "field names". Sorry.
0
 
dr_dedoCommented:
this should do it, i don't use a counter, i depend you display 3 columns, if u want, u can make a for loop in it. adding link won't be a probelm also.
-----------
<?
echo "<table style='width:100%;border:0;'>\n";
$con = mysql_connect("localhost","root","");
$con = mysql_select_db("test");
$result = mysql_query("SELECT * FROM kb_categories ORDER BY title") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
 echo "<tr><td>&bull; ".stripslashes($row['title'])."<br>".stripslashes($row['description'])."</td>";
 echo ($row = mysql_fetch_array($result))? "<td>&bull; ".stripslashes($row['title'])."<br>".stripslashes($row['description'])."</td>": '' ;
 echo ($row = mysql_fetch_array($result))? "<td>&bull; ".stripslashes($row['title'])."<br>".stripslashes($row['description'])."</td>": '' ;
 echo "</tr>\n";
// echo "<tr><td>""</td></tr>\n";
}
mysql_free_result($result);
echo "</table>\n";
?>
----------------
0
 
Julian MatzAuthor Commented:
Ok, I think I get it now. That's great, thanks!
-Julian.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now