Problem on paging

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chelvan
    New Member
    • Aug 2008
    • 90

    Problem on paging

    Hi Forums
    When i doing following code, it do the query & show the result contain pages. but when i move to the next page that displays the error message.

    the error message is :

    Notice: Undefined index: h1 in C:\AppServ\www\ facebook\findpe poles.php on line 65 (marked as ------error #1)

    Notice: Undefined index: txtser in C:\AppServ\www\ facebook\findpe poles.php on line 66 (marked as ------error #2)


    Code:
    <?php
    $con=mysql_connect("localhost","root","iceberg");
    if(!$con){
    	die('could not connect:'.mysql_error());
    	}
    mysql_select_db("mydata",$con);
    $recordsPerPage = 6; 
    $pageNum = 1;
    if(isset($_GET['p'])) {
      $pageNum = $_GET['p'];
      settype($pageNum, 'integer');
    }
    $offset = ($pageNum - 1) * $recordsPerPage;
    
    $db_c=0;
    $pr_c=0;
    
    $username=$_POST['h1']; //-------------------error #1
    $servalue=$_POST['txtser'];//-------------------error 2
    
    $myref=$refno;
    
    if($servalue=='')
    {
    echo " ";
    echo "Enter Your Friend Name,City or Country To Find Your Match";
    }
    else
    {
    $sql_sel=mysql_query("SELECT * FROM user WHERE fullname='$servalue' or city='$servalue' or country='$servalue' LIMIT $offset, $recordsPerPage");
    $sql_sel1=mysql_query("SELECT * FROM user WHERE fullname='$servalue' or city='$servalue' or country='$servalue' ");
    
    
    
    while($row=mysql_fetch_array($sql_sel)){
    	$frienduse[$db_c]=$row['username'];
    	$friendref[$db_c]=$row['refno'];
    	$fullname[$db_c]=$row['fullname'];
    	$country[$db_c]=$row['country'];
    	$lookingfor[$db_c]=$row['lookingFor'];
    	$religious[$db_c]=$row['religious'];
    	$friendimg[$db_c]=$row['prophoto'];
    
    	$db_c=$db_c+1;
    }
    }
    /*if($db_c==0){
    	$db_c==1;
    }*/
    
    
    
    echo "<p align='right'><a href='home.php'>Home</a></p>";
    echo "<table align='center'>";
    while($pr_c<$db_c){
    echo"<tr><td rowspan='5' valign='middle'>";
    echo "<a href='serView.php'><img src='img/".$friendimg[$pr_c]."' width='115' heught='120'></a>";echo "&nbsp;</td></tr>";
    //echo"<tr><td>";
    //echo $friendref[$pr_c]; echo "&nbsp;</td></tr>";
    echo"<tr><td>";
    echo $fullname[$pr_c];echo "&nbsp;</td><td colspan='50'>&nbsp;</td><td><a href='serView.php?$friendref[$pr_c]'>View Profile</a></td></tr>";
    echo"<tr><td>";
    echo $country[$pr_c];echo "&nbsp;</td><td colspan='50'>&nbsp;</td><td>Send Message</td></tr>";
    echo"<tr><td>";
    echo $lookingfor[$pr_c];echo "&nbsp;</td><td colspan='50'>&nbsp;</td><td><a href='addmyFriends.php?$friendref[$pr_c]'>Friendship</a></td></tr>";
    echo"<tr><td>";
    echo $religious[$pr_c];echo "&nbsp;<br><br></td></tr>";
    
    //echo "<br><br>";
    $pr_c=$pr_c+1;
    }
    echo"</table>";
    
     $query   = "SELECT COUNT(refno) AS dt FROM user WHERE fullname='$servalue' or city='$servalue' or country='$servalue';";
    
      $result  = mysql_query($query) or die('Mysql Err. 2');
      $row     = mysql_fetch_assoc($result);
      $numrows = $row['dt'];
    
      $maxPage = ceil($numrows/$recordsPerPage);
    
      $nav = '';
      for($page = 1; $page <= $maxPage; $page++)
      {
           if ($page == $pageNum)
           {
                $nav .= "<div class=\"pNo\">$page</div>";
           }
          else
           {
                $nav .= "<div class=\"pNo\"><a href=\"?p=$page\">$page</a></div>";
           }
      }
    
      if ($pageNum > 1)  {
    
           $page = $pageNum - 1;
           $prev = "<a href=\"?p=$page\"><strong><</strong></a>";
    
           $first = "<a href=\"?p=1\"><strong><<</strong></a>";
      }
      else {
           $prev  = '<strong>  </strong>';
           $first = '<strong>   </strong>';
      }
    
      if ($pageNum < $maxPage) {
           $page = $pageNum + 1;
           $next = "<a href=\"?p=$page\"><strong>></strong></a>";
    
           $last = "<a href=\"?p=$maxPage\"><strong>>></strong></a>";
      }
      else {
           $next = '<strong>  </strong>';
           $last = '<strong>   </strong>';
      }
       echo '<br />'; 
      echo '<br />';
    
      echo "<div class=\"pagingDiv\">
          <div class=\"pNo\">$first</div>
          <div class=\"pNo\">$prev</div>
          $nav
          <div class=\"pNo\">$next</div>
          <div class=\"pNo\">$last</div></div>";
    ?>
    i couldn't understand that? can any one explain that?


    thanks
    chel-1
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    And undefined index warning means that you are trying to use an array element that doesn't exist.

    You need to find where that is happening and make sure the element does exist before you try to use it.

    The isset function can help with that.

    Comment

    • chelvan
      New Member
      • Aug 2008
      • 90

      #3
      Originally posted by Atli
      Hi.

      And undefined index warning means that you are trying to use an array element that doesn't exist.

      You need to find where that is happening and make sure the element does exist before you try to use it.

      The isset function can help with that.
      thanks atli.
      i try to find that.

      chel-1

      Comment

      • chelvan
        New Member
        • Aug 2008
        • 90

        #4
        hi
        now my question is....

        Code:
        <?php
        $con=mysql_connect("localhost","root","iceberg");
        
        if(!$con){
        die('could not connect:'.mysql_error());
        }
        mysql_select_db("mydata",$con);
        
        
        
        
        
        
        // how many rows to show per page
        $rowsPerPage = 2;
        
        // 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;
        
        echo "<table align='center' border='0'>";
        
        $sql_fr=mysql_query("SELECT*FROM myfriends WHERE refno='$refno'");
        while($row=mysql_fetch_array($sql_fr)){
        	$fref[$my]= $row['myfriend'];
        	
        	$query = " SELECT * FROM user WHERE refno='$fref[$my]'" .
                 " LIMIT $offset, $rowsPerPage";
        $result = mysql_query($query) or die('Error, query failed');
        
        // print 
        while($row = mysql_fetch_array($result)){
          	$fname[$my]=$row['fullname'];
        	$photo[$my]=$row['prophoto'];
        	$em[$my]=$row['email'];
        	$bd[$my]=$row['birthdate'];
        	
        	$em[$my]=substr($em[$my],0,strpos($em[$my],"/"));
        	$pos[$my]=strpos($fname[$my],' ');
        	 $fname[$my]=substr($fname[$my],0,$pos[$my]);
        	
        	$my+=1;
        	}
        }
        
        
        echo "<table align='center' border='0'>";
        while($fr_c<$my){
        	echo"<tr><td rowspan='5' aligh='middle'><img src='img/".$photo[$fr_c]."' width='115' height='120'></td></tr>";
        	echo "<tr><td>".$fname[$fr_c]."</td><td width='200'></td><td><a href='viewgrps.php?$fref[$fr_c]'>View Groups</a></td></tr>";
        	echo "<tr><td>".$em[$fr_c]."</td><td width='200'></td><td><a href='fralbpage.php?$fref[$fr_c]'>View Albums</a></td></tr>";
        	echo "<tr><td>".$bd[$fr_c]."</td><td width='200'></td><td><a href='events.php'>View Events</a></td></tr>";
        	echo "<tr><td>Send Message</td></tr>";
        	echo"<tr><td>&nbsp;</td></tr>";
        	$fr_c+=1;
        	}
        echo "</table>";
        
        // how many rows we have in database
        $query   = "SELECT COUNT(refno) AS numrows FROM myfriends WHERE refno='$refno'";
        $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);
        
        // print the link to access each page
        $self = $_SERVER['PHP_SELF'];
        $nav  = '';
        
        for($page = 1; $page <= $maxPage; $page++)
        {
           if ($page == $pageNum)
           {
              $nav .= " $page "; // no need to create a link to current page
           }
           else
           {
              $nav .= " <a href=\"$self?page=$page\">$page</a> ";
           } 
        }
        
        
        
        
        
        
        
        
        // creating previous and next link
        // plus the link to go straight to
        // the first and last page
        
        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  = '&nbsp;'; // we're on page one, don't print previous link
           $first = '&nbsp;'; // nor the first page link
        }
        
        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 = '&nbsp;'; // we're on the last page, don't print next link
           $last = '&nbsp;'; // nor the last page link
        }
        
        // print the navigation link
        echo "<br><br>";
        echo $first . $prev . $nav . $next . $last;
        
        // and close the database connection
        
        
        
        
        mysql_close();
        
        ?>

        the above paging code find the num of rows on database & it define the upcoming pages. but my problem is on the next page doesn't have any data.
        why?

        thanks
        chel-1

        Comment

        • gabosom
          New Member
          • Sep 2008
          • 9

          #5
          what comes to my mind is that you're trying to do a query while looping in the results of another query in this part. I'm not really sure if you could do that. I would recommend to first finish working with all the results from one query, store the info you need in an array and then work on the second query with that array OR create two links to the DB and then one query use it with one link and the other with the second link.

          Cheers,

          Gabo

          Code:
          while($row=mysql_fetch_array($sql_fr)){
              $fref[$my]= $row['myfriend'];
           
              $query = " SELECT * FROM user WHERE refno='$fref[$my]'" .
                   " LIMIT $offset, $rowsPerPage";
          $result = mysql_query($query) or die('Error, query failed');
           
          // print 
          while($row = mysql_fetch_array($result)){
                $fname[$my]=$row['fullname'];
              $photo[$my]=$row['prophoto'];
              $em[$my]=$row['email'];
              $bd[$my]=$row['birthdate'];
           
              $em[$my]=substr($em[$my],0,strpos($em[$my],"/"));
              $pos[$my]=strpos($fname[$my],' ');
               $fname[$my]=substr($fname[$my],0,$pos[$my]);
           
              $my+=1;
              }
          }

          Comment

          • chelvan
            New Member
            • Aug 2008
            • 90

            #6
            Originally posted by gabosom
            what comes to my mind is that you're trying to do a query while looping in the results of another query in this part. I'm not really sure if you could do that. I would recommend to first finish working with all the results from one query, store the info you need in an array and then work on the second query with that array OR create two links to the DB and then one query use it with one link and the other with the second link.

            Cheers,

            Gabo

            Code:
            while($row=mysql_fetch_array($sql_fr)){
                $fref[$my]= $row['myfriend'];
             
                $query = " SELECT * FROM user WHERE refno='$fref[$my]'" .
                     " LIMIT $offset, $rowsPerPage";
            $result = mysql_query($query) or die('Error, query failed');
             
            // print 
            while($row = mysql_fetch_array($result)){
                  $fname[$my]=$row['fullname'];
                $photo[$my]=$row['prophoto'];
                $em[$my]=$row['email'];
                $bd[$my]=$row['birthdate'];
             
                $em[$my]=substr($em[$my],0,strpos($em[$my],"/"));
                $pos[$my]=strpos($fname[$my],' ');
                 $fname[$my]=substr($fname[$my],0,$pos[$my]);
             
                $my+=1;
                }
            }

            thanks Gabo!
            now its working.

            chel-1

            Comment

            Working...