Jump to content

Paginate Problem


Dada78

Recommended Posts

I have a Paginate problem. I am trying to apply what I learned on my last page I used this on. This other page I am trying to add this to displays comments. But I am getting errors and don't know what they are.

 

Here is the errors I am getting.

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mesquit1/public_html/local/utiles.php on line 109

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mesquit1/public_html/local/utiles.php on line 115

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mesquit1/public_html/local/utiles.php on line 120

Nothing to Display!

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mesquit1/public_html/local/display.php on line 62

 

 

Here is the code that displays the the comments that I have added the code to.

 

This code starts on line 37

 

// start code for displaying comments

function show_data(){
   $id = array();
   $firstname = array();
   $location = array();
   $comment_date = array();
   $comment = array();
   $id = mysql_real_escape_string($_GET['id']);
   $i=0;
   if(isset($id)) {

$limit          = 5;

{

      $sql = "SELECT * FROM comments WHERE id = $id;";

      $site['url'] = 'http://mesquitechristmas.com/local/display.php?id=$id';
      
      $PageNav = swPagination( $Sql, $MRPP = $limit, $UriAdd = $site['url'], $SortByAllow = FALSE, $SortOrderSeed = FALSE );
      $result = $PageNav['RowList']; #Don't think you need the stuff above

      IF ( !$PageNav['TotalRes'] )  echo("Nothing to Display!");
        $i = 0;
         while ($row = mysql_fetch_array ($result)) {
            $color = ($i%2== 0 || $i == 0)?'#DDDDDD':'#EEEEEE';
            $id[$i] = $row["id"];
            $firstname[$i] = $row["firstname"];
            $location[$i] = $row["location"];
            $comment_date[$i] = $row["comment_date"];
            $comment[$i] = $row["comment"];


print"<tr bgcolor='".$color."'>
    <td align='left' width='120'><strong> Name</strong>: $firstname[$i]</td>
    <td align='left' width='120'><strong> Location</strong>: $location[$i]</td>
    <td align='left' width='216'><strong> Date</strong>: $comment_date[$i]</td>
  </tr>
  <tr bgcolor='".$color."'>
    <td class='commentBorder' colspan='4'><strong> Comment</strong>: $comment[$i]</td>
  </tr>";
      ++$i;
         }
      }
   }
    ECHO $PageNav['HrefNav'];
}

// end code for displaying comments 

 

 

 

Here is the code for the included utiles.php that is at the top of the page

 

 

<?php
    /**
     * swPaginate( $Sql, $MRPP, $UriAdd, $SortByAllow = FALSE, $SortOrderSeed = FALSE )
     *
     * Short description:
     * Returns an Array of all required paginate values and links
     * This contains all the values and queries to produce
     * paginated links from queries.
     *
     * @by      rr1024
     * @access  public
     * @type    Active  - There are 2 SQL Queries 1 query is passed passively
     * @const   $PageNumDis - +/- Numbers displayed to user 2 will display <First><Prev> 1 2 3 4 <Next><Last>
     * @param   $Sql    -  Primary data row query for display
     * @param   1.1 MOD Not Required X $SqlCnt -  Count all the rows in $Sql
     * @param   $MRPP   -  Maximum Rows Per Page - Defines the total I.E. Results per page
     *             number of results to be displayed per page from $Sql.
     * @param   $UriAdd -  Your pages URI components, it must have URL and URI end in & we will append to this
     * @param   $SortByAllow - Array table list added to query to allow sorting by fields.
     * @param   $SortOrderSeed - String Default is ASC - Allowable is ASC, DESC and RAND but RAND does not function yet.
     * @return  $Paginate Array (
     *             ['RowList'] => You db results list for displaying the data on a page.
     *             ['FromToTotal'] => [OPTIONAL] Display result From n to nn and total results from the ['RowList']
     *             ['PagesNav'] => [OPTIONAL] Display "Pages:"
     *             ['HrefNav'] => html href link to all pages others are provided but not listed
     *                          )
     * @since   1.1
     * @update  01.09.2007
     * @usage   $Sql = "SELECT * FROM `Blog` WHERE `New` = '1' ORDER BY `Date`";
     *          $Uri = $site['url'].'blog_search.php?blog=newblogs&';
     *          $PageNav = swPagination( $Sql, 30, $URI, FALSE, $Order );
     *          $res = $PageNav['RowList'];
     *          WHILE ( $arr = mysql_fetch_array( $res ) ) {
     *               $DataDisplay .= $arr['Name']."<br>";
     *          }
     * @access  ECHO $PageNav['HrefNav'];
     * @depend  dbRes() is my fuctional equivilate to old ae db_res() or standard mysql_query()
    **/
FUNCTION swPagination( $Sql, $MRPP, $UriAdd, $SortByAllow = FALSE, $SortOrderSeed = FALSE )
{

    GLOBAL $site;

      $Paginate   = array(); # Assign output as Array
      $MaxRowsPP  = $MRPP; # Maximum Rows Per Page
      $SqlAdd     = '';
      $PageNumDis = 6; #If 2 Will show <First><Prev> 1 2 3 4 <Next><Last> +/- Integer value 2 before 2 after
      $_GET  = array_map( 'mysql_real_escape_string', $_GET ); // security

        $Page = ( is_numeric( $_GET['page'] ) && $_GET['page'] > 0 ) ? $_GET['page'] : 1;


        $From = ( ( $Page * $MaxRowsPP ) - $MaxRowsPP );



        IF ( $SortOrderSeed != FALSE OR isset($_GET['sortorder']) ) {
             $SortOrderAllow = array( 'ASC', 'DESC', 'RAND' );
             //$SortOrder = ( in_array(strtoupper($_GET['sortorder']), $SortOrderAllow, TRUE) ) ? strtoupper($_GET['sortorder']) : $SortOrderAllow[0];
             $SortTemp = strtoupper( $_GET['sortorder'] );

             IF ( isset( $SortTemp ) ) {

                  IF ( in_array( $SortTemp, $SortOrderAllow, TRUE ) )  {
                       $SortOrder = $SortTemp;
                  }ELSE{
                       $SortOrder = strtoupper( $SortOrderSeed );
                  }

             }ELSE{
                  $SortOrder = $SortOrderSeed;
             }

             IF ( $SortOrder == 'RAND' ) {
                  $SqlAdd = "RAND()";
             }ELSE{
                  $SqlAdd = $SortOrder;
             }

             $SortOrder = strtolower($SortOrder);
             $SortOrderUri = "&sortorder={$SortOrder}";

        }ELSE{
             $SortOrderUri = "&sortorder=asc";
             $SqlAdd .= 'ASC';
        }

        IF ( $SortByAllow != FALSE ) {

             IF ( is_array($SortByAllow) ) {
                  $SortBy = ( in_array($_GET['sortby'], $SortByAllow) ) ? $_GET['sortby'] : $SortByAllow[0];
             }
             $SortByUri = "&sortby={$SortBy}";
             $SqlAdd .= " ORDER BY {$SortBy} ";
        }ELSE{
             $SortByUri = '';
             $SqlAdd = '';
        }

        $Uri = $UriAdd.$PageUri.$SortByUri.$SortOrderUri;


        $RowList = mysql_query( $Sql ." {$SqlAdd} LIMIT $From, $MaxRowsPP" );

        $Paginate['RowList'] = $RowList;  # used outside our function in a while loop to display our results.



        $TotalResults = mysql_num_rows( mysql_query( $Sql ) ); # USE RAW SQL QUERY FOR THIS
        $TotalPages   = ceil( $TotalResults / $MaxRowsPP );
        $Paginate['TotalRes'] = $TotalResults;


        $ResFrom = $From + 1;
        $ResTo   = $From + mysql_num_rows( $RowList );
        # For Testing Only
        $Paginate['test_Paginate'] = "TotalPages   = " . $TotalPages    . "<br>".
                                 "TotalResults = " . $TotalResults  . "<br>".
                                 "Max Rows PP  = " . $MaxRowsPP     . "<br>".
                                 "Sample TotalResults  = " . mysql_num_rows( mysql_query( $Sql ) ) . "<br>".
                                 "SQL Query =" . $Sql ." {$SqlAdd} LIMIT $From, $MaxRowsPP"; # For Testing Only
#===============================================================
#          START PAGINATE OUTPUT ARRAY
#===============================================================

        IF ( $SortByAllow != FALSE ) {

             FOREACH ( $SortByAllow AS $Key => $Value ) {
                       //echo "Key: $Key; Value: $Value<br />\n";
                  $DisplayName = str_replace( "_", " ", $Value );
                  $DisplayName = ucwords( str_replace ( "-", " ", $Value ) );
                  $Paginate['HrefSort_'.$Value] = "<a href ='{$_SERVER['PHP_SELF']}?{$Uri}'>$DisplayName</a>";
                  $Paginate['UriSort_'.$Value]  = $Uri;
             }

             # $Paginate['HrefSortByID']    = "<a href = '{$_SERVER['PHP_SELF']}?page={$Page}&sortby=list_id&sortorder={$SortOrder}'>ID</a>";
             # $Paginate['UriSortByID']     = "{$PageUri}{$SortByUri}{$SortOrderUri}";
             # $Paginate['HrefSortByOrder'] = "<a href = '{$_SERVER['PHP_SELF']}?page={$Page}&sortby=list_order&sortorder={$SortOrder}'>ORDER</a>";
             # $Paginate['UriSortByOrder']  = "{$PageUri}{$SortByUri}{$SortOrderUri}";
             # $Paginate['HrefSortByName']  = "<a href = '{$_SERVER['PHP_SELF']}?page={$Page}&sortby=list_name&sortorder={$SortOrder}'>NAME</a>";
             # $Paginate['UriSortByName']   = "{$PageUri}{$SortByUri}{$SortOrderUri}";
        }

        # Build First and Prev links. If on page 1, we won't make links
        IF ( $Page > 1 ) {
             $prev = ( $Page - 1 );
             $Paginate['HrefNav'] .= "<a href='{$UriAdd}page=1{$SortByUri}{$SortOrderUri}'>First</a><strong> | </strong>";
             $Paginate['UriNav']  .= "[FIRST]{$UriAdd}page=1{$SortByUri}{$SortOrderUri}[/FIRST]";
             $Paginate['HrefFirst']= "<a href='{$UriAdd}page=1{$SortByUri}{$SortOrderUri}'>First</a><strong> | </strong>";
             $Paginate['HrefNav'] .= "<a href='{$UriAdd}page={$prev}{$SortByUri}{$SortOrderUri}'>Prev</a><strong> | </strong>";
             $Paginate['UriNav']  .= "[PREV]{$UriAdd}page={$prev}{$SortByUri}{$SortOrderUri}[/PREV]";
             $Paginate['HrefPrev'] = "<a href='{$UriAdd}page={$prev}{$SortByUri}{$SortOrderUri}'>Prev</a><strong> | </strong>";
        }

        # build the links to the 2 previous and 2 next pages
        FOR ( $i = ($Page - $PageNumDis ); $i <= ( $Page + $PageNumDis ); $i++ ) {
             # only make a link if the prev/next is a valid page number
             IF ( ( $i >= 1 ) && ( $i <= $TotalPages ) ) {
                  $Paginate['HrefNav'] .= ($Page == $i) ? "<strong>[$i]</strong>" : " <a href='{$UriAdd}page=$i{$SortByUri}{$SortOrderUri}'>$i</a> ";
                  $Paginate['UriNav']  .= ($Page == $i) ? "<strong>[$i]</strong>" : "
[PAGE]{$UriAdd}page=$i{$SortByUri}{$SortOrderUri}[/PAGE]
";
                  $Paginate[$i]         = "{$UriAdd}page=$i{$SortByUri}{$SortOrderUri}";
             }
        }

        # Build Next and Last links. If on last page, we won't make a links
        IF ( $Page < $TotalPages ) {
             $next = ( $Page + 1 );
             $Paginate['HrefNav'] .= "<strong> | </strong><a href='{$UriAdd}page={$next}{$SortByUri}{$SortOrderUri}'>Next</a><strong> | </strong>";
             $Paginate['UriNav']  .= "[NEXT]{$UriAdd}page={$next}{$SortByUri}{$SortOrderUri}[/NEXT]";
             $Paginate['HrefNext'] = " | <a href='{$UriAdd}page={$next}{$SortByUri}{$SortOrderUri}'>Next</a><strong> | </strong>";
             $Paginate['HrefNav'] .= "<a href='{$UriAdd}page={$TotalPages}{$SortByUri}{$SortOrderUri}'>Last</a>";
             $Paginate['UriNav']  .= "[LAST]{$UriAdd}page={$TotalPages}{$SortByUri}{$SortOrderUri}[/LAST]";
             $Paginate['HrefLast'] = "<a href='{$UriAdd}page={$TotalPages}{$SortByUri}{$SortOrderUri}'>Last</a>";
        }

        RETURN $Paginate;
}
?> 

 

This works fine on another page but for some reason it isn't working on this other page.

 

-Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/88895-paginate-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.