Page 1 of 1

Help with tables

Posted: Thu Jan 29, 2004 4:23 pm
by mjar81
ok, so i have this code that pulls data from my mySQL database.

i want the table to display the most recent post first (in reverse order using the primary key identifyer)

Code: Select all

<TABLE align="center" border="1" cellpadding="2" cellspacing="0" bordercolor="000000" width="80%">
<?php

//connect to the database "localhost" using the supplied
//username and password and assign it the variable $db
$db = mysql_connect("localhost", "mjar81", "******");

//select the database "prayer" from mySQL 
mysql_select_db("prayer",$db);

//assign the variable $result to everything from the "prayer_content" table
$result = mysql_query("SELECT * FROM prayer_content",$db);

//this outputs html to format the table and input some text
echo"<TR><TD><B>Name:</B><TD><B>Request:</B><TD><B>Date Submitted:</B><TD><B>Email:</B></TR>";

//while loop assigning $myrow to the $result array
while($myrow = mysql_fetch_array($result))
	&#123;
		echo "<TR><TD>";
		echo $myrow&#1111;"name"];
		echo "<TD>";
		echo $myrow&#1111;"request"];
		echo "<td>";
		echo $myrow&#1111;"date"];
		echo "<br \>";
		echo $myrow&#1111;"time"];
		echo "<TD>";

		//if statement to decide whether or not to display the email
		if($myrow&#1111;"displayemail"] == 1)
			&#123;
				//email display function
				//html code
				echo "<a href=mailto:";
				echo $myrow&#1111;"email"];
				echo ">";
				echo $myrow&#1111;"email"];
				echo "</a>"; 
			&#125;
			else
			&#123;
				//don't display the e-mail
				echo "Hidden.";
			&#125;
&#125;

//close the table
echo "</TABLE>";
?>
any idea how to implement this... i'm a newbie!

thanks,
mjar81

Posted: Thu Jan 29, 2004 4:42 pm
by Pointybeard
Urm...dont exactly follow your question there. Are you wanting help with the query?

SELECT * FROM prayer_content ORDER BY `FIELD` DESC

Would do what you want. That'll pull all the records and in decending order based on the field you specify. Just replace 'FIELD' with your primary key field.

Hope thats what you meant. :p

-PB

Posted: Thu Jan 29, 2004 4:46 pm
by ol4pr0
Dont think that will only show the last post by just using

Code: Select all

ORDER BY `FIELD` DESC LIMIT <somenumber>"
Dont know what will tho.

Posted: Thu Jan 29, 2004 4:56 pm
by Pointybeard
most recent post first
i think mjar81 wants to show all the posts....fairly vague question tho...

What your saying would show only the last post....

SELECT * FROM `prayer_content` ORDER BY `FIELD` DESC LIMIT 0, 1

as long as the thing your ordering by will get you the 'last' post. Prolly wanna order by date or somthing.

Posted: Thu Jan 29, 2004 4:58 pm
by ol4pr0
Pointybeard wrote: SELECT * FROM `prayer_content` ORDER BY `FIELD` DESC LIMIT 0, 1

as long as the thing your ordering by will get you the 'last' post. Prolly wanna order by date or somthing.
That makes more sence ;-)

Posted: Thu Jan 29, 2004 5:14 pm
by mjar81
woah, more replies than i could hope for in such a short time!

i'm sorry that my question sounded vague... it seemed OK to me, but i know what i'm talking about!

i'm looking to display ALL results from the table, in descending order.

anyways, the solution you guys offered to me worked for sorting the list, but when i refer to another php file and then refresh the first page (the one i'm having the problem with) i get a "parse error" on the line that i've modified from:

Code: Select all

$result = mysql_query("SELECT * FROM prayer_content",$db);
to

Code: Select all

$result = mysql_query("SELECT * FROM prayer_content ORDER BY 'Id' DESC LIMIT 50",$db);

Posted: Thu Jan 29, 2004 5:18 pm
by ol4pr0

Code: Select all

<?
$connect= mysql_connect('localhost', 'username', 'password')
	or die("Cant connect: " .mysql_error());

mysql_select_db($db, $connect); 

$query = ("SELECT * FROM prayer_content ORDER BY 'Id' DESC LIMIT 50"
$result = mysql_query($query);
?>

Posted: Thu Jan 29, 2004 5:29 pm
by mjar81
THAT DID IT!


THANK YOU SOOOOO MUCH!

i'll be coming back here!

Posted: Thu Jan 29, 2004 5:32 pm
by ol4pr0
:-) Glad to be off some service



Por Supuesto y sin culpa :lol:

i'm stupid

Posted: Thu Jan 29, 2004 9:09 pm
by mjar81
i finally realized my problem wasn't entirely in coding....

my index.php referred to a page called post.php and it had a 0second meta refresh to go back to the first page...

stupid me, i used dreamweaver to preview it and forgot to save so i would keep getting the same error when it went back to the index.php page!

thanks for the help today... my next task is to write an administrator access module!

fun fun!
thanks again.
mjar81