mysql_select_db("tdd1984") or die("Problem selecting database"); $type = mysql_real_escape_string ($_GET['type']); //pagination strts $limit = 25; $sql = "SELECT itemId, userId, description, date, city, county, idNum, name, price, type COUNT FROM (items JOIN user ON items.userId=user.ID) LEFT JOIN images ON items.itemId=images.idNum"; if (isset($_GET['type'])) { $sql .= " WHERE type='". mysql_real_escape_string($_GET['type'])."'"; } $sql .="GROUP BY itemId"; $result_count = mysql_query($sql) or die ("MySQL Error: ".mysql_error()); $numofrows = mysql_num_rows ($result_count); //pagination total number of rows 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 //pagination query2 $sql1 = "SELECT itemId, userId, description, date, city, county, idNum, name, price, type FROM (items JOIN user ON items.userId=user.ID) LEFT JOIN images ON items.itemId=images.idNum LIMIT $limitvalue, $limit"; if (isset($_GET['type'])) { $sql .= " WHERE type='". mysql_real_escape_string($_GET['type'])."'"; } $sql .="GROUP BY itemId"; $result = mysql_query($sql1) or die("Error: " . mysql_error()); //pagination if statement if nothing is to be returned :) if(mysql_num_rows($result) == 0){ echo("Nothing to Display!"); } echo "\n"; echo "\n"; //pagination while loop while($row = mysql_fetch_array($result)){ for($i = 0; $i < $numofrows; $i++) { //get a row from our result set if($i % 2) { //this means if there is a remainder echo "\n"; } else { echo "\n"; } if($row['idNum'] == null) { echo ""; } else { echo ""; } echo ""; echo "\n"; echo "\n"; } } echo "
  ".$row['name']."
".$row['description']."
$".$row['price']." ".$row['date']." ".$row['city']." ".$row['county']."
\n"; //pagination next button if($page != 1){ $pageprev = $page--; // Fancy way of subtracting 1 from $page echo("PREV".$limit." "); /* 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 = $$numofrows / $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("$i "); } /* 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(($numofrows % $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("$i "); } /* This is the exact statement that turns pages into link form that is used above */ } // Ends the if statement if(($numofrows - ($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("NEXT".$limit.""); /* 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. */ } ?>