Help with tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjar81
Forum Newbie
Posts: 4
Joined: Thu Jan 29, 2004 4:23 pm

Help with tables

Post 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
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post 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
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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.
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post 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.
Last edited by Pointybeard on Thu Jan 29, 2004 5:03 pm, edited 1 time in total.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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 ;-)
mjar81
Forum Newbie
Posts: 4
Joined: Thu Jan 29, 2004 4:23 pm

Post 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);
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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);
?>
mjar81
Forum Newbie
Posts: 4
Joined: Thu Jan 29, 2004 4:23 pm

Post by mjar81 »

THAT DID IT!


THANK YOU SOOOOO MUCH!

i'll be coming back here!
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

:-) Glad to be off some service



Por Supuesto y sin culpa :lol:
mjar81
Forum Newbie
Posts: 4
Joined: Thu Jan 29, 2004 4:23 pm

i'm stupid

Post 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
Post Reply