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

My $_POST variable works only for the first page.

P: 3
I have written a form in with radio buttons the name is set to orderby and the value is set to KundeVorName and the next value is KundeNachName and it goes so on. I wanna modify my query according this values. It works well for the first page but at the second it looses its value and gives me an error message. When i hard code it like "Select * from Kunde ORDER BY KundeVorName" it works for all pages when i use my variable $orderby it works just for the first page. here is my code:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. A:link {text-decoration: none}
  5. A:visited {text-decoration: none}
  6. A:active {text-decoration: none}
  7. A:hover {text-decoration: underline overline; color: red;}
  8. table.second { margin-top:20px; margin-bottom:0px;}
  9. </style>
  10. <title>Implementing Paging with next and prev</title>
  11. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  12. </head>
  13.  
  14. <body bgcolor="#99CC99">
  15.  
  16. <table width="60%" align="center" bgcolor="#009966"><tr>
  17. <td><a href="insert.php">Veri Gir</a></td>
  18. <td><a href="query.php">Veri Görüntüle</a></td>
  19. <td><a href="search.php"> Veri Ara </a></td>
  20. <td><a href="update.php"> Veri Güncelle </a></td>
  21. <td><a href="logout.php"> Ç?k?? </a></td>
  22. </tr></table>
  23. <?php
  24. include 'connect.php';
  25. $orderby = $_POST['orderby'];
  26. $type = $_POST['type'];
  27.  
  28.     if (isset($orderby)) {
  29.         echo "orderby is set\n";
  30.     } else {
  31.         echo "orderby is not set\n";
  32.     }
  33.     if (isset($type)) {
  34.         echo "type is set\n";
  35.     } else {
  36.         echo "type is not set\n";
  37.     }
  38.  
  39. // how many rows to show per page
  40. $rowsPerPage = 5;
  41.  
  42. // by default we show first page
  43. $pageNum = 1;
  44.  
  45. // if $_GET['page'] defined, use it as page number
  46. if(isset($_GET['page']))
  47. {
  48.     $pageNum = $_GET['page'];
  49. }
  50.  
  51. // counting the offset
  52. $offset = ($pageNum - 1) * $rowsPerPage;
  53.  
  54.  
  55. $query = " SELECT * FROM Kunde ORDER BY $orderby" .
  56.          " LIMIT $offset, $rowsPerPage";
  57. $result = mysql_query($query) or die('Error, query failed');
  58.  
  59. // start of the query *******************************************************************************
  60. ?>
  61. <div><table class='second' border='1'><tr><td>ID</td><td>?sim Soyad</td><td>Firma</td><td>S?n?f</td><td>Ürünler</td><td>Adres</td><td>?ehir</td><td>Ülke</td><td>Türü</td><td>?? Telefonu</td><td>Fax</td><td>Cep</td><td>Mail</td><td>Web</td><td>Tarih</td></b></tr>
  62. <?php
  63. while($row = mysql_fetch_array($result))
  64. {
  65. ?>
  66. <tr><td><?php echo $row['KundeID']; ?> </td>
  67. <td> <?php echo $row['KundeVorName']."&nbsp;".$row['KundeNachName']; ?> </td>
  68. <td> <?php echo $row['KundeFirma']; ?> </td>
  69. <td> <?php echo $row['KundeKategorie']; ?> </td>
  70. <td> <?php echo $row['KundeProdukte']; ?> </td>
  71. <td> <?php echo $row['KundeAdresse']; ?> </td>
  72. <td> <?php echo $row['KundeOrt']; ?> </td>
  73. <td> <?php echo $row['KundeLand']; ?> </td>
  74. <td> <?php echo $row['KundeArt']; ?> </td>
  75. <td> <?php echo $row['KundeFestnetz']."&nbsp;"; ?> </td>
  76. <td> <?php echo $row['KundeFax']."&nbsp;"; ?> </td>
  77. <td> <?php echo $row['KundeMobile']."&nbsp;"; ?> </td>
  78. <td> <?php $mail = $row['KundeMail']; ?><a href="mailto:<?php echo "$mail"; ?>"><?php echo $row['KundeMail']."&nbsp;"; ?> </a></td>
  79. <td> <?php $web = $row['KundeWeb']; ?> <a href="http://<?php echo "$web"; ?>" Target="blank"> <?php echo $row['KundeWeb']."&nbsp;"; ?></a> </td>
  80. <td> <?php echo $row['KundeRegDatum']."&nbsp;"; ?> </td></tr>
  81. <?php } ?>
  82. </table>
  83. <?php
  84. // end of the query *******************************************************************************
  85. // how many rows we have in database
  86. $query   = "SELECT COUNT(KundeID) AS numrows FROM Kunde ";
  87. $result  = mysql_query($query) or die('Error, query failed');
  88. $row     = mysql_fetch_array($result, MYSQL_ASSOC);
  89. $numrows = $row['numrows'];
  90.  
  91. // how many pages we have when using paging?
  92. $maxPage = ceil($numrows/$rowsPerPage);
  93.  
  94. // print the link to access each page
  95. $self = $_SERVER['PHP_SELF'];
  96. $nav = '';
  97. for($page = 1; $page <= $maxPage; $page++)
  98. {
  99.     if ($page == $pageNum)
  100.     {
  101.         $nav .= " $page ";   // no need to create a link to current page
  102.     }
  103.     else
  104.     {
  105.         $nav .= " <a href=\"$self?page=$page\">$page</a> ";
  106.     }        
  107. }
  108.  
  109. // creating previous and next link
  110. // plus the link to go straight to
  111. // the first and last page
  112.  
  113. if ($pageNum > 1)
  114. {
  115.     $page = $pageNum - 1;
  116.     $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
  117.  
  118.     $first = " <a href=\"$self?page=1\">[First Page]</a> ";
  119. else
  120. {
  121.     $prev  = '&nbsp;'; // we're on page one, don't print previous link
  122.     $first = '&nbsp;'; // nor the first page link
  123. }
  124.  
  125. if ($pageNum < $maxPage)
  126. {
  127.     $page = $pageNum + 1;
  128.     $next = " <a href=\"$self?page=$page\">[Next]</a> ";
  129.  
  130.     $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
  131. else
  132. {
  133.     $next = '&nbsp;'; // we're on the last page, don't print next link
  134.     $last = '&nbsp;'; // nor the last page link
  135. }
  136.  
  137. // print the navigation link
  138. echo $first . $prev . $nav . $next . $last;
  139.  
  140. // and close the database connection
  141.  
  142. ?>
  143. </body>
  144. </html>
  145.  
[Please use CODE tags when posting source code. Thanks! --pbmods]
I get orderby is not set ,type is not set and query error messages.
How can I fix my problem with the variables?
May 30 '07 #1
Share this Question
Share on Google+
3 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Mesut. Welcome to TSDN!

Um... that's a lot of code you just dumped there. And most of it really isn't relevant to your problem at all.

You might want to save your POST variables in the session. Take a look at this thread.
May 30 '07 #2

P: 3
Heya, Mesut. Welcome to TSDN!

Um... that's a lot of code you just dumped there. And most of it really isn't relevant to your problem at all.

You might want to save your POST variables in the session. Take a look at this thread.

I am really a newbie, i did not get the answer i have modified most of the scripts now i a m stucked up, i dont know how to modify
Expand|Select|Wrap|Line Numbers
  1. foreach($_POST as $k => $v)
  2. {
  3. $_SESSION[$k] = $v;
  4. }
  5.  
[Please use CODE tags when posting source code. Thanks! --pbmods]
variable...??
Anyway thats an amazing fast answer, you are great!!
May 30 '07 #3

pbmods
Expert 5K+
P: 5,821
I am really a newbie, i did not get the answer
We've all been there. No problem ^_^

First off, you need to make one change:
Expand|Select|Wrap|Line Numbers
  1. //  Add this line, or else nothing will work properly:
  2. session_start();
  3.  
  4. foreach($_POST as $k => $v)
  5. {
  6. $_SESSION[$k] = $v;
  7. }
  8.  
Alrightey. What this code does is run through $_POST and copy the values in $_POST to $_SESSION.

To be more verbose, for each element in $_POST, we assign the key (e.g., 'orderby') to $k and the value (e.g., 'KundeVorName') to $v. Then we set $_SESION[$k] to $v ($_SESSION['orderby'] = 'KundeVorName').

The end result is that anything that was in $_POST is now in $_SESSION, and $_SESSION (unlike $_POST) is persistent between page loads.

Put that script by where you assign the value of $orderby and then change your statement from
Expand|Select|Wrap|Line Numbers
  1. $orderby = $_POST['orderby'];
  2.  
to

Expand|Select|Wrap|Line Numbers
  1. $orderby = $_SESSION['orderby'];
  2.  
May 30 '07 #4

Post your reply

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