944,205 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 12466
  • PHP RSS
Sep 20th, 2007
0

Displaying query results dynamically in table

Expand Post »
I am trying to display MySQL query results dynamically in a table. Based on the query, the table will have a maximum of 3 columns and 15 rows. I would like to have the results display as follows:

Record 1 Record 16 Record 31
Record 2 Record 17 Record 32

Record 15 Record 30 Record 45

If the query returns less than 16 records, everything should display in the first column; if more than 15 but less than 31 - 2 columns, et al.

I am fairly new to PHP and am trying to learn all I can as I go along. Any books, websites, or online classes you can suggest to increase my knowledge of PHP would be greatly appreciated.

Thanks,

Deb
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hsmom314 is offline Offline
4 posts
since Sep 2007
Sep 20th, 2007
0

Re: Displaying query results dynamically in table

ok i will give you an example:

PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <?php
  6. $host=""; // Host name
  7. $username=""; // Mysql username
  8. $password=""; // Mysql password
  9. $db_name=""; // Database name
  10. $tbl_name=""; // Table name
  11.  
  12. // Connect to server and select databse.
  13. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  14. mysql_select_db("$db_name")or die("cannot select DB");
  15.  
  16. $user_result = "select * from table;";
  17. $qry = mysql_query($user_result) OR die(mysql_error());
  18. $user_array = mysql_fetch_assoc($qry);
  19. echo "<center>";
  20. echo "<table CELLPADDING=10 border =1 >";
  21. echo "<tr>";
  22. echo "<td>".$user_array['field 1']."</td>";
  23. echo "<td>".$user_array['field 2']."</td>";
  24. echo "<td>".$user_array['field 3']."</td>";
  25. echo "</tr>";
  26. echo "</table>";
  27. }
  28. mysql_close();
  29. ?>
  30.  
  31. </body>
  32. </html>

hope that helps you
Last edited by sam1; Sep 20th, 2007 at 10:52 am.
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Sep 20th, 2007
0

Re: Displaying query results dynamically in table

Thank you for your suggestion. However, I don't think I explained what I'm trying to do well enough. I am trying to display one field from my database in this table. The results from the one field should display in a table vertically (max. 15 rows) then start a new column. I've written some code (using a while loop and count function) that will display it horizontally but everything that I try to get it to display vertically ends up duplicating the same record.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hsmom314 is offline Offline
4 posts
since Sep 2007
Sep 20th, 2007
0

Re: Displaying query results dynamically in table

try using the SQL limit instruction.for example:
PHP Syntax (Toggle Plain Text)
  1. SELECT * FROM 'users' LIMIT 0 10;
  2. SELECT * FROM 'users' LIMIT 10 10;

the first number tell you the index in which the SQL DB needs to start from and the second number tell you the number of rows to be selected.
I think you can easily figure it out.
Also, try using MySQL manual if you use MySQL as your DBMS.

Good luck.
Reputation Points: 55
Solved Threads: 11
Junior Poster
mostafadotnet is offline Offline
157 posts
since Jul 2006
Sep 20th, 2007
0

Re: Displaying query results dynamically in table

You will of course have to implement a paging mechanism if your results exceed rows*columns and you will need to get all of the items into a single array before you generate the <tr> and <td> elements. The following example code shows how you can generate the table you want
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $data = array("a","b","c","d","e","f","g","h","i","j");
  3. $rows = 4;
  4. $itemCount=count($data);
  5. echo "<table>";
  6. for ($row=0; $row < $rows; $row++){
  7. echo "<tr>";
  8. for ($itemIndex=$row; $itemIndex < $itemCount; $itemIndex+=$rows){
  9. echo "<td>".$data[$itemIndex]."</td>";
  10. }
  11. }
  12. echo "</table>";
  13. ?>
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 23rd, 2007
0

Re: Displaying query results dynamically in table

Here's a quick and dirty way to do it with nested "for" loops:

$strQS="select field from table order by index";
$refItems=mysql_query($strQS) or die (mysql_error());
$cols=ceil(mysql_num_rows($refItems)/15);
$tableData=array();
for($x=0;$x<$cols;$x++)
{
	for($y=0;$y<15;$y++)
	{
		$tableData[$y][$x]=@mysql_fetch_assoc($refItems);
	}
}
echo <<<TABLESTART
<table>
<tr>
<th colspan={$cols}>
Your Header Here
</th>
</tr>
TABLESTART;
for($x=0;$x<15;$x++)
{
	echo "<tr>\n";
	for($y=0;$y<$cols;$y++)
	{
		echo "<td>".$tableData[$x][$y][field]."</td>\n";
	}
	echo "</tr>\n";
}
echo "</table>"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
schizoman is offline Offline
6 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to insert open website into a page in my site?
Next Thread in PHP Forum Timeline: phpBB3 installed, need help installing portal





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC