Gotharious Posted September 17, 2011 Share Posted September 17, 2011 Hello All, I'm working on this project, and everything was great but the last part is screwed. The procedure is supposed to be, you choose a hotel ---> book a room ---> confirm the booking and enter your details --> get redirected to paypal Everything is fine except for the confirmation part, where you're supposed to confirm the room you booked, number of nights and so on, all the info is right, except for the booked room, where it should show you the price for the room multiplied by the number of night... instead it gives "Total Price = 0" So anyone can give me an example on how can I make it show the result of multiplying 2 inputs by a user? Variables are $nrooms $newdate Here is the code I wrote <?php $dbo = new DB(); $hotelObj = new hotelManager(); $hotelID = $_POST['hotelid']; $datein = $_POST['datein']; $dateout = $_POST['dateout']; $roomid = $_POST['roomid']; $roonsNo = $_POST['roomsNo']; $pr = $_POST['pr']; $_SESSION['hotelID'] = isset($_POST['hotelid']) ? $_POST['hotelid'] : $_SESSION['hotelID']; $_SESSION['datein'] = isset($_POST['datein']) ? $_POST['datein'] : $_SESSION['datein']; $_SESSION['dateout'] = isset($_POST['dateout']) ? $_POST['dateout'] : $_SESSION['dateout']; $_SESSION['roomid'] = isset($_POST['roomid']) ? $_POST['roomid'] : $_SESSION['roomid']; $_SESSION['nrooms'] = $_POST['nrooms']; $roomsarray = explode(",",$_POST['roomid']); $_SESSION['roomsarray'] = isset($_POST['roomid']) ? explode(",",$_POST['roomid']) : $_SESSION['roomsarray']; $roonsNo = $_POST['nrooms']; $_SESSION['nrooms'] = isset($_POST['roomnum']) ? explode(",",$_POST['roomnum']) : $_SESSION['nrooms']; /********************** hotels ************************/ /******************************************************/ echo "<table width=95% border=0 align=\"center\" cellpadding=\"0\" cellspacing=\"0\"> <tr><td valign=\"top\">"; echo "<table>"; echo "<tr>"; echo "<td valign=top>"; $imageqry=mysql_query("SELECT * FROM `hotelphotos` where hotel_id='".$_SESSION['hotelID']."' LIMIT 1"); $image=mysql_fetch_array($imageqry); $imagename=$image['attachmentName']; echo "<img src=\"foxmaincms/webroot/files/small/$imagename\"/>"; echo "</td>"; echo "<td>"; $result=$hotelObj->getHotelbyID($_SESSION['hotelID']); $row = mysql_fetch_array($result); echo "<table>"; echo "<tr><td valign=top><strong class=subtitle3>".$row['name']."</strong></td></tr>"; echo "<tr><td class=text valign=top>".$row['location']."</td></tr>"; echo "<tr><td class=text valign=top>check-in Date: ".$_SESSION['datein']."</td></tr>"; echo "<tr><td class=text valign=top>check-out Date: ".$_SESSION['dateout']."</td></tr>"; echo "<tr><td class=text valign=top>"; $newdate = $_SESSION['dateout'] - $_SESSION['datein']; $totalprice = $nrooms * $newdate; echo "</td></tr>"; echo "<tr><td class=text valign=top>Total Price: ".$totalprice."</td></tr>"; echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/247312-need-help-multiplying-2-inputs/ Share on other sites More sharing options...
jcbones Posted September 17, 2011 Share Posted September 17, 2011 You cannot multiply an integer with a date like you are doing. You will have to get the number of nights. $newdate = (int)(strtotime($_SESSION['dateout']) - strtotime($_SESSION['datein'])) / 86400; $totalprice = $nrooms * $newdate; Quote Link to comment https://forums.phpfreaks.com/topic/247312-need-help-multiplying-2-inputs/#findComment-1270155 Share on other sites More sharing options...
Gotharious Posted September 17, 2011 Author Share Posted September 17, 2011 Did that, but still nothing changed, I'm guessing that the $nrooms actually = 0 that's why when it's multiplied it equals 0, but I can't seem to find where is the error in all of this Quote Link to comment https://forums.phpfreaks.com/topic/247312-need-help-multiplying-2-inputs/#findComment-1270156 Share on other sites More sharing options...
jcbones Posted September 17, 2011 Share Posted September 17, 2011 You need to check your post array: echo '<pre>' . print_r($_POST,true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/247312-need-help-multiplying-2-inputs/#findComment-1270247 Share on other sites More sharing options...
Gotharious Posted September 17, 2011 Author Share Posted September 17, 2011 I tried booking 3 rooms and here is the result check-in Date: 18 Sep 2011 check-out Date: 19 Sep 2011 Total Price: 0 Array ( [roomid] => [roomnum] => [hotelid] => 13 [datein] => 18 Sep 2011 [dateout] => 19 Sep 2011 [nrooms] => 3 [bookroom] => book ) Quote Link to comment https://forums.phpfreaks.com/topic/247312-need-help-multiplying-2-inputs/#findComment-1270251 Share on other sites More sharing options...
jcbones Posted September 17, 2011 Share Posted September 17, 2011 The problem is your date format, strtotime() will not accept that particular format, but we can work around that. Try: $dateout = explode(' ',$_SESSION['dateout']); $datein = explode(' ',$_SESSION['datein']); $newdate = (int)(strtotime($dateout[1] . '-' . $dateout[0] . '-' . $dateout[2] ) - strtotime($datein[1] . '-' . $datein[0] . '-' . $datein[2])) / 86400; $totalprice = $nrooms * $newdate; strtotime() will either accept "18 September 2011" or "Sep-18-2011" Quote Link to comment https://forums.phpfreaks.com/topic/247312-need-help-multiplying-2-inputs/#findComment-1270252 Share on other sites More sharing options...
Gotharious Posted September 18, 2011 Author Share Posted September 18, 2011 Tried that, but still no changes at all check-in Date: 19 Sep 2011 check-out Date: 20 Sep 2011 Total Price: 0 Array ( [roomid] => [roomnum] => [hotelid] => 13 [datein] => 19 Sep 2011 [dateout] => 20 Sep 2011 [nrooms] => 2 [bookroom] => book ) Quote Link to comment https://forums.phpfreaks.com/topic/247312-need-help-multiplying-2-inputs/#findComment-1270360 Share on other sites More sharing options...
jcbones Posted September 18, 2011 Share Posted September 18, 2011 With the code provided, I get a return of 2 (totalPrice). Which would be nrooms (2) x number of nights (1). Nowhere have you multiplied by the price of the room. Here is my test script: <?php $_POST['datein'] = '19 Sep 2011'; $_POST['dateout'] = '20 Sep 2011'; $nrooms = 2; $dateout = explode(' ',$_POST['dateout']); $datein = explode(' ',$_POST['datein']); $newdate = (int)(strtotime($dateout[1] . '-' . $dateout[0] . '-' . $dateout[2] ) - strtotime($datein[1] . '-' . $datein[0] . '-' . $datein[2])) / 86400; echo $newdate .'<br />'; $totalprice = $nrooms * $newdate; echo $totalprice; ?> Output: 1 2 Quote Link to comment https://forums.phpfreaks.com/topic/247312-need-help-multiplying-2-inputs/#findComment-1270476 Share on other sites More sharing options...
Gotharious Posted September 19, 2011 Author Share Posted September 19, 2011 Ok here are all the variables in BookinManager.php <?php include("hotelsManager.php"); ?> <?php include("config.php"); ?> <?php include("textManager.php"); ?> <?php function echoPostedData() { $dbo = new DB(); $hotelObj = new hotelManager(); $hotelID = $_POST['hotelid']; $roomid = $_POST['roomid']; $roonsNo = $_POST['roomsNo']; $pr = $_POST['pr']; $_SESSION['hotelID'] = isset($_POST['hotelid']) ? $_POST['hotelid'] : $_SESSION['hotelID']; $_SESSION['datein'] = isset($_POST['datein']) ? $_POST['datein'] : $_SESSION['datein']; $_SESSION['dateout'] = isset($_POST['dateout']) ? $_POST['dateout'] : $_SESSION['dateout']; $_SESSION['roomid'] = isset($_POST['roomid']) ? $_POST['roomid'] : $_SESSION['roomid']; $_SESSION['nrooms'] = $_POST['nrooms']; $roomsarray = explode(",",$_POST['roomid']); $_SESSION['roomsarray'] = isset($_POST['roomid']) ? explode(",",$_POST['roomid']) : $_SESSION['roomsarray']; $roonsNo = $_POST['nrooms']; $_SESSION['nrooms'] = isset($_POST['roomnum']) ? explode(",",$_POST['roomnum']) : $_SESSION['nrooms']; And here is the Input page hotelsmanager.php public function getHotelRooms($hotelID) { $result = mysql_query("select * from rooms where hotel_id = '$hotelID'"); echo "<form name=\"bookingform\" id=\"bookingform\" method=\"post\" action=\"book.php\">"; echo "<input name=\"roomid\" id=\"roomid\" type=\"hidden\">"; echo "<input name=\"roomnum\" id=\"roomnum\" type=\"hidden\">"; echo "<input name=\"hotelid\" id=\"hotelid\" type=\"hidden\" value=\"$hotelID\"/>"; echo "<table width=80% >"; echo"<tr>"; echo "<td>Check-in date</td>"; echo "<td>"; echo "<input type=\"text\" name=\"datein\" class=\"date_input\" value=\"$datein\" />"; echo "</td>"; echo "<td>Check-out date</td>"; echo "<td>"; echo "<input type=\"text\" name=\"dateout\" class=\"date_input\" value=\"$dateout\" />"; echo "</td>"; echo"</tr>"; echo "<table>"; echo "<table class=\"rooms\" width=100% cellspacing=\"1\" >"; echo "<tr> <th class=\"rooms\">Room Type</th> <th class=\"rooms\">Rate for night</th> <th class=\"rooms\">MAX</th> <th class=\"rooms\">Nr.rooms</th> <th class=\"rooms\">Book</th> </tr>"; while($row = @mysql_fetch_array($result)) { echo "<tr>"; echo "<td class=\"text\" align=center>".$row['room_type']."</td>"; echo "<td class=\"text\" align=center>".$row['price_per_day']."</td>"; echo "<td class=\"text\" align=center>".$row['people']." People</td>"; echo "<td class=\"text\" align=center>"; ?> <select name="nrooms" id="nrooms" onchange=""> //alert('<?php echo $row['id'] ; ?>'); //alert(this.value); var exist = 0; if(roomids.length > 0) { for(var hh = 0;hh < roomids.length; hh++) { if(roomids[hh] == <?php echo $row['id'] ; ?>) { exist = 1; //alert(hh); roomnumar[hh] = this.value; } } if(exist == 0) { roomids.push(<?php echo $row['id'] ; ?>); roomnumar.push(this.value); } } else { roomids.push(<?php echo $row['id'] ; ?>); roomnumar.push(this.value); } document.bookingform.roomid.value = roomids; document.bookingform.roomnum.value = roomnumar; "> <option value=0> 0 </option> <?php for($i = 0; $i < $row['available_rooms']; $i++) { $nr = $i+1; $pr=$nr * $row['price_per_day']; echo "<option value=$nr>$nr ($pr\$)</option>"; } echo "</select> </td>"; echo "<td align=center> </td>"; echo "</tr>"; echo "<tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr>"; } echo "<tr class=\"rooms\"> <td colspan=\"4\"> </td> <td align=center><input type=\"submit\" value=\"book\" id=\"bookroom\" name=\"bookroom\"/></td> </tr>"; echo "</table>"; echo "</form>"; //print_r($_SESSION['order']); //$_SESSION['order']=0; //unset($_SESSION['order']); } } Quote Link to comment https://forums.phpfreaks.com/topic/247312-need-help-multiplying-2-inputs/#findComment-1270668 Share on other sites More sharing options...
Gotharious Posted September 21, 2011 Author Share Posted September 21, 2011 anyone....??? Quote Link to comment https://forums.phpfreaks.com/topic/247312-need-help-multiplying-2-inputs/#findComment-1271369 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.