PHP - How To Write (if And Elseif) Multi
Hello friends,
how can i combain this codes If i've 3 functions $limited,$refere,$pass all have commen result echo "good"; if($limited){ if($hits >= $limited){ echo "U1"; }else{ echo "Good"; } } else { echo "Good"; } ================================= if($refere){ if($me >= $you){ echo "U2"; }else{ echo "Good"; } } else { echo "Good"; } ================================= if($pass){ if($she >= $he){ echo "U3"; }else{ echo "Good"; } } else { echo "Good"; } thansk, it would really helps me alot Similar Tutorials<?php $a = 2; $b = 3; $z = fopen("test.txt", 'a+'); if($a > $b) { echo "A is bigger than B"; } elseif fwrite($z, $b); fclose($z); ?> is there a way to get elseif to write to a file? so if b is bigger than a it writes $b to a file? So let's assume i'm making a script for searching cars by different criteria. Name of the car, color, and mileage. I used to do this by using LIKE and just adding AND for as many times as needed. But i've read that that is obsolete and is giving servers a hard time, so i went ahead and started making a new script that will use fulltext search. I've hit a dead end. Code: [Select] function search($table,$what,$string,$limit,$start,$country) { global $totrows; global $pages; $string = explode("*",$string); $what = explode("*",$what); $cname = trim($string[0]); $t = 0; $parameters = ""; while (isset($what[$t])){ if ($t>0) { $paramm .= ", "; } $paramm .= trim($what[$t]); if ($t>0) { $parama .= " "; } $parama .= trim($string[$t]); $t++; } $parameters .= " AND locationc = '".$country."'"; $sql = "SELECT *, MATCH(".$paramm.") AGAINST('".$parama."') AS score FROM main WHERE MATCH(".$paramm.") AGAINST('".$parama."') ORDER BY score DESC"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { var_dump($row); } } How does this work: The user selects and enters his parameters> The script then joins them all into a single variable which is then sent to the function(the reason i do this i because the user might input only the name, so that way i can add more criteria and not need to change the script)> The script explodes those into arrays and then it processes them into a form for use with sql> I run the sql query and then return the results. What's the problem? There's two of them actually> The first one is that mysql returns an error "Can't find FULLTEXT index matching the column list" even though i did setup fulltext indexes. (phpmyadmin c/p) Code: [Select] name FULLTEXT No No name 0 YES color FULLTEXT No No color 0 YES mileage FULLTEXT No No mileage 0 YES The second problem is that the script is not selective and will not work as intended. For example, a car's name is 300, the users input's 300, and the script will return those rows that had mileage 300 or 300000 or whatever. How do i fix this, and is FULLTEXT the right way to go with multi criteria search? the following is the contents of my index.php file if (isset($_GET['topic'])) { include 'modules/pages/posts.php'; } elseif (isset($_GET['forum'])) { include 'modules/pages/topics.php'; } elseif (isset($_GET['parent'])) { include 'modules/pages/parents.php'; } elseif (isset($_GET['do']) == 'unanswered') { include 'modules/pages/unanswered.php'; } elseif (isset($_GET['do']) == 'today') { include 'modules/pages/today.php'; } else { include 'modules/pages/forums.php'; } as you can see depending on what the get value is it loads a corrosponding page. however on the last 2 statements it loads the same page index.php?do=unanswered. why wont it load index.php?do=today when the $_GET value == today? Hi, I can't see for looking on quite simply a very easy statement; Code: [Select] $cartTotal = '900'; echo 'cart total '.$cartTotal.'<br/>'; $postage = '100'; echo 'Postage '.$postage.'<br/>'; $postThreshold = '85'; echo 'Post Threshold '.$postThreshold.'<br/>'; if ($cartTotal <= $postThreshold) { echo 'too little'; } elseif (($cartTotal >= $postThreshold) || ($cartTotal <= $postage)) { echo 'difference'; } else { echo 'over a ton'; }It works apart from if the cart total is above 100. What have I missed? Thanks I am a novice and had this code below working fine until I added the If / elseif to "plan_id". Prior, the column would just list out the numbers from the database, but I want the numbers to echo out the text. Any help is appreciated. while($row = mysql_fetch_array($result)){ $bg = ($bg=='#ffffff' ? '#e7e8e8' : '#ffffff'); //For Alternate Row Colors echo '<tr bgcolor="' . $bg . '"><td>' . $row['customers_firstname'] . " " . $row['customers_lastname'] . '</td><td>' . date("M. d, Y", strtotime($row['date'])) . '</td><td>' . if ($row['plan_id'] == 1) { echo "1 DVD"; } elseif ($row['plan_id'] == 2) { echo "2 DVDs"; } else { echo "3 DVDs"; } . '</td></tr>';} echo '</table>'; echo '<br />'; is there a better way of coding this using arrays? ive never had a use for them b4 the if and the else statment is ok but its the elseif that i think i can put into an array ut what i tryed didnt work. what id like to do Code: [Select] $loc = array('HDD', 'APRIL 2011', 'WHITE VISTA', 'NEW 1.5 TB', 'UNVALIDATED') elseif($rows['auth'] == 'YES' and $rows['location'] == $loc){ $bgc = "wait" ; what works Code: [Select] if ($rows['auth'] == 'YES' and $rows['location'] == 'SERVER'){ $bgc = "pass" ; } elseif($rows['auth'] == 'YES' and $rows['location'] == 'HDD'){ $bgc = "wait" ; }elseif($rows['auth'] == 'YES' and $rows['location'] == 'APRIL 2011'){ $bgc = "wait" ; }elseif($rows['auth'] == 'YES' and $rows['location'] == 'WHITE VISTA'){ $bgc = "wait" ; }elseif($rows['auth'] == 'YES' and $rows['location'] == 'NEW 1.5 TB'){ $bgc = "wait" ; }elseif($rows['auth'] == 'YES' and $rows['location'] == 'UNVALIDATED'){ $bgc = "wait" ; }else{ $bgc = "fail"; } thanks it should be pretty simple. if member type == Gold or silver echo from a if $member== client echo form b. the query is working as i can echo out $member and display the member type before the if statement. after the if statement no variables will echo out. Gold is with a capital G and silver is not in the database. Code: [Select] $data=mysql_query(" SELECT * FROM members WHERE email='$myusername' and password='$mypassword'")or die (mysql_error()); while ($info=mysql_fetch_array($data)){ $member=$info['member_type']; $email=$info['email']; $frist=$info['fname']; $last=$info['lname']; $country=$info['country']; $add=$info['address']; $state=$info['state']; $zip=$info['zip']; } echo $member; echo $frist; ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>3designbreif.com | Create profile</title> <link rel="stylesheet" type="text/css" href="../css/3lance.css" media="all" /> </head> <body> <div class="content" style="background-image:url(../img/3lancecreatep.png);"> <div id="update"> <?php echo "welcome ".$email." Please Change your password and comfirm details."; echo "<br/>"; ?> <?php if($member=='Gold' || $member=='silver'){ ?> <form action="../php/update.php" method="post"> <div style="clear:both;"></div> <label for='password'>Password</label><input type="password" name="password" /> <div style="clear:both;"></div> <label for='password2'>Comfirm Password</label><input type="password" name="password2" /> <div style="clear:both;"></div> <label for="name">Name</label><input type="text" name="name" value="<?php $frist?>"/> <div style="clear:both;"></div> <label for="lname">Last Name</label><input type="text" name="lname" value="<?php $last?>" /> <div style="clear:both;"></div> <label for="address">Address</label><input type="text" name="address" value="<?php $add?>" /> <div style="clear:both;"></div> <label for="state">State</label><input type="text" name="state" value="<?php $state?>" /> <div style="clear:both;"></div> <label for="country">Country</label><input type="text" name="country" value="<?php $country?>" /> <div style="clear:both;"></div> <label for="zip">Zip</label><input type="text" name="zip"value="<?php $zip?>" /> <div style="clear:both;"></div> <?php } if($member=='client'){ echo $member; ?> <form action="../php/update2.php" method="post"> <div style="clear:both;"></div> <label for="password">Password</label><input type="password" name="password" /> <div style="clear:both;"></div> <label for="password2">Comfirm Password</label><input type="password" name="password2" /> <div style="clear:both;"></div> <label for="name">Name</label><input type="text" name="name" value="<?php echo $frist;?>" /> <div style="clear:both;"></div> <label for="lname">Last Name</label><input type="text" name="lname" value="<?php echo $last;?>" /> <div style="clear:both;"></div> <label for="address">Address</label><input type="text" name="address" value="<?php echo $add;?>" /> <div style="clear:both;"></div> <label for="state">State</label><input type="text" name="state" value="<?php echo $state;?>" /> <div style="clear:both;"></div> <label for="country">Country</label><input type="text" name="country" value="<?php echo $country;?>" /> <div style="clear:both;"></div> <label for="zip">Zip</label><input type="text" name="zip"value="<?php echo $zip;?>" /> <div style="clear:both;"></div> <?php } ?> Hi Guys, I'm not sure what to do here. I have a variable that is passed on via a url. I have a database table that has a field called 'publish'. This field contains either a Yes or a No. All I want to do is check the field an if it is Yes then make it No, and likewise if it is No then make it Yes. Here is my code. It changes one way but not the other. The value of $publish seems to stay the same...please help...Thank you. Code: [Select] <?php // connect to the database include('connect-db.php'); // get id value $id = $_GET['id']; echo "$id"; $publish = $_GET['publish']; echo "$publish"; $sql="SELECT * FROM events WHERE ID='$id'"; $result=mysql_query($sql); if ($publish='Yes') { $publish=='No'; } else { $publish=='Yes'; } // echo "<p>$publish</p>"; $sqltwo = "UPDATE events SET publish='$publish' WHERE ID='$id'"; $resulttwo = mysql_query($sqltwo); // sleep(2); // header("Location: beacon.php"); ?> hi everybody, every time i asked a question here, i got quick and perfect answers. now i am back again with one i have a simple form which updates some fields in a mysql table. everything works fine, am just trying to convert numbers posted through the form into words on the update page and it is not working for example: the form sends score to the update page, it is either 1, 2 or 3 all i want is that if the score on the form page was 1 then it should update the field in the table with the value "Low" if 2 then "Average" if 3 then "High" the reason is that i dont want the user to select 1,2 or 3 and also low, average and high. so i can simply recieve 1,2 or 3 and update 2 fields at the same time. i tried it with elseif, it doesn't work my code is as follows: <? if($_POST['Submit']){ $host="mysql"; $db_user="me"; $db_password="mypassword"; $database="mydb"; mysql_connect($host,$db_user,$db_password); mysql_select_db($database); $result=mysql_query("SELECT id FROM mytable ORDER BY id ASC"); while($row=mysql_fetch_assoc($result)){ $name=$_POST["name_".$row[id]]; $lastname=$_POST["lastname_".$row[id]]; $email=$_POST["email_".$row[id]]; $score=$_POST["score_".$row[id]]; // this is where i don't simply want to update the record // with the $_POST, rather convert the number into aplhabets, i.e. 1=Low, 2=Average and 3=High mysql_query("UPDATE mytable SET name='$name', lastname='$lastname', email='$email', score='$score' where id='$row[id]'"); } echo "--- Update Complete ---"; } ?> i'm having trouble with an elseif statement. its not displaying the result it should ?php if(isset($_COOKIE['ID_forum'])){ $username = $_COOKIE['ID_forum']; $query = "SELECT * FROM users WHERE username = '".$username."'"; $level = mysql_query($query) or die ("Select Error :" . mysql_error()); $userlevel = $level['user_level']; echo $userlevel; if ($userlevel == "1") { echo 'you are a member'; } elseif ($userlevel == "2") { echo 'you are a moderator'; } elseif ($userlevel == "3") { echo 'you are an administrator '; } else echo'you are not logged in'; } ?> it is echoing out 'you are not logged in', when it should be saying 'you are an administrator', as the value in the table is 3. i do know that the cookie 'ID_forum' is there, but i cant find why its not working properly. Any ideas would be great. Thanks I can't get these two if, elseif and else statements too work, here is the code: if(empty($this->password)){ $this->errors[] = 'You must enter a password!'; } elseif(!strlen($this->password) >= 6){ $this->errors[] = 'Your password must be longer than 6 characters!'; } if(empty($this->email)){ $this->errors[] = 'You must enter an email address!'; } elseif(!preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$/',$this->email)){ $this->errors[] = 'Your password can only contain these characters: a-z, A-Z and 0-9!'; } if(empty($this->confemail)){ $this->errors[] = 'Please confirm your email address!'; } elseif(!$this->confemail == $this->email){ $this->errors[] = 'The email addresses you entered did not match!'; } The if and elseif statements for "email" work just fine but the other 2 doesn't. With the "password" i first want to check if the field is empty, and when it is, it works as it should. But when i for example type "test" too check if the password is longer than 6 characters i don't get any message at all? With the "confemail" i want to first check if the field is empty, and when it is, it works as it should. But when i type in "test@test.com" in both fields to try to compare "confemail" to "email" i still get the "Please confirm your email address!" message? Okay, I have a script that if someone is a particular "class", they can't get additional bonus points for their character. However, if they aren't that class, then they get additional points. But, it seems to be ignoring the elseif and gives a strength point no matter what the class is. I don't see where I coded wrong, so I would appreciate any help! Thanks! if ($expgain + $player->mine_exp >= $player->mine_maxexp) { echo "<br /><b>Your mining leveled up!</b>"; echo "$blurb $randeffect<p>"; if(($strength + $agility + $wisdom + $vitality >=100) && ($class != 'Apprentice' || $class != 'Mage' || $class != 'Priest' || $class != 'Rogue' || $class != 'Scholar' || $class != 'Wanderer' || $class != 'Warrior' || $class != 'Woodsman')) { echo "<br /><b>You gained 1 strength point!</b>"; $query = $db->execute("update `users` set `energy`=?, `mine_level`=?, `mine_exp`=?, `minecount`=?, `strength`=?, `mine_maxexp`=? where `id`=?", array($player->energy - 1, $player->mine_level + 1, $player->mine_exp + 1, $player->minecount + 1, $player->strength + 1, $player->mine_maxexp + $newmax, $player->id)); } elseif (($strength + $agility + $wisdom + $vitality >= 100) && ($class = 'Apprentice' || $class = 'Mage' || $class = 'Priest' || $class = 'Rogue' || $class = 'Scholar' || $class = 'Wanderer' || $class = 'Warrior' || $class = 'Woodsman')) { echo "You have already maxed out your stats at 100!"; $query = $db->execute("update `users` set `energy`=?, `mine_level`=?, `mine_exp`=?, `minecount`=?, `mine_maxexp`=? where `id`=?", array($player->energy - 1, $player->mine_level + 1, $player->mine_exp + 1, $player->minecount + 1, $player->mine_maxexp + $newmax, $player->id)); } elseif($strength + $vitality + $wisdom + $agility < 100) { echo "<br /><b>Your mining leveled up and you gained 1 strength point!</b>"; $query = $db->execute("update `users` set `energy`=?, `mine_level`=?, `mine_exp`=?, `minecount`=?, `strength`=?, `mine_maxexp`=? where `id`=?", array($player->energy - 1, $player->mine_level + 1, $player->mine_exp + 1, $player->minecount + 1, $player->strength + 1, $player->mine_maxexp + $newmax, $player->id)); } } else { $query = $db->execute("update `users` set `energy`=?, `mine_exp`=?, `minecount`=? where `id`=?", array($player->energy - 1, $player->mine_exp + 1, $player->minecount + 1, $player->id)); echo "$blurb $randeffect<p>"; } I know this works: if($Authorized == "Declined") { echo "Didn't work"; } else { echo "Success"; } ?> What if I wanted it to echo "Didn't work" if $Authorized == "Declined" OR if the string is empty, i.e. $Authorized == "" Is this it? if($Authorized == "Declined") { echo "Didn't work"; } [elseif ($Authorized == "") { echo "Still didn't work"; }] } else { echo "Success"; } ?> Thanks. Hiya, Trying to get this working based on a drop down posted from the step before. Three options are dropoff, collect and ship. This code always defaults to the first option, despite the fact that echoing the session shows the correct choice has been passed through. Here's what I have: Code: [Select] if ($_SESSION['collection'] = "dropoff") { echo "Please drop it off with us"; } elseif ($_SESSION['collection'] = "collect") { echo "Please collect it from me"; } else { echo "Please ship it to you"; } Any ideas what I'm missing here? Thanks! The below code should take you to a different page depending on what you choice in the drop down, but all the results only take you to the first on even when you put a different option in the drop down, any ideas? Code: [Select] <?php ob_start(); session_start(); $pagerank=1; if ($rank < $pagerank){ header('Location:main.php?id=lowrank.php'); } else{ ?> <form action="main.php?id=search.php" method="post"> <table width="725" border="0" cellspacing="1" cellpadding="1"> <tr> <td width="228">Name: <input name="name" type="text" /></td> <td width="490"><select name="type"> <option>All</option> <option>Alliance</option> <option>Corporation</option> <option>Pilot Name</option> <option>Ship Name</option> <option>Ship Type</option> <option>System Name</option> <option>System Type</option> </select></td> </tr> <tr> <td></td> <td><input name="Submit" type="Submit" /></td> </tr> </table> </form> <?php if (isset($_POST['Submit'])) { $name = $_POST['name']; $type = $_POST['type']; if ($type = "System Name" OR "System Type") { header('Location:main.php?id=searchsystem.php'); exit; } Elseif ($type = "All") { header('Location:main.php?id=searchall.php'); exit; } Else { header('Location:main.php?id=searchpilot.php'); } } } ?> Hi Guys, Beginner needs help with accommodation search feature...i have two drop downs(area and type), each one has an 'any' option. I cant evalute the 'any' options directly against the database so have written this: Code: [Select] <?php //query database if ($_SESSION['type']== 'any' && $_SESSION['area']== 'any'){ $query = mysql_query("SELECT * FROM hotels Order by RAND() "); } elseif ($_SESSION['area'] == 'any' && $_SESSION['type'] != 'any'){ $query = mysql_query("SELECT * FROM hotels WHERE type = $_SESSION[type] Order by RAND()"); } elseif ($_SESSION['type'] == 'any' && $_SESSION['area'] != 'any'){ $query = mysql_query("SELECT * FROM hotels WHERE location =$_SESSION[area] Order by RAND()"); } else{ $query = mysql_query("SELECT * FROM hotels WHERE location ='$_SESSION[area]' AND type ='$_SESSION[type]' Order by RAND()"); } //show results while ($search_result = mysql_fetch_assoc($query)) { echo '<b>'.$search_result[name] . '</b><br>'; echo '<p><img src="'.$search_result['photo1'].'" /></p>'; echo '<p><img src="'.$search_result['award'].'" /></p>'; echo $search_result[description] . '<br><br>'; }?> The first if and last else return the correct info from database but the middle two elseif's throw this error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given.... I have commented out the queries and replaced them with simple echoes and they all seem to evaluate correctly!. So im lost why $query isnt assigned in the middle two elseif's ?? Any ideas what im doing wrong and is there a better way. Cheers!! Is it possible to add more than one elseif in an if, elseif, else statement? Here's an example of what I'm talking about (I do realize this is not proper php syntax): Code: [Select] <?php if (name was not entered in form) { echo ("This user has not entered a name."); } elseif (email was not entered in form) { echo ("This user has not entered an email."); } elseif (age was not entered in the form) { echo ("This user has not entered their age."); } else { echo ("This user has entered all of their information."); } ?> PHP newb here - apologies if the answer to this is really simple.
I've tried to add a geotargetting code into my website but it appears the elseif part isn't working, no matter what I do.
Here's the code - any idea why if is fine, and else is fine, but elseif isn't?
<?php if (function_exists('showCountryContentInPage')) { if(showCountryContentInPage(array("us", "ca"),1)) { // US $region = 'USA'; $country = codediesel_get_users_country(); $store_url = 'URL here'; } elseif(showCountryContentInPage(array("at", "es", "cy", "fi", "ee", "fr", "de", "el", "ie", "it", "mt", "lu", "lv", "nl", "pt", "sk", "si", "be"),1)) { // Europe $region = 'Europe'; $country = codediesel_get_users_country(); $store_url = 'URL here'; } else { // UK $region = 'UK'; $country = codediesel_get_users_country(); $store_url = 'URL here'; } ?> I have simple piece of code to get the value of 'tmap' from a members id: Code: [Select] $query = "SELECT tmap FROM vtp_members WHERE id='$id'"; what I need is to echo different things depending on the value of 'tmap', but don't know how to do this: example: ----------- if tmap is 0 echo "tmap is 0"; elseif tmap is 1 echo "tmap is 1"; and so on to max number of 5 --------------------------- anyone can show how to write this? I have this code which checks for the word "Big Ben" and if it's not there it outputs some html. I want t be able to make another statement to also check for the word "London Tower" and if it's not there then to check for the word "Canary Wharf", So far I've only managed one statement without the code breaking, how do I add the others in as well. <?php $astacker=get_post_meta($post->ID, 'thesite', true); if ( $astacker == [b]'Big Ben'[/b]) { ?> <?php include(TEMPLATEPATH."/feedsgrabstack.php");?> <?php }else { ?> <div id="splitter"><div class="clear"> <a href="<?php echo get_post_meta($post->ID, "linktosource", true);?>">Click Here To View Answers</a> <span style="float:right;"><a href="#related">See Related Questions</a></div></div> <?php } ?> |