djrichwz Posted July 10, 2007 Share Posted July 10, 2007 Hi I have installed php on my local computer on the apache server and all of my php scripts work and will connect to the mysql database but when i try to run the php script for pagination on this site under tutorials it just shows me a blank page with no error has anybody got any ideas and can you please help me. I really dont know what is causing this code follows below <?php mysql_connect("localhost", "root", "ws5212") or die(mysql_error()); mysql_select_db("test") or die("Can not find database"); echo "Connected to MySQL"; $limit = 3; // Sets how many results shown per page $query_count = "SELECT count(*) FROM newstable"; // Sets what we want to pull from the database // count(*) is better for large databases (thanks Greg!) $result_count = mysql_query($query_count); // Pulls what we want from the database $totalrows = mysql_num_rows($result_count); // This counts the number of users echo "connected to newstable" if(empty($page)){ // Checks if the $page variable is empty (not set) $page = 1; // If it is empty, we're on page 1 } $limitvalue = $page * $limit - ($limit); // Ex: (2 * 25) - 25 = 25 <- data starts at 25 $query = "SELECT * FROM newstable LIMIT $limitvalue, $limit"; $result = mysql_query($query) or die("Error: " . mysql_error()); // Selects all the data from table. // mysql_error() will print an error if one occurs. /* Tip: The MySQL LIMIT value syntax is as follows: LIMIT $row_to_start_at, $how_many_rows_to_return */ if(mysql_num_rows($result) == 0){ echo("Nothing to Display!"); } // This reads the number of rows returned // from the result above. /* Tip: You could probably use if($totalrows == 0) for the if statement; however, reading the actual $result from the data you'll be printing to the screen is more accurate, and is a surefire way of preventing certain errors. */ $bgcolor = "#E0E0E0"; // light gray /* Sets up one of the background colors. The color stated here will be displayed second */ echo("<table>"); // Just a simple table while($row = mysql_fetch_array($result)){ /* This starts the loop (a while loop). What this does is returns a row of data to the $row array. Each time the loop restarts, the next row of data is used. So, each pass of the loop returns a different row of data. */ // Start The Background Check if($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF"; }else{ $bgcolor = "#E0E0E0"; } /* Tip: The color you want to appear first on the list should be where the #FFFFFF is listed. What this script does is checks to see if #E0E0E0 was used last; if it was, then it'll use #FFFFFF. All you have to do is replace the colors of your choice. */ echo("<tr bgcolor=".$bgcolor.">n<td>"); // Here we start table row & table data // Make sure your bgcolor is the $bgcolor variable echo($row["title"]); /* Tip: This is how we add the users field from our row. You can use any field from your row: all you do is include the field's name inbetween the array brackets. Ex: $row["field_name_here"] */ echo("</td>n<td>"); // Here we end the one section of table data, and start another. echo($row["summary"]); // Prints the usersID field echo("</td>n</tr>"); // Here we end the table data & table row } /* This closes the loop. Anything after this bracket will display after the data you've pulled from the database. */ echo("</table>"); /* While we're at it, let's close the table tag--we don't want other data inside this table */ if($page != 1){ $pageprev = $page--; // Fancy way of subtracting 1 from $page echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); /* Tip: It is a good idea NOT to use $PHP_SELF in this link. It may work, but to be 99.9% sure that it will, be sure to use the actual name of the file this script will be running on. Also, the adds a space to the end of PREV, and gives some room between the numbers. */ }else echo("PREV".$limit." "); // If we're on page 1, PREV is not a link $numofpages = $totalrows / $limit; /* We divide our total amount of rows (for example 102) by the limit (25). This will yield 4.08, which we can round down to 4. In the next few lines, we'll create 4 pages, and then check to see if we have extra rows remaining for a 5th page. */ for($i = 1; $i <= $numofpages; $i++){ /* This for loop will add 1 to $i at the end of each pass until $i is greater than $numofpages (4.08). */ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF&page=$i\">$i</a> "); } /* This if statement will not make the current page number available in link form. It will, however, make all other pages available in link form. */ } // This ends the for loop if(($totalrows % $limit) != 0){ /* The above statement is the key to knowing if there are remainders, and it's all because of the %. In PHP, C++, and other languages, the % is known as a Modulus. It returns the remainder after dividing two numbers. If there is no remainder, it returns zero. In our example, it will return 0.8 */ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF&page=$i\">$i</a> "); } /* This is the exact statement that turns pages into link form that is used above */ } // Ends the if statement if(($totalrows - ($limit * $page)) > 0){ /* This statement checks to see if there are more rows remaining, meaning there are pages in front of the current one. */ $pagenext = $page++; // Fancy way of adding 1 to page echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); /* Since there are pages remaining, this outputs NEXT in link form. */ }else{ echo("NEXT".$limit); /* If we're on the last page possible, NEXT will NOT be displayed in link form. */ } mysql_free_result($result); /* This line is not required, since MySQL will free the result after all scripts have finished executing; however, it's a nice little backup. */ // The next line tells the server to stop parsing PHP ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.