Closed Thread Icon

Preserved Topic: PHP News script help Pages that link to <a href="https://ozoneasylum.com/backlink?for=17930" title="Pages that link to Preserved Topic: PHP News script help" rel="nofollow" >Preserved Topic: PHP News script help\

 
Author Thread
Burwell
Bipolar (III) Inmate

From: Drexel Hill, Pennsylvania
Insane since: Aug 2000

posted posted 04-11-2001 17:41

I recently finished reading PHP 4 Bible. Now I am trying to make my first script in PHP. I am running into trouble with the script that displays the news form my MySQL database. I want the script to display the news posting from the database by ID, form the greatest ID(The newst post), to the least ID(The oldest post). I am not shure how to order the results from the query. Any help is appreciated. I will enclose the display code below.

------------------------
//Get info from the database
$query = "SELECT * FROM Articles";
$result = mysql_query($query);

//Print out news
while(list($ID, $Title, $Date, $Content) = mysql_fetch_row($result))
{
ord($ID);
Print("<tr>\n");
Print("<td bgcolor='#6B635A' align='center'>\n");
Print("$Title\n");
Print("</td>\n");
Print("</tr>\n");
Print("<tr>\n");
Print("<td>\n");
Print("$Content\n");
Print("<br>\n");
//Print("<div align='right'>\n");
Print("Posted: $Date\n");
//Print("</div>\n");
Print("</td>\n");
Print("</tr>\n");
};
------------------------
Please note that this is not the full script code.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 04-11-2001 18:16

Well your code is a bit sloppy, but that is OK, remember PHP is an embedded language, so you can put the HTML tags anywhere in the doc, which may make things easier to keep a handle on. Your problem there would be with SQL and not PHP.

$query = "SELECT * FROM Articles ORDER BY ID DESC";

The above selects all the information in Articles and orders them by ID in a descending order. 100, 99, 98. You could have it go 1, 2, 3 by using.

$query = "SELECT * FROM Articles ORDER BY ID";

On a side note. Work on variable naming. Variables should tend to be lowercase, as well as DB names and the like. It makes it loads easier to write and read the SQL statements.

Examples:

$query = "SELECT * FROM articles ORDER BY id";
$query = "SELECT * FROM articles ORDER BY id DESC";

They are just small changes in syntax but it could lead to an easier way or reading.

Now for my code:

code:
//Get info from the database 
$query = "SELECT * FROM articles ORDER BY id DESC";
$result = mysql_query($query);

//Print out news
while(list($ID, $Title, $Date, $Content) = mysql_fetch_row($result))
{
$display_block = "<tr>
<td bgcolor=\"#6B645A\" align=\"cener\">$Title</td>
</tr>
<tr>
<td>$Content<br><div align='right'>Posted: $Date</div></td>
</tr>"
};

echo "$display_block";



I am not going to attempt to say I completely the way you code. That is your deal, I would however, work on error suppressions.

Good luck,
-mage-

<edit> Just saw ord, that would simple order the current value. Because the while only brings one value in at a time.</edit>

[This message has been edited by WarMage (edited 04-11-2001).]

Burwell
Bipolar (III) Inmate

From: Drexel Hill, Pennsylvania
Insane since: Aug 2000

posted posted 04-11-2001 19:28

thanks for the help. I have been working on this for a few days now, and the answer seems so simple. I appreciate it. If you are referring to the ord() function, if i am not mistaken i tryed it and it did not work. Then again I am new to this, so i could have been doing something wrong. Thanks again.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 04-11-2001 19:42

You are correct it did not work.

Think of this, you have your data saved into a weird looking array.

Your while loop occures
the value 12 is grabbed and stored in $ID
you then call ord($ID);
so now $ID still holds the value 12.
The while loop runs again.
the value 13 is grabbed and stored in $ID now
they you call ord($ID)
so now $ID still holds the value 13.

You see how that works, you are not storing lots of info into $ID just a simple integer. So you have to have the order occure in the SQL statement not in the grabbing portion.

<error found> Above I wrote $display_block = which should be $display_block .=</error found>

-mage-

Burwell
Bipolar (III) Inmate

From: Drexel Hill, Pennsylvania
Insane since: Aug 2000

posted posted 04-11-2001 20:00

Ahh I see. Good debugging on your part, I never would have caught that. Thanks again!

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 04-11-2001 20:53

Also, on the MySQL side, make sure your ID field is an index if you plan on having lots of rows. Makes that ORDER BY clause dramatically faster.

« BackwardsOnwards »

Show Forum Drop Down Menu