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.

lorax

3:19 pm on Jan 5, 2004 (gmt 0)

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



Are you shooting to simply limit the output for one page or are you intending to make your output pages function similarly to how WebmasterWorld does it? EG show links to the pages that contain the rest of the possible output.

Acternaweb

3:52 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



show links to the pages that contain the rest of the possible output. This way the visitor doesn't have to scroll down for miles.

Thanks

figment88

4:04 pm on Jan 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



are you dynamically generating the pages or creating static pages that you'll upload to the server?

ergophobe

4:30 pm on Jan 5, 2004 (gmt 0)

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



This article on evolt.org may get you going

Multiple Pages with PHP [evolt.org]

Tom

figment88

5:22 pm on Jan 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For static pages, I would do something like the evolt artical. For dynamic, though, it might cause trouble if you are getting a lot of info from the database or a lot of site visitors.

For dynamic, I'd use the limit syntax on the sql query to break the records up into different pages. That way all the data ain't stored in memory at once.

For example,

$num_per_page=20;
$start_record=(($page_number * $num_per_page) - $num_per_page);
$sql="select field1,field2 from table limit $start_record,$num_per_page";

Then increment $page_number each time the visitor hits "Next Page".

To make it prettier by knowing total number of pages at the start and letting someone jump into the middle, you need to do a seperate db hit at start (e.g. select count(some_field) where records=what_I_want).

Acternaweb

5:26 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



Thanks I will read the article. I believe the pages will by dynamic. As far as traffic, it is very little, just a small personal site that I am trying to use to learn more about mysql and php.

ergophobe

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

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




For dynamic, I'd use the limit syntax on the sql query

Hmm. I agree and that's what I do, but I also thought that's what the evolt article suggested and what I meant to indirectly suggest. I'm pretty sure I've seen an article that shows how to use

LIMIT offset, numrecs

In any case it's not that hard. The key thing to remember is that you want the offset to be
(page_number -1) * results_per_page

so that for page 1, the offset is 0 (for example).

lorax

6:05 pm on Jan 5, 2004 (gmt 0)

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



Personally I'd get rid of the "Prev" and "Next" and build a quick index to the first 10 pages using some math. Your query will return the total number of results and you know how many results you want per page. It's a simple jump from there to displaying the links to all of the pages (keep it reasonable though - say, something like <= 10 pages displayed at a time like WebmasterWorld does it). Each link will pass the necessary arguments to build that results page.

Acternaweb

6:40 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



I read the article and it is not what I am really asking. Unless the terminlogy is way off, the script I have doesn't use queries and wouldn't work with procedure in the article.

Is there not a relativley simple way of staying,
if book array = 10 start a new page?

lorax

7:11 pm on Jan 5, 2004 (gmt 0)

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



>> the script I have doesn't use queries

umm, it must if you're using a db...

Acternaweb

7:26 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



Guess I worded it wrong,
this is the code I want to limit the display of

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/>';
}

It is more of a show me than a ask me type, if that makes sense.

lorax

7:36 pm on Jan 5, 2004 (gmt 0)

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



What you're asking for is to manage the number of results on a page AFTER the query whereas what the article is saying - and what we're in favor of - is to control the results WITH the query (the better way). It will save time in running the query and in presenting the results to screen.

If you need help with setting the query up to do this and how to control it from page to page - that is a seperate issue but we can help you with that.

Alternately, if you really want to control the results after the query we can switch gears. ;)

Acternaweb

8:58 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



Thanks for the feedback. I am up for learning whatever I can. If your approach is better I would greatly appreciate it. As I mentioned this script is from a book. I keep mentioning this so I don't get snagged (but doesn't opensource prevent this?)

To answer your question, yes I would need assistance in setting up they queries.

lorax

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

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



>> I am up for learning whatever I can

Class time! ;)

Goal:
Build an application that queries a db, posts the first 10 results in a template page and builds a navigation system.

Step 1:
The query is the control mechanism for the results. On this premise we simply need to limit the number of results we get with the query. But we also need to know how many results there are altogether.

I'm a proponent of using the count() function so I'd approach it this way.

$num_query = "SELECT count(item_number)
FROM book_table";

counts the total number of rows

if(!isset($offset))
$offset = 0;

$page_query = "SELECT item_number, book_title, book_author, ISBN, num_pages, book_publisher, book_copyright (I'm guessing at field names)
FROM book_table
LIMIT $offset,10";

The first part learns the total number of records we need to deal with so we can build the correct number of links (based on 10 results/page) and populate the link URL with the appropriate arguments (variables) to get the correct result set.

The second part looks to see if an offset has been established (necessary for the pages that include the results after the first 10). With the offset in place, we begin the first query.

So far so good? I'm headed home in a few minutes and will pick up again later. Others may see where I'm going with this and can fill in. Also, if anyone has a better way of doing this, let's post it and discuss it. Hopefully we won't confuse Acternaweb with too many options.

Acternaweb

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

10+ Year Member



Hey Teach, thanks for the first lesson, but I am not following where this would go. Currently there is a page called output_fns.php that shows the results (and a filter that someone here helped with)

Also, do I need to have every field name present or just those I want displayed?

Thanks again

lorax

10:36 pm on Jan 6, 2004 (gmt 0)

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



We're doing a bit of offline communication and will pick this thread up once we have a handle on the particulars of the situation.

lorax

8:01 pm on Jan 10, 2004 (gmt 0)

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



Ok, I think we have the files we need to discuss this better. The files came with the book by Welling and Thomson called 'PHP and MySQL Web Development'. The example is for the Book-O-Rama store. Acternaweb has modified this to suit his needs but the idea is the same.

Caveat: I've told Acternaweb he needs to build this. We'll help him work his way through it.

The files:

show_cat.php: this file is the template file. It simply calls another file (book_sc_fns.php) into itself and runs some functions to display an output. It's job is to show all of the books within a given category.

output_fns.php: a collection of functions for outputting the HTML.

book_fns.php: is one of the files that the above file calls. It's a collection of functions for storing and retrieving book data. (It holds the queries we want to modify).

In order to get some sort of navigation in place we'll need to edit some of the existing functions.

I'm going to talk through this without actually giving you the code. See if this works - if not then I'll give you more. Here we go.

The way the code is written it queries for all of the available books for a given category and displays them all at once. We want to control the number of books displayed and we want to create a navigation so the viewer can get to the next/previous selections. I will go on with the assumption that we will set the number of books that will be displayed on the output pages.

The concept I'm after is to limit the query rather than try to control the output of the array that is created by the code.

SO, the query is actually located in the file called book_fns.php. The function which gets the books to be displayed based on category is called get_books().

Locate this and tell me what the query should look like if we want to limit the output from all books to only the first 5. Don't worry about the rest of the books just yet. Hint you'll need to get familiar with the options you can use with SELECT [mysql.com].

Acternaweb

3:17 pm on Jan 12, 2004 (gmt 0)

10+ Year Member



I am really confused, but got something like this from my new book:

if (!$catid ¦¦ $catid=='')
return false;

$conn = db_connect();
$query = "select * from books where catid='$catid'";
$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;
}

if($result > 0)
echo "<a href=\"" . $PHP_SELF . "?start=" . ($result - 10) .
"\">Previous</a><BR>\n";
if($num_books> ($result + 10))
echo "<a href=\"" . $PHP_SELF . "?start=" . ($result + 10) .
"\">Next</a><BR>\n";

lorax

3:57 pm on Jan 12, 2004 (gmt 0)

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



Re: confused.

Don't worry - we're going to go slow.

Re: your post. You're in the right neighborhood but skip all the rest of what you posted and focus on just the query. I'm not sure where found the function db_result_to_array() but I suspect it was created in the example. Let's ignore this for now and stick to the basics. You posted:

$query = "select * from books where catid='$catid'";

The query above will give ALL records that match the criteria of catid = '$catid'. We want to limit this. What do we need to change this query to in order to limit the result set to no more than 5?

Acternaweb

4:25 pm on Jan 12, 2004 (gmt 0)

10+ Year Member



Am I totally way off, or just dumb, lol

$query = "select * from books where catid='$catid'" limit 5;

or
$limit=5

$query = "select * from books where catid='$catid'" $limit;

As always, thanks

lorax

4:33 pm on Jan 12, 2004 (gmt 0)

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



Bingo!

Now, take a close look at the 'limit' option. What other arguments can we pass to it?

Acternaweb

5:26 pm on Jan 12, 2004 (gmt 0)

10+ Year Member



Not sure I am following. It seems like there needs to be logic for what to do with the other "results" beyond 5 but I haven't read anything like that.

lorax

5:35 pm on Jan 12, 2004 (gmt 0)

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



Yes there does but we need a way to control the query first. The Limit option allows us to not only limit the number of results returned but also allows us to set an offset. The offset allows us to specify where we result set to start. By default the query will start at the first record (pos 0) and then will count off 5. If we want to get the next 5 records we'll need to set the offset to pos 5 (the first query returns the first 5 records or records 0-4). The offset is the key to making this work. So with your query of:

$query = "select * from books where catid='$catid'" limit 5;

we change the limit to

limit $offset,5

Where $offset is a variable that we'll control from the results page. Make sense?

Acternaweb

7:43 pm on Jan 12, 2004 (gmt 0)

10+ Year Member



Hi,

I was thinking about that, but I must not understand it. I thought it would just look at those rows for specific data. FOr example, search the first 5 records for name=joe smith.

So to bring it to my level the limit and offset breaks the results into seperate "pages?"

How do you know many records there will be. I read where you can code it till the end,but how many increments of 5 do I need if I don't know how many records I will have?

lorax

7:52 pm on Jan 12, 2004 (gmt 0)

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



I was thinking about that, but I must not understand it. I thought it would just look at those rows for specific data. FOr example, search the first 5 records for name=joe smith.

Nope. It basically says search for ALL records where the condition is true (name='joe smith') but only return 5 starting at record number X (the offset).

How do you know many records there will be. I read where you can code it till the end,but how many increments of 5 do I need if I don't know how many records I will have?

Don't worry about this just yet.

Try this. Create a stand alone page that has the same query and write the code that will return the results to the page. Then play with the LIMIT option so you can see what it does. Don't worry about passing variables to the query just yet - hard-code them in (instead of 'limit $offset,5' use 'limit 0,5')

Acternaweb

9:54 pm on Jan 12, 2004 (gmt 0)

10+ Year Member



haha Cool, it worked. I have one "category" that has 5 records in it. I was able to configure it to select the first 2 and the last set.

I tried 'limit 3,2' and got the 3rd and 4th record.

What if I didn't know how many records there were?

ergophobe

10:26 pm on Jan 12, 2004 (gmt 0)

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



I'm not sure I dare to butt in on lorax's intricately designed and ultimately quite entertaining Socratic tutorial...

There are a couple ways that you could find out how many records there are. Both involve reissuing the query, but without the limit. To go back a few posts:


$query = "select * from books where catid='$catid'";

The query above will give ALL records that match the criteria of catid = '$catid'

So that's what you need - a query that gives you a count of ALL the records. The easy way is:

$query = "select count(*) from books where catid='$catid'";

This query will not return all records where catid=1, but it will return a *count* of all records meeting that criterion. From there it is simplicity itself to find out how many pages there are.

$page_count = $record_count/$offset;

But, you ask, what if there are 13 records and the offset is 5? Won't that give me 2.6 pages? Yes, so you need to round up.

$page_count = ceil($record_count/$offset);

There's another way I use for the case where a multi-page result set will be rare... but I'm afraid lorax will make me go sit in the corner if I talk about that without raising my hand ;-)

Tom

lorax

3:46 am on Jan 13, 2004 (gmt 0)

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



I'm not sure I dare to butt in on lorax's intricately designed and ultimately quite entertaining Socratic tutorial..

;) I don't mind at all.

Next piece. So now you've edited the query and are feeling pretty good. So let's put the edits back in context of the function. The function this query is a part of is 'get_books($catid)'

get_books is the function and $catid is an argument passed to the function for it to use. So if we're going to control the offset for the query then we need to add another variable/argument to the function. So we'll change it to:

get_books($cat_id, $offset)

and change the 'LIMIT' option to be 'LIMIT $offset,5. We're going to leave the number of results hard-coded for now. Once you see how easy this is you'll be able to make that selectable too.

Now, this function is called by show_cat.php. In show_cat.php you'll see a line of code that reads:

$book_array = get_books($catid);

I think you know what we need to do here. We need to pass the offset so that line should read:

$book_array = get_books($catid,$offset);

Now here's the tough part. How do we set the offset? Somewhere beneath the code that calls this function is the code that outputs the results. Locate the line that reads:

display_books($book_array);

Now locate that function - it's in the file called output_fns.php. Look over the code for that function carefully and then look over the HTML output (I assume you still have a working copy of the store to play with). I think you'll see how it works.

Put some HTML comments within the function's code and see where they show up in the source. See if you can get something to echo on screen above the <hr> that seperates the last book entry from the buttons at the bottom of the page.

SFSG?

Acternaweb

1:20 pm on Jan 13, 2004 (gmt 0)

10+ Year Member



I was good up to this point:

Locate the line that reads:display_books($book_array);

I found the code, but what has to ben done?

(I assume you still have a working copy of the store to play with). - Nope there are no html pages, the html is in the output_fns.php page.

Put some HTML comments within the function's code and see where they show up in the source... OK I will try it.

Thanks again

This 69 message thread spans 3 pages: 69