[thelist] php display 3x2
Andrew Maynes
andrew at humanbehaviour.co.uk
Tue Dec 3 10:58:01 CST 2002
To limit yourself to 3x2 (i.e., 6 items total), it's probably best to limit
your SQL query to six items. I assume that if you have more than 6 items in
your database you'll want to paginate as well?
So try:
// get the page variable from the query string
$page = $_GET['page'];
// if page variable doesn't exist, you're on page 1,
// otherwise convert to integer.
if (!$page) $page = 1;
else settype($page, "integer");
$sql = "SELECT ... FROM ... ORDER BY ... LIMIT " . ($page - 1 * 3) . ", 6";
This ensures that you will only six items in your result set.
I did try this but didn't have much success so I tried something else and guess
what... I still didn't have any success!
http://www.hificollective.co.uk/books/index.php
I think I have too much code working the query and have altered it so many times
I now appear to be running around in cirles. This is the code:
<?php
// dbconnect.php
include "common.php";
function list_records()
{
global $records_per_page;
global $sort_order, $order_by, $cur_page;
}
//connect to & select database
if(!($connect = mysql_pconnect("localhost", "root", "xxx"))){
print("Failed to connect to database!\n");
exit();
}
if(!mysql_select_db("hifi", $connect)){
print("Failed to select database!\n");
exit();
}
// do the query
$sql = "SELECT image, title FROM book_shop";
$result = mysql_query($sql, $connect);
// this query should produce only 6 records per page.
$query_data = mysql_fetch_row($result);
$total_num_locn = $query_data[0];
if(!$total_num_locn) error_message('No Books Found!');
$page_num = $cur_page + 1;
$total_num_page = $last_page_num
= ceil($total_num_locn/$records_per_page);
echo "<H4>Books found. Displaying the page $page_num out of
$last_page_num.</H4>";
if(empty($cur_page))
{
$cur_page = 0;
}
$limit_str = "LIMIT ". $cur_page * $records_per_page . ", $records_per_page";
$query = "SELECT image, item_no FROM book_shop $order_by_str $sort_order_str
$limit_str";
$result = mysql_query($sql);
if(!$result) error_message(sql_error());
// end of 6 records per page
print("<table>");
//set a base number to keep track of the columns
$record_column = 0;
while($query_data = mysql_fetch_object($result)){
// how many columns do we want...3
if($record_column <3){
//if this is the first column we need to print the table row tag
if($record_column == 0){
print("<tr>");
}
// print out 3 books for this column with proper tags
print("<td>" . $query_data->image . '<br>' . $query_data->title .
"</td>");
// increase the column number by one and get the next record
$record_column++;
} else {
// if there are 3 columns (like set above) then print the end tag
print("</tr>\n");
// reset the column record to begin new row
$record_column = 0;
}
}
echo "<BR>\n";
echo "<STRONG><CENTER>";
if($page_num > 1)
{
$prev_page = $cur_page - 1;
echo "<A HREF=\"$PHP_SELF?action=list_records& sort_order=$org_sort_order&
order_by=$order_by&cur_page=0\">[Top]</A>";
echo "<A HREF=\"$PHP_SELF?action=list_records& sort_order=$org_sort_order&
order_by=$order_by&cur_page=$prev_page\">[Prev]</A> ";
}
if($page_num < $total_num_page)
{
$next_page = $cur_page + 1;
$last_page = $total_num_page - 1;
echo "<A HREF=\"$PHP_SELF?action=list_records& sort_order=$org_sort_order&
order_by=$order_by& cur_page=$next_page\">[Next]</A> ";
echo "<A HREF=\"$PHP_SELF?action=list_records& sort_order=$org_sort_order&
order_by=$order_by& cur_page=$last_page\">[Bottom]</A>";
}
echo "</STRONG></CENTER>";
print("</table>");
$sql = "SELECT * FROM book_shop";
$result = mysql_query ($sql, $connect);
if(!$result)error_message(sql_error());
$i=0;
while($query_data = mysql_fetch_row($result))
{
$location_table[$i] = $query_data[1];
$i++;
}
$result = mysql_query ($sql, $connect);
if(!$result)error_message(sql_error());
$i=0;
while($query_data = mysql_fetch_row($result))
{
$sql[$i] = $query_data[1];
$i++;
}
?>
More information about the thelist
mailing list