Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Noob - Nested if statements to search multiple arrays

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Noob - Nested if statements to search multiple arrays

Postby phear » Thu Apr 18, 2013 3:27 am

OK, im going to paste all my code from the two pages i have, ill then explain the problem.

index.html
Code: Select all
<hmtl>
<body>
<center><h2>Welcome<h2></center>
<p>
   <b>Enroll a student:</b>
   <form name="enroll" action="script.php" method="post">
      First Name:<input type="text" name="firstname" size="8">
      <br>
      Class:
      <li><input type="radio" name="class[]" value="classone">classone<br>
      <li><input type="radio" name="class[]" value="classtwo">classtwo<br>
      <li><input type="radio" name="class[]" value="classthree">classthree<br>
      <input type="submit" name="enrollsubmit" value="Enroll!">
   </form
</p>
<p>
   <br>
   <br>
   <b>Search for a student:</b>
   <form name="search" action="script.php" method="post">
      Name:<input type="text" name="searchname" size ="13">
      <br>
      <input type="submit" name="searchsubmit" value="Search!">
   </form>
</p>

</body>
</html>


script.php
Code: Select all
<?php

//Define arrays - These are the 'Classes'
$classone = array(bob, john);
$classtwo = array(jane, mary);
$classthree = array(james, tony);

$temparray = array();

//Define Variables
$name = $_POST['firstname'];
$class = $_POST['class'];
$searchname = $_POST['searchname'];

//If script is loaded without clicking through a button:
if ((!isset($_POST['searchsubmit'])) && (!isset($_POST['enrollsubmit']))) {
echo "Error - Form Not Submitted";
}

//This is the Search function
/*
Plan here is to take string from form, search through classrooms for that string, then return the classroom of that student. If no match is found,error.
*/
//**************************************************************************
//**************************************************************************
elseif  ((isset($_POST['searchsubmit'])) && (!isset($_POST['enrollsubmit']))){
   echo "<h2>Search Submitted</h2><br>";
   echo "You searched for <b>$searchname</b>.<br>";
   
   $found = false;
   if ($found == false) {
      foreach ($classone as $a) {
         if ($a == $searchname) {
         echo "found $searchname in Class One";
         $found = true;
            }
         }
      }
   elseif ($found == false) {
      foreach ($classtwo as $a) {
         if ($a == $searchname) {
         echo "found $searchname in Class Two";
         $found = true;
            }
         }
      }
   elseif ($found == false) {
      foreach ($classthree as $a) {
         if ($a == $searchname) {
         echo "found $searchname in Class Three";
         $found = true;
            }
         }
      }
   elseif ($found == false) {
      echo "Sorry, <b>$searchname</b> not found";
      }

   }

//**************************************************************************
//**************************************************************************

//This is the Enroll Function
elseif  ((isset($_POST['enrollsubmit'])) && (!isset($_POST['searchsubmit']))){
   echo "Enrollment Submitted";
   echo "<br>";
   echo $name;
   echo "<br>";
   echo $class;
   echo "<br>";
   
   if (($name == "") && ($class =="")) {
      echo "Sorry, No data enterd";
      }
      elseif (($name =="") && ($class !="")){
         echo "No name enterd!!";
         }
      elseif (($name !="") && ($class =="")){
         echo "No Class enterd!!";
         }
      //All required data is present for enrollment
      /*
      Would like to add function to perform check of class size before adding another student. If classsize >10 then error.
      Added functionality could be to then search for a space in a classroom.
      */
      elseif (($name !="") && ($class !="")){
         if (is_array($_POST['class'])) {
            //define element from posted array as $a, then add $a to temparray
            foreach ($_POST['class'] as $a);
            array_push($temparray, $a);
            }
         //Add Submitted name to temparray
         array_push($temparray, $name);
         
         //This script adds $name to the correct class dependant on the information in the [0] position of $temparray
         switch ($temparray[0]) {
            case 'classone':
            echo "$name has been enrolled into Class One";
            array_push($classone, $temparray[1]);
            break;
            
            case 'classtwo':
            echo "$name has been enrolled into Class Two";
            array_push($classtwo, $temparray[1]);
            break;

            case 'classthree':
            echo "$name has been enrolled into Class Three";
            array_push($classthree, $temparray[1]);
            break;
            }
         }


   }

else {
   echo "Unknown Error!";
   }
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
print_r($classone);
echo "<br>";
print_r($classtwo);
echo "<br>";
print_r($classthree);
?>


The code i am referring to is between the sections of ********** that have been commented in.
OK, i am taking a string from my form 'searchsubmit' and then going through the arrays to find the string to see if it exists. I am using the $found variable to stop searching if the name has been found already. The search works if the name is in 'classone' but not in the next two 'classes'. the script seems to be exiting after seraching the first array and im not sure why. Anyone got any ideas?
phear
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Apr 17, 2013 8:31 am

Re: Noob - Nested if statements to search multiple arrays

Postby johnj » Thu Apr 18, 2013 5:10 am

You are saying if found == false, also else if found == false -- what are you trying to do here?
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Noob - Nested if statements to search multiple arrays

Postby phear » Thu Apr 18, 2013 9:07 am

Once found is set to true, the rest of the code won't run as there is no need, the name has already been found. Not the best way of going about things I know but I'm not sure how to do it another way.
phear
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Apr 17, 2013 8:31 am

Re: Noob - Nested if statements to search multiple arrays

Postby seandisanti » Thu Apr 18, 2013 9:57 am

You're doing it wrong, use a database. It can be as simple as creating a single table database with a table that has the fields,id, name, and class number. Then you'd be able to search all classes at once, have students that take more than one class, generate a roster by class, etc. with very little code. Plus you won't have to hard code names into arrays etc, and data will persist beyond the length of the session
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Noob - Nested if statements to search multiple arrays

Postby phear » Thu Apr 18, 2013 11:00 am

Thats the long term plan seanandisti. I'm really just beginning my journey into php so what you have on that scrip is everything I currently know. Coming from very little programming experience here and even that was a very long time ago.

As it happens I seem to have fixed the problem by replacing 'elseif' with 'if' although I'm confused as to why there is a difference.
phear
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Apr 17, 2013 8:31 am

Re: Noob - Nested if statements to search multiple arrays

Postby seandisanti » Thu Apr 18, 2013 11:01 am

No worries man, we're here to help. Are you familiar at all with mysql or phpmyadmin?
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Noob - Nested if statements to search multiple arrays

Postby phear » Thu Apr 18, 2013 11:26 am

Not yet, mysql is installed just haven't got round to playing with it yet. Is phpmyadmin like a control panel software?

Ill hopefully get round to mysql soon. I'm following some tutorials on zend.com(i think) and the next section is about reading from/writing to files.
phear
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Apr 17, 2013 8:31 am

Re: Noob - Nested if statements to search multiple arrays

Postby sandeep605085 » Thu Apr 18, 2013 11:31 am

seandisanti is right that better to use database while learning so that you will get to know the use of database too.
in your previous comments you wrote that you are confused as why there is difference.
"As it happens I seem to have fixed the problem by replacing 'elseif' with 'if' although I'm confused as to why there is a difference."

let me explain it.
below is the code from you posted code.

$found = false;
if ($found == false) {
foreach ($classone as $a) {
if ($a == $searchname) {
echo "found $searchname in Class One";
$found = true;
}
}
}
elseif ($found == false) {
foreach ($classtwo as $a) {
if ($a == $searchname) {
echo "found $searchname in Class Two";
$found = true;
}
}
}
elseif ($found == false) {
foreach ($classthree as $a) {
if ($a == $searchname) {
echo "found $searchname in Class Three";
$found = true;
}
}
}
elseif ($found == false) {
echo "Sorry, <b>$searchname</b> not found";
}

}


the first if condition will always execute since the $found is false and the first if condition is true.

the use of if.. elseif..elseif...else is that if first 'if' condition is true then code inside first 'if' will be executed. otherwise goes to next if('elseif') to check if that is true to execute the code inside that and so on.

so if you write

$found = false;
if ($found == false) {
foreach ($classone as $a) {
//code
}

if ($found == false) {
foreach ($classtwo as $a) {
//code
}

if ($found == false) {
foreach ($classthree as $a) {
//code
}

if ($found == false) {
//code
}


then all code inside if condition is executed if $found == false.
User avatar
sandeep605085
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions

Re: Noob - Nested if statements to search multiple arrays

Postby seandisanti » Thu Apr 18, 2013 11:38 am

phpmyadmin is a web based interface for mysql that is usually installed and configured automatically with wamp/lamp/xamp. For free video based learning using php and mysql take a look at jream.com. I refer a lot of traffic there because i like the way he does his videos. They're usually less than 10 minutes, and he does well to explain things in plain english without being to overly verbose. He's got videos on mysql and php (and more). Watch this video first; as it is a quick course on mysql and phpmyadmin. http://jream.com/learning/videos/mysql/ ... quickstart

after that one I would skip ahead to

http://jream.com/learning/videos/php-oo ... o-examples

to learn the right way to interact with your mysql database. a lot of the resources out there right now say to use mysql_ functions, but they are deprecated php 5.5 and will be removed in a future version. PDO is faster and more secure, but without a lot of extra work. It's just easier to learn to do it right from the get go.

For general language reference, he's got a basics series, but between what you've already gotten from the other videos, and what you can learn from the first 2 videos, you probably won't need it.

http://jream.com/learning/videos/php-basics
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.