Jump to content


Photo

PHP If statement not working... Help needed!


  • Please log in to reply
7 replies to this topic

#1 leetstar

leetstar
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 25 October 2006 - 12:29 PM

	$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."; }}

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

#2 bbaker

bbaker
  • Members
  • PipPipPip
  • Advanced Member
  • 127 posts
  • LocationNY

Posted 25 October 2006 - 12:44 PM

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.

#3 leetstar

leetstar
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 25 October 2006 - 12:45 PM

It was originaly set to an if, but I`ll try again and post back. Thanks.

#4 leetstar

leetstar
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 25 October 2006 - 12:50 PM

	$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
}	

Still, the code only writes the value assigned at position 0 of dayum.
:(
Jeff.

#5 TecBrat

TecBrat
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 25 October 2006 - 01:05 PM

check to make sure your $daynum[1]... actually have values. Like this:
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

It's kinda lame at the moment, but...
PHP MySQL Playground

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 October 2006 - 04:19 PM

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

Ken

#7 leetstar

leetstar
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 25 October 2006 - 07:24 PM

no luck :(

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


#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 11:29 PM

OK, two pointers for you...

1.
if ($difference = 1){
should actually be
if ($difference == 1){ // notice 2 equals signs
.

2. Your forth condition
elseif ($difference > 1){
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...
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.

Might want to rethink that loop structure slightly.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users