Jump to content

Archived

This topic is now archived and is closed to further replies.

leetstar

PHP If statement not working... Help needed!

Recommended Posts

[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 1
elseif ($dayum[1] != '') { dbAdd($villa,$month,$dayum[1],$year); } //day 2
elseif ($dayum[2] != '') { dbAdd($villa,$month,$dayum[2],$year); } //day 3
elseif ($dayum[3] != '') { dbAdd($villa,$month,$dayum[3],$year); } //day 4
elseif ($dayum[4] != '') { dbAdd($villa,$month,$dayum[4],$year); } //day 5
elseif ($dayum[5] != '') { dbAdd($villa,$month,$dayum[5],$year); } //day 6
elseif ($dayum[6] != '') { dbAdd($villa,$month,$dayum[6],$year); } //day 7
elseif ($dayum[7] != '') { dbAdd($villa,$month,$dayum[7],$year); } //day 8
elseif ($dayum[8] != '') { dbAdd($villa,$month,$dayum[8],$year); } //day 9
elseif ($dayum[9] != '') { dbAdd($villa,$month,$dayum[9],$year); } //day 10
else { 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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 1
if ($dayum[1] != '') { dbAdd($villa,$month,$dayum[1],$year); } //day 2
if ($dayum[2] != '') { dbAdd($villa,$month,$dayum[2],$year); } //day 3
if ($dayum[3] != '') { dbAdd($villa,$month,$dayum[3],$year); } //day 4
if ($dayum[4] != '') { dbAdd($villa,$month,$dayum[4],$year); } //day 5
if ($dayum[5] != '') { dbAdd($villa,$month,$dayum[5],$year); } //day 6
if ($dayum[6] != '') { dbAdd($villa,$month,$dayum[6],$year); } //day 7
if ($dayum[7] != '') { dbAdd($villa,$month,$dayum[7],$year); } //day 8
if ($dayum[8] != '') { dbAdd($villa,$month,$dayum[8],$year); } //day 9
if ($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.

Share this post


Link to post
Share on other sites
check to make sure your $daynum[1]... actually have values. Like this:
[code]
if ($dayum[0] != '') { dbAdd($villa,$month,$dayum[0],$year); } //day 1
echo ("Daynum[1] is: $daynum[1]");
if ($dayum[1] != '') { dbAdd($villa,$month,$dayum[1],$year); } //day 2 [/code]

Share this post


Link to post
Share on other sites
Try changing your code to use a loop:
[code]<?php
for ($i=0;$i<10;$i++)
    if ($dayum[$i] != '') dbAdd($villa,$month,$dayum[$i],$year);
    else echo '$dayum[' . $i . '] is empty<br>';
?>[/code]

Ken

Share this post


Link to post
Share on other sites
no luck :(

I moved around the code abit, I decided to post the entire code so you guys can have a better look:
[code]
<?
//includes
include("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 success
echo "Successful!";
?>
[/code]

Share this post


Link to post
Share on other sites
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]
Difference
1    if ($difference == 1)        True on 1st condition
2    elseif ($difference < 10)    True on 3rd condition
3    elseif ($difference < 10)    True on 3rd condition
4    elseif ($difference < 10)    True on 3rd condition
5    elseif ($difference < 10)    True on 3rd condition
6    elseif ($difference < 10)    True on 3rd condition
7    elseif ($difference < 10)    True on 3rd condition
8    elseif ($difference < 10)    True on 3rd condition
9    elseif ($difference < 10)    True on 3rd condition
10    elseif ($difference > 1)      True on 4th condition - The only one
11    elseif ($difference > 10)      True on 2nd condition
12    elseif ($difference > 10)      True on 2nd condition
13    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.

Regards
Huggie

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.