Forum Moderators: coopster

Message Too Old, No Replies

If results ="x", then start a new page

can it work?

         

Acternaweb

1:41 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



Not sure if I am saying this right, so please bare with me. I have a db that is showing results but the results can get lengty, so I want a new page to start if the results exceed X.

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 '&nbsp;';
}
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.

Acternaweb

3:15 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



Ok, some of that is way over my head, :-)
but I tried this, and not having luck

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>";
";
}

ergophobe

4:51 pm on Jan 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How about this... Can you comment in plain but detailed english on what you expect the following lines to do.


$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

Acternaweb

5:10 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



Thanks, there is a lot I don't get, lol. I think the book I have now is going against what the first book said.

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?

ergophobe

5:31 pm on Jan 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That I understand, but in real detail, what do you expect each of the statements that I put in the box above to do for you. Not generally, but those specific statements... in detail.

Tom

Acternaweb

6:35 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



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) and if the $result of the query is greater then 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"

ergophobe

7:44 pm on Jan 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




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

Acternaweb

3:30 pm on Jan 28, 2004 (gmt 0)

10+ Year Member




I have no idea, and coudn't find anything in my book about it, so I may be way off. But I have a question, how "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?

How do you know which page you are on?

Acternaweb

3:32 pm on Jan 28, 2004 (gmt 0)

10+ Year Member



Wait I think I just confused offset and limit. I thought offset was determined on how many items to be shown on a page.

ergophobe

5:28 pm on Jan 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




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

This 69 message thread spans 3 pages: 69