Hello, I am quite new to php (although I recognize things from learning a tad java for half a semester way back)
and have just written this script that, among other things, pulls data (blog posts) from a mysql table and prints them, LIFO.
I have a for loop that first does a mysql_fetch_array, then this array
is put into another array ($blog). The $blog array is then read backwards in a second for loop.
Now, what I have done works, after much trial and error - but I am thinking there could be a faster/better/easier way of doing this.
So for my learning's sake, if anyone has a better way for me to use next time, it would be appreciated. Thanks!
Here's my take:
<?php
...
$result = mysql_query("SELECT * FROM $table") or die('Error, query failed');
$sql2 = "SELECT COUNT(*) FROM $table"; // used for counting number of rows in sql table
$result2 = mysql_query($sql2);
$num = mysql_result($result2, 0); // $num is the number of rows found in the table
for($i = 0; $i < $num; $i++) {
$row = mysql_fetch_array($result); // makes an array of the first row in the table. Repeat for next row.
$blog[] = $row; // an array $blog that contains the first array. Indexed, starts at 0 and increments.
}
for($i = ($rowsPerPage-1); $i >= 0; $i--) { // prints the contents of the $blog backwards
$post = $blog[$i];
echo($post['field1']);
echo($post['field2']);
echo($post['field3']);
//...etc
...
?>