Right now the code works as it should I imagine... It prints out one table row (probably 20 screens wide), with each product displayed in a cell.
What I'm trying to do is get the results to print out 4 products in a row (one per cell), with as many rows as are needed to display the results.
From what I've read it seems like I need to incorporate a COUNT function, and a few if else if statements, but I don't have a clue as to how I should write them out. This is all still very much a new language to me.
you'll need to change up a couple of lines in here relating to your database tables and queries but this'll put your results into a four-column format and paginate as necessary based on a set number of results per page as specified by $limit at the beginning. I figured that'd be better than a potentially infinately long page of results The limit's set to 8 right now so you'll get two rows of 4 unless you change it.
Hope this works for ya and good luck!
Code:
<table width="800">
<tr>
<td>
<?php
// open connection
$conn = mysql_connect("localhost", "my_username", "pass");
// pick database
mysql_select_db("my_database",$conn);
// bring out the results of our query and format them in rows *and* columns.
// set the number of results to display on each page... 4x2=8
if (!($limit)){
$limit = 8;
}
// check to see if we're on the first page of the results
if (!($page)){
$page = 0;
}
// in order to paginate the results we need to know in advance how many there are
$numresults = mysql_query("SELECT * FROM yourTable WHERE column = 'value'");
$numrows = mysql_num_rows($numresults);
// if there are no results, print a friendly message
if ($numrows == 0){
print("<td>There are currently no results for this query</td>\n
</tr>\n</table>\n");
exit();
}
// divide the number of results by the number displayed on each page, to get number of pages
$pages = intval($numrows/$limit);
// if there are results left over, we need to add one more page
if ($numrows%$limit) {
$pages++;
}
$current = ($page/$limit) + 1; // calculate the current page number.
if (($pages < 1) || ($pages == 0)) {
$total = 1;
} else { // if $pages is less than one or equal to 0, total pages equals 1
$total = $pages;
} // if not, total pages is the value of $pages
$first = $page + 1;
//if this is not the last results page, last result = $page plus $limit
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;
} else { // if this is the last results page, last result equals total number of results
$last = $numrows;
}
// below here can be changed if you want more or less columns etc
$numcols = 4; // how many columns we want on the page
$numcolsprinted = 0; // how many columns we have so far, do not change!
// get the results to be displayed in this set
$query = "SELECT * FROM FROM yourTable WHERE column = 'value' LIMIT $page, $limit ";
$result = mysql_query($query, $mysql_link) or die (mysql_error());
// get each row
while ($row = mysql_fetch_array($result)) {
$producturl = $row["ProductURL"];
$imageurl = $row["ImageURL"];
$title = $row["Title"];
// we have to check to see if a row has been completed and if it has, start a new one
if ($numcolsprinted == $numcols) {
print "</tr>\n<tr>\n";
$numcolsprinted = 0;
}
// output a row of results with columns evenly spaced
print " <td><a href=\"$producturl\"><img src=\"$imageurl\" border=\"0\"></a><br><a href=\"$producturl\">$title</a></td>\n";
// increment row counter
$numcolsprinted++;
} // end while loop, at this point we have all the results
// now we need to pad out the table with empty cells if there's a final row with
// fewer results than the number of columns we want to display
$colstobalance = $numcols - $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {
print " <td> </td>\n
</tr>";
}
?>
<tr>
<td colspan="4"></td>
</tr>
<tr>
<td colspan="4">
<?php
if ($page != 0) { // don't show back link if this is the first page
$back_page = $page - $limit;
echo(" <a href=\"$PHP_SELF?query=".$query."&page=$back_page&limit=$limit\">previous</a> \n");
} else {
print (" ");
}
$ppage = $limit*($i - 1);
print ("page $current of $pages");
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // if this isn't the last page show 'next' link
$next_page = $page + $limit;
echo(" <a href=\"$PHP_SELF?query=".$query."&page=$next_page&limit=$limit\">next</a> \n");
} else {
print (" ");
}
?>
</td>
</tr>
</table>