Having some trouble in my pagation code(PHP)

Posted by user300371 on Stack Overflow See other posts from Stack Overflow or by user300371
Published on 2010-03-28T20:07:35Z Indexed on 2010/03/28 20:13 UTC
Read the original article Hit count: 214

Filed under:

My table is posting and in that table, it shows all the posting people put in. I am trying to make page navigation links at the bottom of the posts. the function generateenter code here_page_links does all the work.

The code seem to be only showing the "<-" and "->" and not the link numbers. I think the function is only reading the $_GET['posting'] ==1.

(this code is from the Oreilly PHP/MySQL book. I am using it as practice)

function generate_page_links($cur_page, $num_pages) {
    $page_links = '';

    // If this page is not the first page, generate the "previous" link
    if ($cur_page > 1) { //cur_page is just a number that is gotten from the url.
      $page_links .= '<a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($cur_page - 1) . '"><--</a> ';
    }
    else {
      $page_links .= '<- ';
    }

    // Loop through the pages generating the page number links
    for ($i = 1; $i <= $num_pages; $i++) {
      if ($cur_page == $i) {
        $page_links .= ' ' . $i;
      }
      else {
         $page_links .= ' <a href="' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search . '&sort=' . $sort . '&page=' . $i . '"> ' . $i . '</a>';
      }
    }

    // If this page is not the last page, generate the "next" link
    if ($cur_page < $num_pages) {
      $page_links .= ' <a href="' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search . '&sort=' . $sort . '&page=' . ($cur_page + 1) . '">-></a>';
    }
    if ($cur_page == $num_pages){ //the last page
      $page_links .= ' ->';
    }

    return $page_links; //need to return this variable in the function
  }

  // Calculate pagination information
  $cur_page = isset($_GET['page']) ? $_GET['page'] : 1;
  $results_per_page = 3;  // number of results per page
  $skip = (($cur_page - 1) * $results_per_page);


  $query = "SELECT * FROM posting ORDER BY date_added DESC";
  $data = mysqli_query($dbc, $query);
  $total = mysqli_num_rows($data);
  $num_pages = ceil($total / $results_per_page);

  //Query again to get just the subset of results
  $query =  $query . " LIMIT $skip, $results_per_page";
  $result = mysqli_query($dbc, $query);
  echo '<table>';
    echo '<tr><td><b>Title</b></td><td><b>Date Posted</b></td></tr>';
  while ($row = mysqli_fetch_array($result)) {
      echo '<tr><td><a href="ad.php? 

    posting_id='.$row['posting_id'].'

    ">'.$row['title'].'</a></td>';
    echo '<td>'.$row['date_added'].'</td>';
    //echo '<td>'.$row['name'].'</td></tr>';
  }
 echo '</table>';

  // Generate navigational page links if we have more than one page
  if ($num_pages > 1) {
    echo generate_page_links($user_search, $sort, $cur_page, $num_pages);
  }

© Stack Overflow or respective owner

Related posts about php