• 0

php loop results into a table


Question

Hi i've just started learning php and want to do something quite simple but just havent been able to get it to work.

currently i loop out the results of a mysql query into a list, the code is as follows:

		<ul class="homelist">
			<?php
			$years = @mysql_query('SELECT DISTINCT releaseyear FROM film order by 1');
			if (!$years) {
			exit('<p>Error retrieving years from database!<br />' .
			'Error: ' . mysql_error() . '</p>');
			}
			while ($year = mysql_fetch_array($years)) {
			$id = $year['releaseyear'];
			echo "<li>Films Released in $id</li>";
			}
			?>
		</ul>

what i would like to do instead is loop out the results into a table that is 5 columns wide as the list of years in the database is getting pretty long and this type of data would be more suited to a table imo.

I did a quick google search but nothing helpful came up, thought you guys could help.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

So instead of using a <li> to display the results, you want it in a table. Say 2 column table. Each column is the same data, just 2 columns make it easier to manage for display? Is that what you are getting at?

i create the initial html table tags outside of the for loop. Using a for loop, loop through one column, then when the counter reaches a certain number, branch off the results into the next column, once the for loop is done, close the table. I'm too busy at the moment to write an example code, but i have done this in the past, similar to what i mentioned. Seem to work fine.

Link to comment
Share on other sites

  • 0

Something like this should do it:

		&lt;table class="homelist"&gt;
		 &lt;thead&gt;&lt;tr&gt;&lt;th&gt;ID&lt;/th&gt;&lt;th&gt;Column 2&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
		 &lt;tbody&gt;
			&lt;?php
			$years = @mysql_query('SELECT * FROM film order by 1');
			if (!$years) {
			exit('&lt;p&gt;Error retrieving years from database!&lt;br /&gt;' .
			'Error: ' . mysql_error() . '&lt;/p&gt;');
			}
			while ($year = mysql_fetch_array($years)) {
			echo '&lt;tr&gt;';
			$id = $year['releaseyear'];
			echo "&lt;td&gt;$id&lt;/td&gt;";
			echo "&lt;td&gt;{$year['column2']}&lt;/td&gt;";
			echo '&lt;/tr&gt;';
			}
			?&gt;
		&lt;/tbody&gt;
		&lt;/table&gt;

Link to comment
Share on other sites

  • 0

I prefer:

		&lt;table class="homelist"&gt;
		&lt;tr&gt;
		&lt;th&gt;ID&lt;/th&gt;
		&lt;th&gt;Column 2&lt;/th&gt;
		&lt;th&gt;Column ....&lt;/th&gt;
		&lt;/tr&gt;
			&lt;?php
			$sql = mysql_query("yourmysqlsquery");
			$rowCheck = mysql_num_rows($sql);
			if ($rowCheck == 0) {
			   echo "Could not retrieve results";
			   die;
			}
			else {
			   while ($query = mysql_fetch_array($sql)) {
				  echo '&lt;tr&gt;';
				  $id = $query['releaseyear'];
				  echo "&lt;td&gt;$id&lt;/td&gt;";
				  echo "&lt;td&gt;{$year['column2']}&lt;/td&gt;";
				  echo '&lt;/tr&gt;';
			   }
			}
			?&gt;
		&lt;/table&gt;

David

Link to comment
Share on other sites

  • 0

mysql_fetch_array() doesn't return an associative array does it? There is one to give you an associative array but I can't remember it... I usually just get a normal array and select items with an integer ie. $year[0];

Link to comment
Share on other sites

  • 0

mysql_fetch_array() basically gets all the detail in that query. A few more ways are like this:

$row=mysql_query("mysqlquery");
for ($i = 0; $i &lt; mysql_fetch_assoc($row); $i++)
{
echo("&lt;td&gt;$row[$i]&lt;/td&gt;\n");
}

Just one row:

$result = mysql_query("mysqlquery");
if (!$result) {
	echo 'Query error: ' . mysql_error();
	die;
}

mysql_fetch_row($result);

echo $row[0];

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.