Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question paging through results

    ok so i found a tutorial to page through results in PHP here: http://www.php-mysql-tutorial.com/php-mysql-paging.php

    I used the code provided from the link at the bottom of the tutorial:
    Code:
    <?php
    
    // how many rows to show per page
    $rowsPerPage = 20;
    
    // by default we show first page
    $pageNum = 1;
    
    // if $_GET['page'] defined, use it as page number
    if(isset($_GET['page']))
    {
    	$pageNum = $_GET['page'];
    }
    
    // counting the offset
    $offset = ($pageNum - 1) * $rowsPerPage;
    
    $query  = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";
    $result = mysql_query($query) or die('Error, query failed');
    
    // print the random numbers
    while(list($val) = mysql_fetch_array($result))
    {
    	echo "$val <br>";
    }
    
    echo '<br>';
    
    // how many rows we have in database
    $query   = "SELECT COUNT(val) AS numrows FROM randoms";
    $result  = mysql_query($query) or die('Error, query failed');
    $row     = mysql_fetch_array($result, MYSQL_ASSOC);
    $numrows = $row['numrows'];
    
    // how many pages we have when using paging?
    $maxPage = ceil($numrows/$rowsPerPage);
    
    $self = $_SERVER['PHP_SELF'];
    
    // creating 'previous' and 'next' link
    // plus 'first page' and 'last page' link
    
    // print 'previous' link only if we're not
    // on page one
    if ($pageNum > 1)
    {
    	$page = $pageNum - 1;
    	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
    	
    	$first = " <a href=\"$self?page=1\">[First Page]</a> ";
    } 
    else
    {
    	$prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
    	$first = ' [First Page] '; // nor 'first page' link
    }
    
    // print 'next' link only if we're not
    // on the last page
    if ($pageNum < $maxPage)
    {
    	$page = $pageNum + 1;
    	$next = " <a href=\"$self?page=$page\">[Next]</a> ";
    	
    	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    } 
    else
    {
    	$next = ' [Next] ';      // we're on the last page, don't enable 'next' link
    	$last = ' [Last Page] '; // nor 'last page' link
    }
    
    // print the page navigation link
    echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
    ?>
    when i tried to use it in my page i got the error: Error, query failed

    Code:
    <?php
    include_once("config.php");
    include_once("functions.php");
    // Check user logged in already:
    // checkLoggedIn("yes");
    doCSS(); ?>
    <script type="text/javascript" src="js/jva.js"></script>
    <div id="header">
    <p class="wrap">
    <? print("\n <a href=\"post.php"."\"><img src=\"images/picture_add.png"."\" border=\"0"."\" /></a> ");?>
    </p>
    <h1 class="wrap">Avert Weekly Headers</h1>
    </div>
    <form name="form1" id="form1">
        <select name="menu1" onchange="MM_jumpMenu('parent',this,0)">
          <option value="#top">2008</option>
          <option value="#1">February 8th</option>
          <option value="#2">February 15th</option>
    	  <option value="#3">February 22nd</option>
    	  <option value="#4">February 29th</option>
    	  <option value="#5">March 7th</option>
    	  <option value="#6">March 14th</option>
    	  <option value="#7">March 21st</option>
    	  <option value="#8">March 28th</option>
    	  <option value="#9">April 4th</option>
    	  <option value="#10">April 11th</option>
    	  <option value="#11">April 18th</option>
    	  <option value="#12">April 25th</option>
    	  <option value="#13">May 2nd</option>
    	  <option value="#14">May 9th</option>
    	  <option value="#15">May 16th</option>
    	  <option value="#16">May 23rd</option>
    	  <option value="#17">May 30th</option>
    	  <option value="#18">June 6th</option>
    	  <option value="#19">June 13th</option>
    	  <option value="#20">June 20th</option>
    	  <option value="#21">June 27th</option>
    	  <option value="#22">July 4th</option>
        </select>
      </form>
    <?
    //SQL to get keyword count
    $result4 = mysql_query("SELECT *, COUNT(keyword) As keywordCount FROM keyword INNER JOIN keywordlist ON keyword.keyword_id=keywordlist.keyword_id GROUP BY keyword ORDER BY keyword ASC");
    
    echo '<form name="form3" method="post" action="">';
    echo '<h2><select name="menu2" onChange="MM_jumpMenu(\'parent\',this,0)">';
    echo '<option value="">Keyword usage';
    echo '</option>';  
    while ($row3 = mysql_fetch_array($result4))
    {
    $KeywordsInUse = $row3['keyword'];
    $KeywordCount = $row3['keywordCount'];
    $keyword_id = $row3['keyword_id'];
    
    echo '<option value="">'.$KeywordsInUse.' (x'.$KeywordCount.')';
    echo '</option>';
    }
    echo '</select></h2>'; 
    echo '</form>'; 
    
    //SQL to get country count
    $result5 = mysql_query("SELECT *, COUNT(country) As countryCount FROM headertbl LEFT JOIN country ON headertbl.countryID=country.countryID GROUP BY country ORDER BY country ASC");
    
    
    echo '<form name="form3" method="post" action="">';
    echo '<select name="menu2" onChange="MM_jumpMenu(\'parent\',this,0)">';
    echo '<option value="">Search country';
    echo '</option>';  
    while ($row4 = mysql_fetch_array($result5))
    
    {
    $countriesInUse = $row4['country'];
    $countryCount = $row4['countryCount'];
    $CountryID = $row4['CountryID'];
    
    
    
    echo '<option value="display1.php?CountryID='.$CountryID.'">'.$countriesInUse.' (x'.$countryCount.') ';
    echo '</option>';
    }
    echo '</select> <a href="display.php">All Headers</a>'; 
    echo '</form>'; 
    
    
    
    
    
    // how many rows to show per page
    $rowsPerPage = 20;
    
    // by default we show first page
    $pageNum = 1;
    
    // if $_GET['page'] defined, use it as page number
    if(isset($_GET['page']))
    {
    	$pageNum = $_GET['page'];
    }
    
    // counting the offset
    $offset = ($pageNum - 1) * $rowsPerPage;
    
    $query  = "SELECT val FROM Headertbl LIMIT $offset, $rowsPerPage ORDER BY HeaderDate DESC";
    $result = mysql_query($query) or die('Error, query failed');
    
    // print the random numbers
    while(list($val) = mysql_fetch_array($result))
    {
    	echo "$val <br>";
    }
    
    echo '<br>';
    
    // how many rows we have in database
    $query   = "SELECT COUNT(val) AS numrows FROM Headertbl";
    $result  = mysql_query($query) or die('Error, query failed');
    $row     = mysql_fetch_array($result, MYSQL_ASSOC);
    $numrows = $row['numrows'];
    
    // how many pages we have when using paging?
    $maxPage = ceil($numrows/$rowsPerPage);
    
    $self = $_SERVER['PHP_SELF'];
    
    // creating 'previous' and 'next' link
    // plus 'first page' and 'last page' link
    
    // print 'previous' link only if we're not
    // on page one
    if ($pageNum > 1)
    {
    	$page = $pageNum - 1;
    	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
    	
    	$first = " <a href=\"$self?page=1\">[First Page]</a> ";
    } 
    else
    {
    	$prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
    	$first = ' [First Page] '; // nor 'first page' link
    }
    
    // print 'next' link only if we're not
    // on the last page
    if ($pageNum < $maxPage)
    {
    	$page = $pageNum + 1;
    	$next = " <a href=\"$self?page=$page\">[Next]</a> ";
    	
    	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    } 
    else
    {
    	$next = ' [Next] ';      // we're on the last page, don't enable 'next' link
    	$last = ' [Last Page] '; // nor 'last page' link
    }
    
    // print the page navigation link
    echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
    
    
    
    
    
    
    
    
    // start main page
    
    
    while($row = mysql_fetch_array($result)){
    echo '<div id="entry" class="wrap">';
    $HeaderID = $row['HeaderID'];
    $HeaderImage = $row['HeaderImage'];
    $date = new DateTime($row['HeaderDate']);
    $HeaderDate = $date->format('F j, Y'); 
    $HeaderPhotoNo = $row['HeaderPhotoNo'];
    $HeaderName = $row['HeaderName'];
    $PhotoID = $row['PhotoID'];
    $CountryID = $row['CountryID'];
    
    
    //SQL to get countries
    $result1 = mysql_fetch_array(mysql_query("SELECT * FROM country WHERE CountryID =".$CountryID));
    $Country = $result1['country'];
    
    //SQL for Keywords
    $result2 = mysql_query("SELECT * FROM keyword INNER JOIN keywordlist ON keyword.keyword_id=keywordlist.keyword_id WHERE keyword.HeaderID=".$HeaderID);
    
    //date selection
    //$result3 = mysql_query("SELECT HeaderDate FROM Headertbl");
    //echo '<h2><select name="$HeaderDate">';
    //while($row2 = mysql_fetch_array($result3))
    //      {
    //      $HeaderDateSelect = $row2['HeaderDate'];
    //echo '<option value="'.$HeaderDate.'" >'.$HeaderDateSelect.'</option>';
    //}
    //echo '</select></h2>';
    
    
    //div container of header and information
    echo <<<EOF
      <a title="$HeaderID" id="$HeaderID" name="$HeaderID"></a>
      <div class="entry_header">
        <b>Header Name:</b> $HeaderName <a href="edit.php?HeaderID=$HeaderID"><img src="images/picture_edit.png" border="0" /></a> <a href="upload.php?HeaderID=$HeaderID"><img src="images/picture_right.png" border="0" /></a><br />
          <b>Country:</b> <a href="http://www.avert.org/photo_search.php?search_keyword_id=&search_country_id=country-$CountryID&page_type=thumbnails&search=search" target="_blank">$Country</a> <b>Keywords:</b> 
    EOF;
    //loop through Keywords 
          $i = 0;
    while ($row1 = mysql_fetch_array($result2))
    {
        if ($i++ > 0)
        {
            echo ', ';
        }
       $Keyword = $row1['keyword'];
    	  $KeywordID = $row1['keyword_id'];
           echo '<a href="http://www.avert.org/photo_search.php?search_keyword_id='.$KeywordID.'&search_country_id=&page_type=thumbnails&search=search" target="_blank" >'.$Keyword.'</a>';
    	   echo ' <a href="editkeywords.php?HeaderID='.$HeaderID.'">[Edit]</a>';
    }  
    	  
          
    echo '<br />';
    echo '<b>PhotoID:</b> '. $PhotoID .'<br />';
    echo '<b>Header Photo No:</b> '. $HeaderPhotoNo .'<br />';
    echo '<b>Header Date:</b> '.$HeaderDate.'<br />';
    echo '<img src="headers/'.$HeaderImage.'">';
    echo '</div>';
    echo '</div>';
    
    }
    mysql_close($link);
    
    ?>

  • #2
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    if you wanted to figure out which query was broken you could change the die comment.

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Its likely here.
    PHP Code:
    $result mysql_query($query) or die('Error, query failed'); 
    Lets change that to something useful shall we?
    PHP Code:
    $result mysql_query($query) or die(mysql_error().'<br>SQL: ' $query); 
    Post your error here.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    jarv (07-14-2008)

  • #4
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    sorted thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •