-2

I am trying to add pagination to my search result, in doing so I have encountered the following error at the following line:

Notice: Undefined variable: limit in C:\xampp\htdocs\t69\functions\functions.php on line 262

Below is line 262:

$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");

Essentially I have a search results that should display only 10 items, if there is more than 10 item, it should show additional pages to navigate to (like page 1 - 5).

My problem is as follow The limit does not seem to apply well to the items being displayed, well more than 10 items are displayed, and I do not want to force to only display 10 such as LIMIT 10, because if there is more than 10 than only 10 would be displayed when I want that if theres more than 10 only 10 would be shown on that page with pagination shown at the bottom.

Below is portion of the code:

$outputList = '';

    if(isset($_GET['provider'])){
        $provider_id = $_GET['provider'];

    global $con;
$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");
$nr = mysql_num_rows($get_crs); // Get total of Num rows from the database query
if (isset($_GET['pg'])) { // Get pn from URL vars if it is present
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pg']); 
    //$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
} else { // If the pn URL variable is not present force it to be value of page number 1
    $pn = 1;
} 
//This is where we set how many database items to show on each page 
$itemsPerPage = 10; 
// Get the value of the last page in the pagination result set
$lastPage = ceil($nr / $itemsPerPage);
// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
if ($pn < 1) { // If it is less than 1
    $pn = 1; // force if to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
    $pn = $lastPage; // force it to be $lastpage's value
} 
// This creates the numbers to click in between the next and back buttons
// This section is explained well in the video that accompanies this script
$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 
// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
//////////////////////////////// END Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
    // This shows the user what page they are on, and the total number of pages
    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';
    // If we are not on page 1 we can place the Back button
    if ($pn != 1) {
        $previous = $pn - 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
    } 
    // Lay in the clickable numbers display here between the Back and Next links
    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
    // If we are not on the very last page we can place the Next button
    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
    } 
}



    while($row_crs = mysql_fetch_array($get_crs)){ 

    $crs_id = $row_crs['course_id'];
        $crs_cat = $row_crs['course_cat'];

    $crs_provider = $row_crs['course_provider'];

    $crs_title = $row_crs['course_title'];
        $crs_price = $row_crs['course_price'];
          $crs_city= $row_crs['course_city'];

          $crs_date= $row_crs['course_date1'];


$crs_sdesc= $row_crs['course_sdesc'];

        $crs_image = $row_crs['course_image'];


    $outputList .=  "

           <article class='search-result row'><center>
      <div class='col-xs-12 col-sm-12 col-md-3' id='thumbnailContainer'>
        <a href='#' title='Lorem ipsum' class='thumbnail' id='resultThumbnail'><img src='content/logo_ontrack.png' /></a>
      <button id='resultprice'><i class='fa fa-usd fa-2x'><span id='resultpriceText'> $crs_price</span></i></button>
      </div>
      <div class='col-xs-12 col-sm-12 col-md-2'>
        <ul class='meta-search' id='listDesign'>
          <li><button id='resultInfo'><i class='fa fa-calendar fa-1x'><span id='iconText'>  $crs_date</span></i></button></li>
           <li><button id='resultInfo'><i class='fa fa fa-tags fa-1x'><span id='iconText'> <b> Web Development</b></span></i></button></li>
<li><button id='resultInfo'><i class='fa fa-graduation-cap fa-1x'><span id='iconText'> <b> HOTT</b></span></i></button></li>
<li><button id='resultInfo'><i class='fa fa-map-marker fa-1x'><span id='iconText'> <b> $crs_city</b></span></i></button></li>
        </ul>
      </div></center>
      <div class='col-xs-12 col-sm-12 col-md-7 excerpet'>
        <h3 id='resultHeading'><a href='#' id='headingLinking'><b>$crs_title</b></a></h3>

        <p id='courseshortDescription'>
$crs_sdesc
        </p>  

        <center><button class='btn btn-danger' id='findoutBtn'><a href='details.php?crs_id=$crs_id' style='color:white;'>Find Out More</a></button></center>     


      <span class='clearfix borda'></span>
    </article>




        ";


print "$outputList"; 

echo $paginationDisplay;


    }

}

Any help would be greatly appreciated. If you need any clarification, let me know.

jon e
  • 31
  • 1
  • 7
  • its a different question – jon e Jan 15 '15 at 01:01
  • No, it's not. Your error is because the variable `$limit` is unset; the question I linked to has lots of information on why that is happening and how you can fix it – andrewsi Jan 15 '15 at 01:06

3 Answers3

1
$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");
...
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

How do you expect to use $limit? It has to be defined before you use it.

For future, look more closely at the errors you get:

Notice: Undefined variable: limit in C:\xampp\htdocs\t69\functions\functions.php on line 262

Undefined variable: limit

Undefined variable: limit

Undefined variable: limit

Undefined variable: limit

on line 262

Shahar
  • 1,687
  • 2
  • 12
  • 18
1

The error actually tells you the problem

Notice: Undefined variable: limit in C:\xampp\htdocs\t69\functions\functions.php on line 262

The limit needs to be declared before it is used.

Default your search criteria or switch these lines around.

$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 
Demodave
  • 6,242
  • 6
  • 43
  • 58
0

You need to replace:

$get_crs = mysql_query("select * from books where book_provider='$provider_id' $limit");

with:

$get_crs = mysql_query("SELECT * FROM `books` WHERE `book_provider`='$provider_id' LIMIT $limit");

I uppercased your SQL to make it more clear what is what in your formulated query (I also escaped database tables and column names. These are just best practices. Sorry I forgot to mention you need to actually define the limit as well.

Sensei
  • 124
  • 5