PHP - If...elseif...else Statement With A Loop
Hi My PHP skills aren't that good but I know what I want to do as I have used other programming languages before. I run a WordPress website about a Church and its Cemetery. I have been researching the graves for the last 2 years and have added each grave to the website via a Custom Post Type with the buried occupants. In WordPress I have used a plugin called Advanced Custom Fields and have set each grave to allow at least 5 people buried there. So the following code below is used to check if the fields have any value which would represent each person in that grave and add it to the page. I don't know how to add it as a database table so this is the easiest way I could think of. echo '<div id="section-grave">'; echo "<h3>Who's buried in this Grave?</h3>"; //echo '<p>Listed below, are the people buried in this grave.</p>'; if(get_field('is_burial_date_known')) { // start For Loop for ($x = 1; $x <= 5; $x += 1) { $name_of_deceased = 'name_of_deceased_' . $x; $burial_date = 'burial_date_' . $x; $clergy_name = 'clergy_name_' . $x; if(get_field($name_of_deceased)) { echo '<p>' . $x . ') ' . get_field($name_of_deceased) . '</p>'; } if(get_field($burial_date)) { echo '<p>Burial Date: ' . get_field($burial_date) . '</p>'; } if(get_field($clergy_name)) { echo '<p>Buried By: ' . get_field($clergy_name) . '</p>'; echo '<hr>'; } } // end For Loop } else { echo '<p>Nobody buried here.</p>'; } echo '</div>'; I want to add a paragraph above which I have commented out to show how many people are buried in that grave. So if it was only one person then it should say 1 person as oppose to people. So I need a way of counting the names but I can't see a way how to do it. It will probably based on an IF...Elseif...Else statement and which ever condition is right it just displays the correct line or paragraph. I need a way of counting thetotal number of people per grave but outputting the total relies on the loop which is after the paragraph or line. I hope I have explained it properly so can anyone see how I can do it please? Colin Similar Tutorials
User Authentication Help By Using If Elseif And Else Statement And Redirect Page On 3 Differnet Urls
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 Ok I have a new issue, and for the life of me, I cannot figure out where I went wrong, in my head it looks sound, but it does not seem to really work. I get no errors, but it does not work. if ($_REQUEST['postal'] != "" && $_REQUEST['lbs'] == "" && $_REQUEST['length'] != "" && $_REQUEST['width'] != "" && $_REQUEST['height'] != '') { } elseif ($_REQUEST['postal'] == "" && $_REQUEST['lbs'] != "" && $_REQUEST['length'] != "" && $_REQUEST['width'] != "" && $_REQUEST['height'] != '') { // THis is the problem right here.... // } elseif ($_REQUEST['postal'] == "" && $_REQUEST['lbs'] == "" && ($_REQUEST['length'] == "" || $_REQUEST['width'] == "" || $_REQUEST['height'] != '')) { } elseif ($_REQUEST['postal'] != "" && $_REQUEST['lbs'] != "" && $_REQUEST['length'] != "" && $_REQUEST['width'] != "" && $_REQUEST['height'] != "") { } else { } The Second elseif is the issue. If I enter the postal and weight, it should go to that if else and execute its code, because I left all the length, height and width blank. But in stead it goes to the last else. What I am looking to do is that if any of the length, width, or height is not entered, then it comes back with an error... But it does not seem to pick up on it. ANy help would be appreciated. [EDIT] I swear I looked at it 50 times and didn't see it... I go and make my post, re-read it and realize my operators were all wrong... [makes action of smaking slef in head] [/EDIT] 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 } ?> Below code is working fine but i need to redirect on 3 different pages and its giving me error. My table structure is as
User table
Email Password
admin@yahoo.com 123
tariq@yahoo.com 987
bilal@yahoo.com 456
if user name is like; admin@yahoo.com the page should redirect on welcome.php
if user name is like; info@aiousoft.com the page should redirect to welcome2.php
and if user doesnot exist in database then give error as ELSE "user doesnot exist"
thanks
signin.php
<html><head><title>Sign In</title></head><body> <?php include 'header.php'; ?> <?php include 'menu.php'; ?> <center> <form method="post" action="checklogin.php"> <h3>Please Signin</h3> <table width="400" border="0"> <tr><td>Email</td> <td><input name="email" type="text" id="email"></td></tr> <tr><td>Password</td> <td><input name="password" type="password" id="password"></td></tr> </table> <p><label> <input type="submit" email="submit" value="Submit"> </label><input email="reset" type="reset"> </p> </form> </center> </body> </html> checklogin.php <html><head><title>Check Login</title></head><body> <?php include 'header.php'; include 'menu.php'; $email=$_POST['email']; $password=$_POST['password']; @ $db = mysql_pconnect('localhost', 'root', ''); if (!$db) { echo 'Error: Could not connect to database. Please try again later.'; exit;} mysql_select_db('car'); $q=mysql_query("select * from user where email='".$email."' and password='".$password."' ") or die(mysql_error()); $res=mysql_fetch_row($q); if($res) { header('location:welcome.php'); } else { echo' Please signin again as your user name and password is not valid'; } ?> </body> </html> Attached Files header.php 284bytes 0 downloads menu.php 308bytes 0 downloads I am trying to build this out in my head, but I am unsure of the correct way to do it. I am running a sql query to pull info from the DB. I already have it checking the number of rows with a if statement, but I need it to check to see if a particular row is empty. If it is empty it needs to echo out one thing and if not another. But I also need it to run the loop till it ends with all the info from the DB is out. I am having it check to see if there is an image path in the row. If not have it echo the HTML without the image path and if so, have it included. but some results may or may not have an image so it needs to display both not one or the other. Here's my code. I am pretty sure I have this structured wrong (Well, I know it's wrong cause it doesn't run): Code: [Select] <?php $sql = "select * from tbl_tribue where letter = '$_GET[letter]' order by letter asc"; $result = mysql_query($sql,$conn); $num_rows = mysql_num_rows($result); if ($num_rows=="0") { echo "<p>No pet tributes for the letter.</p>"; } else { while ($row = mysql_fetch_array($result)){ $petname = $row['petname']; $desc = $row['description']; $family = $row['familyname']; $img = $row['imgpath']; if ($img="") { echo <<<END <div> <p><strong>$petname</strong><br /> $desc<br /> <em>- $family</em></p> </div> END; } } else { echo <<<END <div> <img src="$img" class="photoWithBorder2" style="float:left; margin-right:10px; margin-bottom:10px;" /> <p><strong>$petname</strong><br /> $desc<br /> <em>- $family</em></p> </div> END; } } ?> I left out the DB info. Anyways, I am I close or WAY off the mark? Thanks for any help! Hi, Im building a custom theme for wordpress and i need some help with a loop, its not wordpress specific code just a general if and loop. heres what i have. Code: [Select] $pages = get_pages('parent=0&sort_column=menu_order'); if ($pages) { $output=''; foreach ($pages as $page) { if($page->post_status=='publish'){ $output.='<li class=top><a href="'.$page->guid.'" class=top_link><span>'.$page->post_title.'</span></a>'; $sub_pages = get_pages( 'child_of ='. $page->ID); if ($sub_pages){ foreach ($sub_pages as $sub_page) { if($sub_page->post_status=='publish'&& $sub_page->post_parent== $page->ID) { if($sub_page->guid==" ") { } else { $output.='<ul class=sub>'; $output.='<li><a href="'.$sub_page->guid.'">'.$sub_page->post_title.'</a></li>'; $output.='</ul>'; } } } }} $output.='</li>'; } } What im trying to do is only echo <ul class=sub> and the inside <li> if there is something in sub_page guid. I managed to build this code and it works, but i need to move the <ul></ul> outside of the loop so it doesnt repeat over and over again, but i also dont want it being echoed if sub_page->guid is empty sorry for my poor explanation im doing the best i can. Thanks in advance for any help Hello! I am going to end up ending with many If-Else statements so I'm hoping someone can change this into a for loop for me, I will learn this way too Code: [Select] if(empty($_GET['start'])) { $active_1_20 = 'active'; $limit_by = 'LIMIT 0,20'; $rank = 1; } elseif($_GET['start']==21) { $active_21_40 = 'active'; $limit_by = 'LIMIT 20,40'; $rank = 21; } elseif($_GET['start']==41) { $active_41_60 = 'active'; $limit_by = 'LIMIT 40,60'; $rank = 41; } elseif($_GET['start']==61) { $active_61_80 = 'active'; $limit_by = 'LIMIT 60,80'; $rank = 61; } You can see the obvious pattern, next one will be elseif($_GET['start']==81) and I want it to end to elseif($_GET['start']==501) Much appreciated anyone! Thanks. If I have code that looks something like this: <?php $t = array('one','two','three','_four','_five','_six'); $u = 'five'; foreach ($t as $v) { if (substr($v,0,1) != '_') { // if begins with an underscore, remove it, unless it is equal to _$u echo $v.'<br />'; } } ?> I am trying to take out all array items that begin with an underscore except the one that is equal to this.. _five something like $v == '_'.$u So what should be echoed is this: one two three _five Is there any way to do this with only an if, and not an if/else?? Thanks.. Object- Take array of IP addresses and add up all occurrences of a particular IP address. //$item is an array of IP addresses foreach ($item as $key => $unique_ip) { if($unique_ip == $ip) $a++; } echo $a; I get a blank page. What am I missing? Code: [Select] <?php echo '<form action="hwCase.php" method="post"> Enter number <input type="text" name="number"/><br /> </form>'; $input = $_POST['number']; // if a number is entered in the form, ie not null if (isset($input)) { switch ($input) { // if number entered is less than 0, ie negative number case ($input<0): echo "Enter a positive number."; break; // if number entered is greater than 1000 case ($input>1000): echo "Enter a number less than 1000."; break; // if number entered is not a number, ie a letter, a character case (!ctype_digit($input)): echo "<br><img src=\"squidward.jpg\"><br><br>Enter a valid number. "; break; // if valid number is entered, loop "hello world" that many times case (($input>0) && ($input<1000)): echo "hello world " . $input . "<br />"; break; // if anything else is entered default: echo "Enter a number."; break; } } // if no number is entered in the form, ie null else { echo "Please enter a number."; } ?> For some reason, my loop doesn't work. Please help me fix it! Thanks in advance! I need to loop this part: Code: [Select] case (($input>0) && ($input<1000)): echo "hello world " . $input . "<br />"; break; After it goes through the while loop its supposed to take the ID of the first one in the loop and find out its awardType and then with the awardType variable go through the the if statement. After it displays it the code that fits the awardType variable then it gets the second item in the while loop and then with the ID of the second item takes it back to get the awardType and then goes back through the if statement again. I'm not sure what I'm doing wrong. As of right now all its doing is displaying its awardName and a blank dropdown. Code: [Select] <?php session_start(); // Access the existing session // Include the database page require ('../../inc/dbconfig.php');l $userID = $_SESSION['userID']; $statusQuery = " SELECT * FROM statuses"; $statusResult = mysqli_query ( $dbc, $statusQuery ); // Run The Query $awardQuery = " SELECT awards.ID, awards.awardName, awards.awardType FROM awards"; $awardResult = mysqli_query ( $dbc, $awardQuery ); // Run The Query $row = mysqli_fetch_array ( $awardResult, MYSQL_ASSOC ); $awardType = $row[ 'awardType' ]; $charactersQuery = " SELECT characters.ID, characters.characterName FROM characters ORDER BY characters.characterName"; $charactersResult = mysqli_query ( $dbc, $charactersQuery ); // Run The Query $matchQuery = " SELECT eventSegments.ID, eventSegments.segmentTitle FROM eventSegments INNER JOIN events ON eventSegments.eventID = events.ID WHERE DATE_FORMAT(events.bookingDate,'%Y') = DATE_FORMAT(curdate(),'%Y')"; $matchResult = mysqli_query ( $dbc, $matchQuery ); // Run The Query ?> Code: [Select] <fieldset> <legend>Nominees</legend> <?php mysqli_data_seek( $awardResult, 0 ); while ( $row = mysqli_fetch_array ( $awardResult, MYSQL_ASSOC ) ) { ?> <?php if ($awardType == 'Singular') { ?> <div class="field required"> <label for="charactersDrop"><?php echo $row['awardName']; ?></label> <select class="dropdown" name="charactersDrop" id="charactersDrop" title="Characters Drop"> <option value="">- Select -</option> <?php while ( $row = mysqli_fetch_array ( $charactersResult, MYSQL_ASSOC ) ) { print "<option value=\"".$row['ID']."\">".$row['characterName']."</option>\r"; } ?> </select> <input type="button" value="Add Character" class="" onclick="HandlerCharacters()"/> <ul id="characterList"> </ul> <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span> </div> <? } else { ?> <div class="field required"> <label for="events"><?php echo $row['awardName']; ?></label> <select class="dropdown" name="events" id="events" title="Events for <?php echo date("Y") ?>"> <option value="">- Select -</option> <?php while ( $row = mysqli_fetch_array ( $matchResult, MYSQL_ASSOC ) ) { print "<option value=\"".$row['ID']."\">".$row['segmentTitle']."</option>\r"; } ?> </select> <input type="button" value="Add Match" class="" onclick="AddMatch()"/> <ul id="matchList"> </ul> <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span> </div> <?php } ?> <?php } ?> i am trying to get the allowed file types from the database into a switch statement but with no luck so far. They types are in the database like: jpg, png, gif. All seperated with a comma and all in one db column. I am then splitting them at the comma and inserting them into $file_list. In the switch statement i want it to be like: switch($ext) { case "jpg": case "png": case "gif": break; } how can i make this possible? here is my code so far: $ext = strtolower(pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION)); $f_types = $f_types_info['file_types']; $f_types = preg_split("|\,|", $f_types); $types = count($f_types); $file_list = ""; for($i=0; $i<$types; $i++) { $file_list .= $f_types[$i]; } switch ($ext) { case $file_list: break; default: $error = "File Type Not Allowed"; break; } Is it possible to have a loop inside a mysql select statement? If yes, can someone please give me an idea. Hi,
Does anyone have a clue how I might solve this little issue:
I have a MySQL query, for example:
<?php // Make a MySQL Connection $query = "SELECT * FROM staff"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ if($row['clock'] < time()) { echo "<b>"$row['name']. " - ". $row['age']"</b>"; echo "<br />"; } else { echo $row['name']. " - ". $row['age']; echo "<br />"; } } ?>Taking data from the following table setup: name - age - clock Timmy Mellowman - 23 - 09:00:00 Sandy Smith - 21 - 12:00:00 Bobby Wallace - 15 - 14:00:00 What im trying to achieve is compare the current time to the time in the clock column and depending if it's earlier or later, change the styling of the results. The above code does appear to work somewhat however it seems to change the styling of all results in one go, rather than individually when it passes the time in the cell, which is what im looking for. Thanks 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 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 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? 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 />'; 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"); ?> |