By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,722 Members | 1,904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,722 IT Pros & Developers. It's quick & easy.

Problem on paging

chelvan
P: 90
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\findpepoles.php on line 65 (marked as ------error #1)

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


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con=mysql_connect("localhost","root","iceberg");
  3. if(!$con){
  4.     die('could not connect:'.mysql_error());
  5.     }
  6. mysql_select_db("mydata",$con);
  7. $recordsPerPage = 6; 
  8. $pageNum = 1;
  9. if(isset($_GET['p'])) {
  10.   $pageNum = $_GET['p'];
  11.   settype($pageNum, 'integer');
  12. }
  13. $offset = ($pageNum - 1) * $recordsPerPage;
  14.  
  15. $db_c=0;
  16. $pr_c=0;
  17.  
  18. $username=$_POST['h1']; //-------------------error #1
  19. $servalue=$_POST['txtser'];//-------------------error 2
  20.  
  21. $myref=$refno;
  22.  
  23. if($servalue=='')
  24. {
  25. echo " ";
  26. echo "Enter Your Friend Name,City or Country To Find Your Match";
  27. }
  28. else
  29. {
  30. $sql_sel=mysql_query("SELECT * FROM user WHERE fullname='$servalue' or city='$servalue' or country='$servalue' LIMIT $offset, $recordsPerPage");
  31. $sql_sel1=mysql_query("SELECT * FROM user WHERE fullname='$servalue' or city='$servalue' or country='$servalue' ");
  32.  
  33.  
  34.  
  35. while($row=mysql_fetch_array($sql_sel)){
  36.     $frienduse[$db_c]=$row['username'];
  37.     $friendref[$db_c]=$row['refno'];
  38.     $fullname[$db_c]=$row['fullname'];
  39.     $country[$db_c]=$row['country'];
  40.     $lookingfor[$db_c]=$row['lookingFor'];
  41.     $religious[$db_c]=$row['religious'];
  42.     $friendimg[$db_c]=$row['prophoto'];
  43.  
  44.     $db_c=$db_c+1;
  45. }
  46. }
  47. /*if($db_c==0){
  48.     $db_c==1;
  49. }*/
  50.  
  51.  
  52.  
  53. echo "<p align='right'><a href='home.php'>Home</a></p>";
  54. echo "<table align='center'>";
  55. while($pr_c<$db_c){
  56. echo"<tr><td rowspan='5' valign='middle'>";
  57. echo "<a href='serView.php'><img src='img/".$friendimg[$pr_c]."' width='115' heught='120'></a>";echo "&nbsp;</td></tr>";
  58. //echo"<tr><td>";
  59. //echo $friendref[$pr_c]; echo "&nbsp;</td></tr>";
  60. echo"<tr><td>";
  61. 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>";
  62. echo"<tr><td>";
  63. echo $country[$pr_c];echo "&nbsp;</td><td colspan='50'>&nbsp;</td><td>Send Message</td></tr>";
  64. echo"<tr><td>";
  65. echo $lookingfor[$pr_c];echo "&nbsp;</td><td colspan='50'>&nbsp;</td><td><a href='addmyFriends.php?$friendref[$pr_c]'>Friendship</a></td></tr>";
  66. echo"<tr><td>";
  67. echo $religious[$pr_c];echo "&nbsp;<br><br></td></tr>";
  68.  
  69. //echo "<br><br>";
  70. $pr_c=$pr_c+1;
  71. }
  72. echo"</table>";
  73.  
  74.  $query   = "SELECT COUNT(refno) AS dt FROM user WHERE fullname='$servalue' or city='$servalue' or country='$servalue';";
  75.  
  76.   $result  = mysql_query($query) or die('Mysql Err. 2');
  77.   $row     = mysql_fetch_assoc($result);
  78.   $numrows = $row['dt'];
  79.  
  80.   $maxPage = ceil($numrows/$recordsPerPage);
  81.  
  82.   $nav = '';
  83.   for($page = 1; $page <= $maxPage; $page++)
  84.   {
  85.        if ($page == $pageNum)
  86.        {
  87.             $nav .= "<div class=\"pNo\">$page</div>";
  88.        }
  89.       else
  90.        {
  91.             $nav .= "<div class=\"pNo\"><a href=\"?p=$page\">$page</a></div>";
  92.        }
  93.   }
  94.  
  95.   if ($pageNum > 1)  {
  96.  
  97.        $page = $pageNum - 1;
  98.        $prev = "<a href=\"?p=$page\"><strong><</strong></a>";
  99.  
  100.        $first = "<a href=\"?p=1\"><strong><<</strong></a>";
  101.   }
  102.   else {
  103.        $prev  = '<strong>  </strong>';
  104.        $first = '<strong>   </strong>';
  105.   }
  106.  
  107.   if ($pageNum < $maxPage) {
  108.        $page = $pageNum + 1;
  109.        $next = "<a href=\"?p=$page\"><strong>></strong></a>";
  110.  
  111.        $last = "<a href=\"?p=$maxPage\"><strong>>></strong></a>";
  112.   }
  113.   else {
  114.        $next = '<strong>  </strong>';
  115.        $last = '<strong>   </strong>';
  116.   }
  117.    echo '<br />'; 
  118.   echo '<br />';
  119.  
  120.   echo "<div class=\"pagingDiv\">
  121.       <div class=\"pNo\">$first</div>
  122.       <div class=\"pNo\">$prev</div>
  123.       $nav
  124.       <div class=\"pNo\">$next</div>
  125.       <div class=\"pNo\">$last</div></div>";
  126. ?>
  127.  
i couldn't understand that? can any one explain that?


thanks
chel-1
Sep 25 '08 #1
Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,058
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.
Sep 25 '08 #2

chelvan
P: 90
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
Sep 26 '08 #3

chelvan
P: 90
hi
now my question is....

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con=mysql_connect("localhost","root","iceberg");
  3.  
  4. if(!$con){
  5. die('could not connect:'.mysql_error());
  6. }
  7. mysql_select_db("mydata",$con);
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14. // how many rows to show per page
  15. $rowsPerPage = 2;
  16.  
  17. // by default we show first page
  18. $pageNum = 1;
  19.  
  20. // if $_GET['page'] defined, use it as page number
  21. if(isset($_GET['page']))
  22. {
  23.     $pageNum = $_GET['page'];
  24. }
  25.  
  26. // counting the offset
  27. $offset = ($pageNum - 1) * $rowsPerPage;
  28.  
  29. echo "<table align='center' border='0'>";
  30.  
  31. $sql_fr=mysql_query("SELECT*FROM myfriends WHERE refno='$refno'");
  32. while($row=mysql_fetch_array($sql_fr)){
  33.     $fref[$my]= $row['myfriend'];
  34.  
  35.     $query = " SELECT * FROM user WHERE refno='$fref[$my]'" .
  36.          " LIMIT $offset, $rowsPerPage";
  37. $result = mysql_query($query) or die('Error, query failed');
  38.  
  39. // print 
  40. while($row = mysql_fetch_array($result)){
  41.       $fname[$my]=$row['fullname'];
  42.     $photo[$my]=$row['prophoto'];
  43.     $em[$my]=$row['email'];
  44.     $bd[$my]=$row['birthdate'];
  45.  
  46.     $em[$my]=substr($em[$my],0,strpos($em[$my],"/"));
  47.     $pos[$my]=strpos($fname[$my],' ');
  48.      $fname[$my]=substr($fname[$my],0,$pos[$my]);
  49.  
  50.     $my+=1;
  51.     }
  52. }
  53.  
  54.  
  55. echo "<table align='center' border='0'>";
  56. while($fr_c<$my){
  57.     echo"<tr><td rowspan='5' aligh='middle'><img src='img/".$photo[$fr_c]."' width='115' height='120'></td></tr>";
  58.     echo "<tr><td>".$fname[$fr_c]."</td><td width='200'></td><td><a href='viewgrps.php?$fref[$fr_c]'>View Groups</a></td></tr>";
  59.     echo "<tr><td>".$em[$fr_c]."</td><td width='200'></td><td><a href='fralbpage.php?$fref[$fr_c]'>View Albums</a></td></tr>";
  60.     echo "<tr><td>".$bd[$fr_c]."</td><td width='200'></td><td><a href='events.php'>View Events</a></td></tr>";
  61.     echo "<tr><td>Send Message</td></tr>";
  62.     echo"<tr><td>&nbsp;</td></tr>";
  63.     $fr_c+=1;
  64.     }
  65. echo "</table>";
  66.  
  67. // how many rows we have in database
  68. $query   = "SELECT COUNT(refno) AS numrows FROM myfriends WHERE refno='$refno'";
  69. $result  = mysql_query($query) or die('Error, query failed');
  70. $row     = mysql_fetch_array($result, MYSQL_ASSOC);
  71. $numrows = $row['numrows'];
  72.  
  73. // how many pages we have when using paging?
  74. $maxPage = ceil($numrows/$rowsPerPage);
  75.  
  76. // print the link to access each page
  77. $self = $_SERVER['PHP_SELF'];
  78. $nav  = '';
  79.  
  80. for($page = 1; $page <= $maxPage; $page++)
  81. {
  82.    if ($page == $pageNum)
  83.    {
  84.       $nav .= " $page "; // no need to create a link to current page
  85.    }
  86.    else
  87.    {
  88.       $nav .= " <a href=\"$self?page=$page\">$page</a> ";
  89.    } 
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. // creating previous and next link
  100. // plus the link to go straight to
  101. // the first and last page
  102.  
  103. if ($pageNum > 1)
  104. {
  105.    $page  = $pageNum - 1;
  106.    $prev  = " <a href=\"$self?page=$page\">[Prev]</a> ";
  107.  
  108.    $first = " <a href=\"$self?page=1\">[First Page]</a> ";
  109. else
  110. {
  111.    $prev  = '&nbsp;'; // we're on page one, don't print previous link
  112.    $first = '&nbsp;'; // nor the first page link
  113. }
  114.  
  115. if ($pageNum < $maxPage)
  116. {
  117.    $page = $pageNum + 1;
  118.    $next = " <a href=\"$self?page=$page\">[Next]</a> ";
  119.  
  120.    $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
  121. else
  122. {
  123.    $next = '&nbsp;'; // we're on the last page, don't print next link
  124.    $last = '&nbsp;'; // nor the last page link
  125. }
  126.  
  127. // print the navigation link
  128. echo "<br><br>";
  129. echo $first . $prev . $nav . $next . $last;
  130.  
  131. // and close the database connection
  132.  
  133.  
  134.  
  135.  
  136. mysql_close();
  137.  
  138. ?>
  139.  
  140.  

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
Sep 26 '08 #4

P: 9
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

Expand|Select|Wrap|Line Numbers
  1. while($row=mysql_fetch_array($sql_fr)){
  2.     $fref[$my]= $row['myfriend'];
  3.  
  4.     $query = " SELECT * FROM user WHERE refno='$fref[$my]'" .
  5.          " LIMIT $offset, $rowsPerPage";
  6. $result = mysql_query($query) or die('Error, query failed');
  7.  
  8. // print 
  9. while($row = mysql_fetch_array($result)){
  10.       $fname[$my]=$row['fullname'];
  11.     $photo[$my]=$row['prophoto'];
  12.     $em[$my]=$row['email'];
  13.     $bd[$my]=$row['birthdate'];
  14.  
  15.     $em[$my]=substr($em[$my],0,strpos($em[$my],"/"));
  16.     $pos[$my]=strpos($fname[$my],' ');
  17.      $fname[$my]=substr($fname[$my],0,$pos[$my]);
  18.  
  19.     $my+=1;
  20.     }
  21. }
  22.  
Sep 29 '08 #5

chelvan
P: 90
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

Expand|Select|Wrap|Line Numbers
  1. while($row=mysql_fetch_array($sql_fr)){
  2.     $fref[$my]= $row['myfriend'];
  3.  
  4.     $query = " SELECT * FROM user WHERE refno='$fref[$my]'" .
  5.          " LIMIT $offset, $rowsPerPage";
  6. $result = mysql_query($query) or die('Error, query failed');
  7.  
  8. // print 
  9. while($row = mysql_fetch_array($result)){
  10.       $fname[$my]=$row['fullname'];
  11.     $photo[$my]=$row['prophoto'];
  12.     $em[$my]=$row['email'];
  13.     $bd[$my]=$row['birthdate'];
  14.  
  15.     $em[$my]=substr($em[$my],0,strpos($em[$my],"/"));
  16.     $pos[$my]=strpos($fname[$my],' ');
  17.      $fname[$my]=substr($fname[$my],0,$pos[$my]);
  18.  
  19.     $my+=1;
  20.     }
  21. }
  22.  

thanks Gabo!
now its working.

chel-1
Sep 30 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.