leetstar Posted October 25, 2006 Share Posted October 25, 2006 [code] $dayum = range($day, $day2); for($i = $day; $i < $day2; ++$i) { ${'d' . $i } = $dayum[$i]; }if ($dayum[0] != '') { dbAdd($villa,$month,$dayum[0],$year); } //day 1elseif ($dayum[1] != '') { dbAdd($villa,$month,$dayum[1],$year); } //day 2elseif ($dayum[2] != '') { dbAdd($villa,$month,$dayum[2],$year); } //day 3elseif ($dayum[3] != '') { dbAdd($villa,$month,$dayum[3],$year); } //day 4elseif ($dayum[4] != '') { dbAdd($villa,$month,$dayum[4],$year); } //day 5elseif ($dayum[5] != '') { dbAdd($villa,$month,$dayum[5],$year); } //day 6elseif ($dayum[6] != '') { dbAdd($villa,$month,$dayum[6],$year); } //day 7elseif ($dayum[7] != '') { dbAdd($villa,$month,$dayum[7],$year); } //day 8elseif ($dayum[8] != '') { dbAdd($villa,$month,$dayum[8],$year); } //day 9elseif ($dayum[9] != '') { dbAdd($villa,$month,$dayum[9],$year); } //day 10else { echo "Unknown error."; }}[/code]I`m using the following code to input data from an array into a SQL database. The code should work as follows:If position 1 of the array dayum has something in it, then call the function dbAdd, and write it to the database. For some reason, it`s only writting the first position, and ignoring the others.Thanks,jeff Quote Link to comment https://forums.phpfreaks.com/topic/25044-php-if-statement-not-working-help-needed/ Share on other sites More sharing options...
bbaker Posted October 25, 2006 Share Posted October 25, 2006 because if the first position is true it will ingore all the "elseif" conditions. if you change all the elseif's to if's it should do it correctly. Quote Link to comment https://forums.phpfreaks.com/topic/25044-php-if-statement-not-working-help-needed/#findComment-114162 Share on other sites More sharing options...
leetstar Posted October 25, 2006 Author Share Posted October 25, 2006 It was originaly set to an if, but I`ll try again and post back. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/25044-php-if-statement-not-working-help-needed/#findComment-114163 Share on other sites More sharing options...
leetstar Posted October 25, 2006 Author Share Posted October 25, 2006 [code] $dayum = range($day, $day2); for($i = $day; $i < $day2; ++$i) { ${'d' . $i } = $dayum[$i]; }if ($dayum[0] != '') { dbAdd($villa,$month,$dayum[0],$year); } //day 1if ($dayum[1] != '') { dbAdd($villa,$month,$dayum[1],$year); } //day 2if ($dayum[2] != '') { dbAdd($villa,$month,$dayum[2],$year); } //day 3if ($dayum[3] != '') { dbAdd($villa,$month,$dayum[3],$year); } //day 4if ($dayum[4] != '') { dbAdd($villa,$month,$dayum[4],$year); } //day 5if ($dayum[5] != '') { dbAdd($villa,$month,$dayum[5],$year); } //day 6if ($dayum[6] != '') { dbAdd($villa,$month,$dayum[6],$year); } //day 7if ($dayum[7] != '') { dbAdd($villa,$month,$dayum[7],$year); } //day 8if ($dayum[8] != '') { dbAdd($villa,$month,$dayum[8],$year); } //day 9if ($dayum[9] != '') { dbAdd($villa,$month,$dayum[9],$year); } //day 10} [/code]Still, the code only writes the value assigned at position 0 of dayum.:(Jeff. Quote Link to comment https://forums.phpfreaks.com/topic/25044-php-if-statement-not-working-help-needed/#findComment-114168 Share on other sites More sharing options...
TecBrat Posted October 25, 2006 Share Posted October 25, 2006 check to make sure your $daynum[1]... actually have values. Like this:[code]if ($dayum[0] != '') { dbAdd($villa,$month,$dayum[0],$year); } //day 1echo ("Daynum[1] is: $daynum[1]");if ($dayum[1] != '') { dbAdd($villa,$month,$dayum[1],$year); } //day 2 [/code] Quote Link to comment https://forums.phpfreaks.com/topic/25044-php-if-statement-not-working-help-needed/#findComment-114181 Share on other sites More sharing options...
kenrbnsn Posted October 25, 2006 Share Posted October 25, 2006 Try changing your code to use a loop:[code]<?phpfor ($i=0;$i<10;$i++) if ($dayum[$i] != '') dbAdd($villa,$month,$dayum[$i],$year); else echo '$dayum[' . $i . '] is empty<br>';?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/25044-php-if-statement-not-working-help-needed/#findComment-114274 Share on other sites More sharing options...
leetstar Posted October 25, 2006 Author Share Posted October 25, 2006 no luck :(I moved around the code abit, I decided to post the entire code so you guys can have a better look:[code]<?//includesinclude("config.php");//uses addional vars grabbed from the form, such as and $date$month = $_POST['select_month'];$year = $_POST['select_year'];$villa = $_POST['select_villa'];//converts date$day = date("z", mktime(0, 0, 0, $month, $date, $year)) + 1;$day2 = date("z", mktime(0, 0, 0, $month, $date2, $year)) + 1;//add date to db$difference = $day2 - day;if ($difference = 1){ dbAdd($villa,$month,$day,$year);} elseif ($difference > 10){ echo "Sorry, you can only book off 10 days at a time."; exit;} elseif ($difference < 10){ echo "Sorry, the 'date to' field must be bigger then the 'date' filed."; exit;}elseif ($difference > 1){ $dayum = range($day, $day2); for($i = $day; $i < $day2; ++$i) { ${'d' . $i } = $dayum[$i]; for ($i=0;$i<10;$i++) if ($dayum[$i] != '') dbAdd($villa,$month,$dayum[$i],$year); else echo '$dayum[' . $i . '] is empty<br>';}} else { echo "wtf"; }//echo successecho "Successful!";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25044-php-if-statement-not-working-help-needed/#findComment-114393 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 OK, two pointers for you...1. [code=php:0]if ($difference = 1){[/code] should actually be [code=php:0]if ($difference == 1){ // notice 2 equals signs[/code].2. Your forth condition [code=php:0]elseif ($difference > 1){[/code] is only ever going to be true when the difference is 10, as the other 3 conditions take care of everything else.e.g The loop exits when the condition is true, so lets see which values equate to true on which part of the loop...[code]Difference1 if ($difference == 1) True on 1st condition 2 elseif ($difference < 10) True on 3rd condition3 elseif ($difference < 10) True on 3rd condition4 elseif ($difference < 10) True on 3rd condition5 elseif ($difference < 10) True on 3rd condition6 elseif ($difference < 10) True on 3rd condition7 elseif ($difference < 10) True on 3rd condition8 elseif ($difference < 10) True on 3rd condition9 elseif ($difference < 10) True on 3rd condition10 elseif ($difference > 1) True on 4th condition - The only one11 elseif ($difference > 10) True on 2nd condition12 elseif ($difference > 10) True on 2nd condition13 elseif ($difference > 10) True on 2nd condition. elseif ($difference > 10) True on 2nd condition etc. etc. etc.[/code]Might want to rethink that loop structure slightly.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25044-php-if-statement-not-working-help-needed/#findComment-114508 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.