Jump to content

[SOLVED] Mysql - Outputting table contents


shotos

Recommended Posts

hello,

i have a table i always display but always in increasing order with respect to ID column which is also auto-incremented and the primary key.

now, i want it displayed in decreasing order i.e. if mysql_num_rows=5, row 5 should be displayed first, then 4, then 3...............

 

my idea is to first check the number of rows, then make a query for each row to be displayed begining from the mysql_num_rows value.

am wondering if there isn`t a simple way to do this since with my idea above every row displayed will require read from the table.

 

$db = &new Mysql();

		$query = "select * from Element_$header";

		$result = mysql_query($query,$db->link); // or die(mysql_error(), . " Query was: $result");

		if(mysql_num_rows($result))

			{

				$column_count = mysql_num_fields($result);

				print ("<table border= 1, align=center cellpadding=10 cellspacing=0 >\n");

				print ("<tr>");

				for ($column_num= 0;$column_num < $column_count; $column_num++)

					{

						$field_name = mysql_field_name($result, $column_num);

						$field_name = trim($field_name);

						$field_name = str_replace("_"," ",$field_name);

						print("<th align=center>$field_name</th>");

					}

				print("</tr>\n");

				while($row = mysql_fetch_row($result))

					{

						print("<tr valign=top>");

						for ($column_num=0; $column_num<$column_count; $column_num++)

							{

								if($column_num == 2)

									{

										$write = $row[$column_num];
										$write = explode(" ",$write);
										$weekday = $write[0];
										$time = $write[1];
										$weekday = explode("-",$weekday);
										$time = explode(":",$time);
										print("<td align=center>$weekday[2]/$weekday[1]/$weekday[0], $time[0]:$time[1]</td>");



									}

								else

									{ 

										$write = $row[$column_num];

										$write = str_replace("_"," ",$write);

										print("<td align=center>$write</td>");

									}

							}

						print("</tr>\n");

					}

				print("</table>\n");

			}

		else

			{

				print("<font align=center>No Entries Available at the Moment!!!</font>");

			}

 

Link to comment
https://forums.phpfreaks.com/topic/122125-solved-mysql-outputting-table-contents/
Share on other sites

in you sql statement just do an ORDER BY the id in the table.

 

so

 

select * from Element_$header ORDER BY element_id DESC

 

that will output the list by the highest id first to the lowest

 

5

4

3

2

1

 

Hope this helps

 

Garry

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.