My $_POST variable works only for the first page.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mesut
    New Member
    • May 2007
    • 3

    My $_POST variable works only for the first page.

    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:
    [code=php]
    <html>
    <head>
    <style type="text/css">
    A:link {text-decoration: none}
    A:visited {text-decoration: none}
    A:active {text-decoration: none}
    A:hover {text-decoration: underline overline; color: red;}
    table.second { margin-top:20px; margin-bottom:0px;}
    </style>
    <title>Implemen ting Paging with next and prev</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#99CC9 9">

    <table width="60%" align="center" bgcolor="#00996 6"><tr>
    <td><a href="insert.ph p">Veri Gir</a></td>
    <td><a href="query.php ">Veri Görüntüle</a></td>
    <td><a href="search.ph p"> Veri Ara </a></td>
    <td><a href="update.ph p"> Veri Güncelle </a></td>
    <td><a href="logout.ph p"> Ç?k?? </a></td>
    </tr></table>
    <?php
    include 'connect.php';
    $orderby = $_POST['orderby'];
    $type = $_POST['type'];

    if (isset($orderby )) {
    echo "orderby is set\n";
    } else {
    echo "orderby is not set\n";
    }
    if (isset($type)) {
    echo "type is set\n";
    } else {
    echo "type is not set\n";
    }

    // how many rows to show per page
    $rowsPerPage = 5;

    // 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 * FROM Kunde ORDER BY $orderby" .
    " LIMIT $offset, $rowsPerPage";
    $result = mysql_query($qu ery) or die('Error, query failed');

    // start of the query *************** *************** *************** *************** *************** ****
    ?>
    <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>
    <?php
    while($row = mysql_fetch_arr ay($result))
    {
    ?>
    <tr><td><?php echo $row['KundeID']; ?> </td>
    <td> <?php echo $row['KundeVorName']."&nbsp;".$r ow['KundeNachName']; ?> </td>
    <td> <?php echo $row['KundeFirma']; ?> </td>
    <td> <?php echo $row['KundeKategorie ']; ?> </td>
    <td> <?php echo $row['KundeProdukte']; ?> </td>
    <td> <?php echo $row['KundeAdresse']; ?> </td>
    <td> <?php echo $row['KundeOrt']; ?> </td>
    <td> <?php echo $row['KundeLand']; ?> </td>
    <td> <?php echo $row['KundeArt']; ?> </td>
    <td> <?php echo $row['KundeFestnetz']."&nbsp;"; ?> </td>
    <td> <?php echo $row['KundeFax']."&nbsp;"; ?> </td>
    <td> <?php echo $row['KundeMobile']."&nbsp;"; ?> </td>
    <td> <?php $mail = $row['KundeMail']; ?><a href="mailto:<? php echo "$mail"; ?>"><?php echo $row['KundeMail']."&nbsp;"; ?> </a></td>
    <td> <?php $web = $row['KundeWeb']; ?> <a href="http://<?php echo "$web"; ?>" Target="blank"> <?php echo $row['KundeWeb']."&nbsp;"; ?></a> </td>
    <td> <?php echo $row['KundeRegDatum']."&nbsp;"; ?> </td></tr>
    <?php } ?>
    </table>
    <?php
    // end of the query *************** *************** *************** *************** *************** ****
    // how many rows we have in database
    $query = "SELECT COUNT(KundeID) AS numrows FROM Kunde ";
    $result = mysql_query($qu ery) or die('Error, query failed');
    $row = mysql_fetch_arr ay($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?pa ge=$page\">$pag e</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?pa ge=$page\">[Prev]</a> ";

    $first = " <a href=\"$self?pa ge=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?pa ge=$page\">[Next]</a> ";

    $last = " <a href=\"$self?pa ge=$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 $first . $prev . $nav . $next . $last;

    // and close the database connection

    ?>
    </body>
    </html>
    [/code]

    [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?
    Last edited by pbmods; May 30 '07, 05:22 PM. Reason: Added code tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    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.

    Comment

    • Mesut
      New Member
      • May 2007
      • 3

      #3
      Originally posted by pbmods
      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
      [code=php]
      foreach($_POST as $k => $v)
      {
      $_SESSION[$k] = $v;
      }
      [/code]

      [Please use CODE tags when posting source code. Thanks! --pbmods]
      variable...??
      Anyway thats an amazing fast answer, you are great!!

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by Mesut
        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:
        [code=php]
        // Add this line, or else nothing will work properly:
        session_start() ;

        foreach($_POST as $k => $v)
        {
        $_SESSION[$k] = $v;
        }
        [/code]

        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
        [code=php]
        $orderby = $_POST['orderby'];
        [/code]

        to

        [code=php]
        $orderby = $_SESSION['orderby'];
        [/code]

        Comment

        Working...