peuge Posted August 24, 2008 Share Posted August 24, 2008 Hi all, I have a booking form and am sending data entered by the user and also collected from a database about availability. On my page where I insert the data into the db I have the following code (and more but that's working fine) The code below is just for testing purposes so I know what is and what is not being sent. //This displays the number of available bikes that are sent to the booked.php page echo '<br/>The available bikes that are sent <br/>'; $avail1 = urldecode($avail); $available = unserialize($avail1); foreach ($available as $jim){ echo $jim.'<br/>'; } //This displays the number of bikes the user selected and is sent to the booked.php page echo 'Them amnt that is sent: '.$amnt.'<br/>'; //This is where I am going to have my SQL insert statement. for ($i=0;$i<$amnt;$i++){ echo $available[$i]; echo '<br/>';} I am not getting any errors or anything. The problem is that when there are already bookings and I choose to take 1 bike for rental the for loop above does not work and does not show anything. Everything above the for loop works fine all the time. If I choose to take more than 1, for example 3 bikes, then it only displays two bikes (in the for loop). Now if there are no bookings (i.e. all the bikes are available) then it works absolutely fine. It has nothing to do with my SQL queries as it is getting the right data, I can see that because of what it is showing me from above. For some reason it seems that when there are no bookings that $available[0] exists whereas when there are bookings $available[0] does not exist? Any help much appreciated Thnks Quote Link to comment https://forums.phpfreaks.com/topic/121100-solved-wierd-prob-with-arrays/ Share on other sites More sharing options...
sasa Posted August 24, 2008 Share Posted August 24, 2008 can you print_r($available); to see what is in it Quote Link to comment https://forums.phpfreaks.com/topic/121100-solved-wierd-prob-with-arrays/#findComment-624305 Share on other sites More sharing options...
peuge Posted August 24, 2008 Author Share Posted August 24, 2008 It gives the value of the array Array ( [1] => CA789456 [2] => CA654321) It does that for if I have no bookings and if I do. And it is giving the right values and the right amount of values. Those values are registration plate numbers. Quote Link to comment https://forums.phpfreaks.com/topic/121100-solved-wierd-prob-with-arrays/#findComment-624310 Share on other sites More sharing options...
peuge Posted August 24, 2008 Author Share Posted August 24, 2008 Ok so just realised now when there are no booking (i.e. all bikes available) then it displays array([0] => CA789465.... etc Otherwise is starts on Array[1] Quote Link to comment https://forums.phpfreaks.com/topic/121100-solved-wierd-prob-with-arrays/#findComment-624315 Share on other sites More sharing options...
peuge Posted August 25, 2008 Author Share Posted August 25, 2008 So I have figured out this is where the problem lies. I give available the values with this $available = array_diff($bikes,$booked); I use that when there are bookings. When I print_r that it only starts at array[1]? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/121100-solved-wierd-prob-with-arrays/#findComment-624667 Share on other sites More sharing options...
peuge Posted August 25, 2008 Author Share Posted August 25, 2008 Ok figured it out. When using array_diff it retains the original keys. Quote Link to comment https://forums.phpfreaks.com/topic/121100-solved-wierd-prob-with-arrays/#findComment-624677 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.