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

Passing variables to pages via Sessions

P: 17
i have page findroom.php that will redirect to the page DisplayDetails.php.

i wanted the DisplayDetails.php page to display data from the query in the page findroom.php. The data that i wanted to display from the query from page DisplayDetails is 'room_price' and 'room_type'.

However, when i clik the Book Now link, it dosesnt't display the value.

Since i'm just a starter in php, can someone tell me am i treating the session the right way as it doesn't diplay any result for the data that i wanted to take from the query in the findroom.php. But, for the the others data which is 'checkin', 'checkout' and others, it just show perfectly.

I do hope if someone can tell me am i using the session in the right way or not.

below is the code: findroom.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. unset($_SESSION['error']);
  4. // echo variable from the session, we set this on our other page
  5. $_SESSION['checkin'] = $_POST['checkin'];
  6. //$_SESSION['checkin']=$checkin;
  7. $_SESSION['checkout'] = $_POST['checkout'];
  8. $_SESSION['rooms']= $_POST['rooms'];
  9. $_SESSION['adults']= $_POST['adults'];
  10. $_SESSION['children']= $_POST['children'];
  11. $days = (strtotime($_POST['checkout']) - strtotime($_POST['checkin'])) / (60 * 60 * 24);
  12.  
  13. $_SESSION['days']=$days;
  14.  
  15. $_SESSION['room_price']=$data['room_price'];
  16. $room_price=$_SESSION['room_price'];
  17.  
  18. $_SESSION['room_type']=$data['room_type'];
  19. $room_type=$_SESSION['room_type'];
  20. ?>
  21.  
  22. <html>
  23. <body>
  24. <form action="DisplayDetails.php" method="post">
  25. <p>
  26.  
  27. <?php
  28.  
  29. //$result = mysql_query("SELECT id_no,room_type,room_price from room1 WHERE room_no NOT IN ( SELECT id_room_no
  30. //FROM reservation1 WHERE datein >='$datein' AND dateout <='$dateout')");
  31.  
  32. $result = mysql_query("SELECT room_price, room_type from room1 WHERE room_no NOT IN ( SELECT id_room_no
  33. FROM reservation1 WHERE datein >='$datein' AND dateout <='$dateout')");
  34. ?>
  35.  
  36. <?php 
  37.     /*if(isset($_POST['Check']) && $_POST['Check']=='Submit') 
  38.     { 
  39.           echo "The rooms availale on the date of :";
  40.           echo $datein;
  41.           echo "  until  ";
  42.           echo $dateout; 
  43.     } */
  44. ?> 
  45.  
  46. </p>
  47. <p><strong><strong>Room Availbility</strong> </p>
  48. <td><table width="61%" height="64" border="1" cellpadding="0" cellspacing="0" bordercolor="#CC66CC" class="report2">
  49.       <tr>
  50.         <td width="190" bgcolor="#E8E8E8"><div align="center"><strong>Room Type </strong></div></td>
  51.         <td width="218" bgcolor="#E8E8E8"><div align="center"><strong>Room Price </strong></div></td>
  52.         <td bgcolor="#E8E8E8"><strong>Task</strong></div></td>
  53.       </tr>
  54.       <?php
  55.         //$counter=1;
  56.         while ($data = mysql_fetch_array($result)):
  57.         ?>
  58.       <tr>
  59.  
  60.         <td><?php echo $data['room_type']; ?></td>
  61.         <td><?php echo $data['room_price']; ?></td>
  62.   <td width="153"><label><a href="DisplayDetails.php?id_no=<?php echo $data['id_no'];?>"><strong>Book Now</strong></a></label></td>
  63.  
  64.  
  65.     </tr>
  66.       <?php
  67.           //$counter++;
  68.           endwhile;
  69.         ?>
  70.     </table>
  71.   <table width="373" border="1">
  72.      <tr>
  73.        <td colspan="2"><strong>Reservation Summary</strong></td>
  74.       </tr>
  75.      <tr>
  76.        <td>Check In :</td>
  77.        <td><label>
  78.          <?php echo $_SESSION['checkin']; ?>
  79.        </label></td>
  80.       </tr>
  81.      <tr>
  82.        <td>Check Out :</td>
  83.        <td><label><?php echo $_SESSION['checkout']; ?></label></td>
  84.       </tr>
  85.      <tr>
  86.        <td>Rooms :</td>
  87.        <td><label><?php echo $_SESSION['rooms']; ?></label></td>
  88.       </tr>
  89.      <tr>
  90.        <td>Adults Per Room :</td>
  91.        <td><label><?php echo $_SESSION['adults']; ?></label></td>
  92.       </tr>
  93.      <tr>
  94.        <td>Children Per Room :</td>
  95.        <td><label><?php echo $_SESSION['children']; ?></label></td>
  96.       </tr>
  97.      <tr>
  98.        <td>Days :</td>
  99.        <td><?php echo $_SESSION['days']; ?></td>
  100.      </tr>
  101.    </table>
  102.    <p>
  103.     <label></label>
  104.    </form>
  105.   </body>
  106. </html>
  107.  
  108.  
below is the code for : DisplayDetails.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. session_start();
  4. $_SESSION['days']= $_POST['days'];
  5. $_SESSION['room_price']= $_POST['room_price'];
  6. $_SESSION['room_type']= $_POST['room_type'];
  7.  
  8. // echo variable from the session, we set this on our other page
  9. //$_SESSION['checkin'] = $_POST['checkin'];
  10. //$_SESSION['checkout'] = $_POST['checkout'];
  11. //$_SESSION['rooms']= $_POST['rooms'];
  12. //$_SESSION['adults']= $_POST['adults'];
  13. //$_SESSION['children']= $_POST['children'];
  14.  
  15. ?>
  16.  
  17. <html>
  18. <body>
  19. <h3><center>
  20.    Room's Reservation
  21. </center></h3>
  22. <form action="DisplayDetails.php" method="post">
  23.  
  24.   <table width="373" border="1">
  25.     <tr>
  26.       <td colspan="2"><strong>Reservation Summary</strong></td>
  27.     </tr>
  28.     <tr>
  29.       <td>Check In :</td>
  30.       <td><label> <?php echo $_SESSION['checkin']; ?> </label></td>
  31.     </tr>
  32.     <tr>
  33.       <td>Check Out :</td>
  34.       <td><label><?php echo $_SESSION['checkout']; ?></label></td>
  35.     </tr>
  36.     <tr>
  37.       <td>Rooms :</td>
  38.       <td><label><?php echo $_SESSION['rooms']; ?></label></td>
  39.     </tr>
  40.     <tr>
  41.       <td>Adults Per Room :</td>
  42.       <td><label><?php echo $_SESSION['adults']; ?></label></td>
  43.     </tr>
  44.     <tr>
  45.       <td>Children Per Room :</td>
  46.       <td><label><?php echo $_SESSION['children']; ?></label></td>
  47.     </tr>
  48.     <tr>
  49.       <td>Days :</td>
  50.       <td><?php echo $_SESSION['days']; ?></td>
  51.     </tr>
  52.     <tr>
  53.       <td>Room Type</td>
  54.       <td><?php echo $_SESSION['room_type']; ?></td>
  55.     </tr>
  56.     <tr>
  57.       <td>Room Price</td>
  58.       <td><?php echo $_SESSION['room_price']; ?></td>
  59.     </tr>
  60.   </table>
  61. </form>
  62.  
  63. </body>
  64. </html>
  65.  
  66.  
any kinds of help really apprecited
Mar 20 '10 #1
Share this Question
Share on Google+
3 Replies


P: 17
i try to trace the output based on the session using this cod :
Expand|Select|Wrap|Line Numbers
  1.  
  2. echo "<pre>";
  3. var_dump($_SESSION);
  4. echo "</pre>"; 
  5.  
  6.  
it produce that this kind of result:
Expand|Select|Wrap|Line Numbers
  1.  
  2. array(8) {
  3.   ["checkin"]=>
  4.   string(12) "     2010-03-01"
  5.   ["checkout"]=>
  6.   string(12) "     2010-03-24"
  7.   ["rooms"]=>
  8.   string(1) "1"
  9.   ["adults"]=>
  10.   string(1) "1"
  11.   ["children"]=>
  12.   string(1) "1"
  13.   ["days"]=>
  14.   int(23)
  15.   ["room_price"]=>
  16.   NULL
  17.   ["room_type"]=>
  18.   NULL
  19. }
  20.  
  21.  
  22.  
it shows that my room_type and room_price which is the data that i wanted to take from the query at the page findroom.php is NULL. i wonderi if i'm using the right code to take the data from query is right or not?
can someone tell me is this sysntax is right or not?

Expand|Select|Wrap|Line Numbers
  1.  
  2. $_SESSION['room_price']=$data['room_price'];
  3. $room_price=$_SESSION['room_price'];
  4.  
  5. $_SESSION['room_type']=$data['room_type'];
  6. $room_type=$_SESSION['room_type'];
  7.  
  8.  
or can someone tell me is it actually possible to take the data from the query and the make it as session variable?
Mar 20 '10 #2

Atli
Expert 5K+
P: 5,058
Hey.

When you set the session variables for the room_type and room_price variables, the data has not yet been fetched from the database. - You can not use variables before they are created. -- If you want to set the session variables based on data from a SQL query, you need to do so AFTER the query has executed and the data has been retrieved from the result set.

Also, your query is not only fetching values for a single room. It fetches a range (or at least appears to do so) and displayes multiple rooms, from which your visitors can select one via a link. - How do you plan on passing the values for the selected room in the session? -- Keep in mind that once the links are shown to the visitor, the PHP code (including the code that sets the session values) has already been executed.

Based on my understanding of what you are doing, I would recommend passing just the room ID to the second page and retrieving the data again from the database there. It is a lot better than cluttering the session with the entire result set. - Abusing the sessions is never a good idea.
Mar 20 '10 #3

P: 17
Yeah..i finally realize that the prob comes from the wrongly flow of my code.I'm really make myself like fool where i look back at my code. There's eally no logic there. However, i alreaddy fix it..

Since i'm newbie(well...everybody said so..:)), i just thinks the way to pass data without even thinking any logic in the system. I finally sets id at the url at the that will redirect to the DisplayDetails.php page where by setting it at the link "Book Now".

Thanks a lot Atli. Your reply actually DID knocked great sense to my thinking about how to improve my system...

cheers..:)
Mar 20 '10 #4

Post your reply

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