Forum Moderators: coopster
Is it possible, or do I need to create a new page?
Another words, If $book_array =5, then start a new page
Here is the code as exists:
function display_books($book_array)
{
//display all books in the array passed in
if (!is_array($book_array))
{
echo '<br />No items currently available in this category<br />';
}
else
{
//create table
echo '<table width = \"100%\" border = 0>';
//create a table row for each book
foreach ($book_array as $row)
{
$url = 'show_book.php?item_number='.($row['item_number']);
echo '<tr><td>';
if (@file_exists('images/'.$row['item_number'].'.jpg'))
{
$team = '<img src=\'images/'.($row['item_number']).'.jpg\' border=0>';
do_html_url($url, $team);
}
else
{
echo ' ';
}
echo '</td><td>';
$team = $row['player'].' of the '.$row['team'];
do_html_url($url, $team);
echo '</td></tr>';
}
echo '</table>';
}
echo '<hr/>';
}
As always, thanks for your help.
What am I doing wrong, is my logic way off?
This is what currently exists:
function get_books($catid,$offset)
{
// query database for the books in a category
if (!$catid ¦¦ $catid=='')
return false;
$conn = db_connect();
$query = "select * from books where catid='$catid' limit $offset, 5";
$result = @mysql_query($query);
if (!$result)
return false;
$num_books = @mysql_num_rows($result);
if ($num_books ==0)
return false;
$result = db_result_to_array($result);
return $result;
}
My try:
if($result=="0"){
$result="Nothing here";
}
if($result=="1"){
$result="End of items";
}
if($result>"1"){
$result="<a href=\"show_cat.php?catid=".$catid."&offset=".($offset - 2)."\"><< Previous</a> -
<a href=\"show_cat.php?catid=".$catid."&offset=".($offset + 2)."\">Next >></a></p>";
";
}
$query = "select * from books where catid='$catid' limit $offset, 5";$result="<a href=\"show_cat.php?catid=".$catid."&offset=".($offset - 2)."\"><< Previous</a> -
";
There's something you're still not getting I think. If you say what you think you're doing, I think we'll get to the root of why it's not doing that.
Am I right that your goal is first to understand this so that you can do it on your own, and you don't just want "the answer" without understanding it?
Cheers,
Tom
What happens now is the prev/next links appear even if there is not new items.
I want the code to say if there are more items show the links, otherwise don't show them.
Does that make sense?
Oh ok, If am understand correctly, the results of the query is called $result. We are searching the db and pulling out the first 5 cat id's (items). I hope to "grab" all the books (which are now items)
So far so good. Let's assume that there are 10 cat_id's that match the WHERE part of your query. You apply a limit and grab the first five. Your result set now has five items.
and if the $result of the query is greater then 1
What do you mean by this? The "result" as far as MySQL is concerned, is a link identifier and has nothing to do with the number of items returned. To get that number you need to do something like
$num_rows = mysql_num_rows($result);
So let's assume that's what you mean and $num_rows is greater than 1.
to display the next/prev links in increments of 2. If the result is 1 to have it say end or something to the effect and if the result is 0 then say "end"
Okay, so what if $num_rows = 2. That's greater than 1, but you don't want to show multiple pages, you want one page with two items. So to go back up a step, you mean that if the result is greater than 2, you will make multiple pages.
With me so far?
Assuming a yes answer to that one, I have two more questions. DO NOT POST CODE. Answer these questions just in plain language and pseud-code, not PHP.
1. How will you figure out what the offset is?
2. How do you know which page you are on?
Tom
I thought offset was determined on how many items to be shown on a page.
Ah ha! Now we're getting somewhere. I thought you were thinking that. In fact you are right, but you are missing a piece.
1. number of items on a page is determined by the limit: "limit 5" (no offset) means you put 5 items on the page.
2. offset is determined by the number of items on each page and by the page number of the page you are on. If you do not know the page number, you can not know the offset!
Go back and reread the following messages from earlier in this thread and see if they make more sense now:
26, 28, 33, 42, 46, 51
I ask again
1. How will you figure out what the offset is?
2. How do you know which page you are on?
I should, however, have asked them in the reverse order.
First you figure out which page you are on. How? You tell it in the url as in href="search_results.php?page=3". In your script $page_num = $_GET['page'].
THEN you figure offset. If you reread the posts I mentioned, you should see how to calculate offset by knowing how many records appear on each page and the number of the page you are on.
"accurate" does the number have ot be. For example what happens if the offset is set to 1000 and I know that there wont be 1000 records?
You need to check to make sure your query actually returns results. However, since we will calculate the number of pages, this situation (offset larger than the record set) will not happen.
Tom