Problem with query strings on urls

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Problem with query strings on urls

Post by Noobie »

Probably completely the wrong subject - not sure what to call the problem.

I'm trying to create a dynamic page navigation system within a portfolio. It should count the number of pages within a particular job (rows linked via a join) and produce a next/previous arrangement. This isn't pagination as such as I needed more control over what appeared on which page.

It's nearly working except that it can't seem to count!! Say there are a total of 3 pages - it's only counting up to 2 and I'm not sure why. Here's the code:

Code: Select all

$num_rows = mysql_num_rows($result);
   if ($page_number == 1){
      }
 
 if ($page_number != $num_rows){
      $next = $page_number + 1;
   }else{
      $next = $page_number;
   }
 
   if ($page_number != 1){
      $prev = $page_number - 1;
   }else{
      $prev = 1;
   }
 
   echo '<a href="'. $_SERVER['PHP_SELF'] . '?id=' . $id;
   echo '&page_number=' . $prev . '">prev</a>&nbsp;';
   echo $page_number.  '/' . $num_rows ;
   echo '&nbsp;<a href="'. $_SERVER['PHP_SELF'] . '?id=' . $id;
   echo '&page_number=' . $next . '">next</a>';
Say we've got 3 pages. The above is correctly calculating that and producing something that looks like this:

prev 1/3 next

If you click "next" you go to page 2. But you can't then get to page 3. You can click on prev and go back to page 1.

Any suggestions about what I'm missing to force it to count all of the rows?
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Problem with query strings on urls

Post by chopsmith »

First, you can get rid of this:

Code: Select all

 
    if ($page_number == 1){
       }
 
What you've got after that should properly assign the $prev and $next variables, though. Are you sure this is exactly how you've got it written? Also, when you're on page 1, do you really want a "prev" link there, and do you really want a "next" link when you're on page 3?
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Re: Problem with query strings on urls

Post by Noobie »

Hi Chopsmith

Here's the whole thing (promise not to laugh - I really am a newbie!)

Code: Select all

 // get the id from the URL request
    $portfolio_id = $_REQUEST['id'];
 
 // retrieve the rows from both page and list tables
    $query = "SELECT * from portfolio_list NATURAL JOIN portfolio_page WHERE portfolio_id='$portfolio_id'  ";
 
    $result = mysql_query( $query );
 
       if( $result && $pagination = mysql_fetch_object( $result ) )
    {
     
      $id = $pagination -> id;
      $portfolio_id = $pagination -> portfolio_id;
      $page_number = $pagination -> page_number;
      $portfolio_name = $pagination -> portfolio_name;
 
}
 
$num_rows = mysql_num_rows($result);
 
 if ($page_number != $num_rows){
      $next = $page_number + 1;
   }else{
      $next = $page_number;
   }
 
   if ($page_number != 1){
      $prev = $page_number - 1;
   }else{
      $prev = 1;
   }
 
   echo '<a href="'. $_SERVER['PHP_SELF'] . '?id=' . $id;
   echo '&page_number=' . $prev . '">prev</a>&nbsp;';
   echo $page_number.  '/' . $num_rows ;
   echo '&nbsp;<a href="'. $_SERVER['PHP_SELF'] . '?id=' . $id;
   echo '&page_number=' . $next . '">next</a>';
I removed the bit you mentioned but still no joy.

What I was hoping to do once I got the basics worked out is use images (arrows) instead of next and prev and have a paler unlinked version for page 1's prev and the same for the final page's next - if that makes sense. I just hoped to get the basic mechanism worked out first.

BTW - I also don't really like the "*" and generally replace it with specific names once it's working.
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Problem with query strings on urls

Post by chopsmith »

I'd never laugh. We all have to learn sometime.

This doesn't work (I've actually never seen a NATURAL JOIN before, and I'm assuming you don't need to tell it which fields to join on):

Code: Select all

 
$query = "SELECT * from portfolio_list NATURAL JOIN portfolio_page WHERE portfolio_id='$portfolio_id'  ";
 
If you run it, you'll be looking for rows whose portfolio_id is literally "$portfolio_id", rather than the value of the $portfolio_id variable. You can change it to:

Code: Select all

 
$query = "SELECT * from portfolio_list NATURAL JOIN portfolio_page WHERE portfolio_id='{$portfolio_id}'"; //note the {}
 
Or:

Code: Select all

 
$query = "SELECT * from portfolio_list NATURAL JOIN portfolio_page WHERE portfolio_id='" . $portfolio_id . "'";
 
The following is a bit unconventional in that it has whitespace between your object, the -> operator, and the object's member variables.

Code: Select all

 
$id = $pagination -> id;
$portfolio_id = $pagination -> portfolio_id;
$page_number = $pagination -> page_number;
$portfolio_name = $pagination -> portfolio_name;
 
I thought that would be considered a syntax error, but I guess not. Anyway, I'd change it to:

Code: Select all

 
$id = $pagination->id;
$portfolio_id = $pagination->portfolio_id;
$page_number = $pagination->page_number;
$portfolio_name = $pagination->portfolio_name;
 
Also, this is a bit weird:

Code: Select all

 
if( $result && $pagination = mysql_fetch_object( $result ) ) {
  $id = $pagination -> id;
  $portfolio_id = $pagination -> portfolio_id;
  $page_number = $pagination -> page_number;
  $portfolio_name = $pagination -> portfolio_name;
}
 
Because you're not doing anything in case the result is FALSE, and you are programatically adding the id to the url, i'd get rid of that condition. I'd also change the "if" statement into a "while" statement:

Code: Select all

 
  while ($pagination = mysql_fetch_object($result)) {
    $id = $pagination->id;
    $portfolio_id = $pagination->portfolio_id;
    $page_number = $pagination->page_number;
    $portfolio_name = $pagination->portfolio_name;
  }
 
Try making those changes, run it and let me know what happens.
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Problem with query strings on urls

Post by chopsmith »

Also, can you show me the structure of the database tables you're joining. I'm curious as to why you're getting the page_number from a database.
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Re: Problem with query strings on urls

Post by Noobie »

Hi Chopsmith

Thank you so much for helping with this.

This is for a client's portfolio page - it's split into jobs and each job is split into pages with each page having photos and text.

Normal pagination didn't seem appropriate because the client wanted to have exact control over what appeared on which page for each job and the quantity of content would change for each page.

In the end I thought the best way around it would be to have a table for each job (portfolio_list) which has the structure:

portfolio_id, portfolio_name, job_descrip

Then I added another table for the various pages belonging to each job (portfolio_page) which has the structure:

id, portfolio_id, page_text, page_number

The list of jobs on the main portfolio page sends the portfolio_id (from the list table) to the job pages which then has a join to get the contents from the page table.

The client has specified a little menu in each job which would look like < 1/6 > with the arrows being the links up and down. On page 1 the left arrow has no link and on page 6 (in this case) the right arrow has no link.

I needed a row for page number in the table so that the client can put certain content on each page. Does that make any sense at all?

I'm not very experienced with PHP - usually do simpler stuff taking things from the same table so hadn't had much experience with joins - I got that "Natural Join" off a tutorial somewhere (looked at about 300 over the last week so not sure which one!!).
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Problem with query strings on urls

Post by chopsmith »

Can you post what the code looks like at this point?
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Problem with query strings on urls

Post by chopsmith »

If I were you, I'd use what I show below. I'm just trying to help you quickly rather than keep going back and forth. Take a look at the code and notice/learn the differences. I think your problem was more with design than with coding. That's always what takes more practice.

Code: Select all

 
//get portfolio_id and page_number to be viewed
$portfolio_id = (isset($_GET['portfolio_id'])) ? $_GET['portfolio_id'] : 1;
$page_number = (isset($_GET['page_number'])) ? $_GET['page_number'] : 1;
 
//get the number of pages for this portfolio_id
$sql_numPages = "SELECT * from portfolio_list INNER JOIN portfolio_page on portfolio_list.portfolio_id = portfolio_page.portfolio_id WHERE portfolio_page.portfolio_id='{$portfolio_id}'";
$result_numPages = mysql_query($sql_numPages);
$num_pages = mysql_num_rows($result_numPages);
 
//get the contents of this page
$sql_pageContent = "SELECT * from portfolio_list INNER JOIN portfolio_page on portfolio_list.portfolio_id = portfolio_page.portfolio_id WHERE portfolio_page.portfolio_id='{$portfolio_id}' and portfolio_page.page_number='{$page_number}'";
$result_pageContent = mysql_query($sql_pageContent);
while ($pageContent = mysql_fetch_object($result_pageContent)) {
    $portfolio_name = $pageContent->portfolio_name;
    $job_description = $pageContent->job_descrip;
    $page_text = $pageContent->page_text;
}
 
$next = ($page_number != $num_pages) ? $page_number + 1 : $page_number;
$prev = ($page_number != 1) ? $page_number - 1 : $page_number;
 
$content = $portfolio_name . " - " . $job_description . "<br />" . $page_text;
$previous_link = "<a href='{$_SERVER['PHP-SELF']}?id={$portfolio_id}&page_number={$prev}'>Previous</a>";
$next_link = "<a href='{$_SERVER['PHP-SELF']}?id={$portfolio_id}&page_number={$next}'>Next</a><br />";
$current_state = $page_number . '/' . $num_pages;
echo $content . "<br /><br />" . $previous_link . " " . $current_state . " " . $next_link;
 
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Re: Problem with query strings on urls

Post by Noobie »

Thanks again Chopsmith - I'll give this a run through as soon as I can and report back!
Post Reply