Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array and Switch Problem

Status
Not open for further replies.

tekpr00

IS-IT--Management
Jan 22, 2008
186
CA
Hello All,
Thanks for everyone that has been helping me with this PHP becuase I am just learning it. Lets face it I am not much of a coder.lol
My next problem is that I am trying to use switch to print array however when I use the query from the drop down list to print the array nothing shows. Here is my result and code below. Thanks for your input as usual

Result
Office: (Pick one)

(Query)
Nothing shows

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Employee Office Array </title>
  
 </head>
 <body>

<form action="emp_office_array.php" method="post">
<p>Office:
<select name ="office">
<option value="">Pick One</option>
<option value="foyer">Foyer</option>
<option value="common">Common</option>
<option value="north">North</option>
<option value="south">South</option>
</select></p>

<input type="submit" name="submit"
 value="Query" />

<?php 
//Create first array:
$foyer = array(0=> "1. John Smith");

//create second array:
$common = array(0=> "1. Fred Flintstone" , "2. Ann Jones");

//create third array:
$north = array(0=> "1. Pebbles Flintstone", "2. Dino Flintstone");

//create fourth array:
$south = array(0=> "1. Betty Rubble", "2. Barney Rubble", "3. Great Gazoo");

//creat associtive array:
$offices = array ( 
'Foyer' => $foyer,
'Common' => $common,
'North' => $north,
'South' => $south
);

//print contents

//var_dump($offices);
$offices=$_POST['offices'];
switch ($offices){
	case 'Foyer':
	print "<p>Employee in $foyer is</p>";
	print "<p><i>{$offices['foyer'][0]}</i>.</p>";
	break;
	case 'common':
	print "<p> Employee in $common are</p>";
	print "<p><i>{$offices['Common'][0][1]}</i>.</p>";
	break;
	print "<p> Employee in $north are</p>";
	print "<p><i>{$offices['North'][0][1]}</i>.</p>";
	break;
	print "<p> Employee in $south are</p>";
	print "<p><i>{$offices['South'][0][1][2]}</i>.</p>";
	break;
}//end of switch

?>
 </form>
</body>
</html>
 
There's a few errors there which should be showing up.

However the main issue seems to be that you are overwriting your array so it is no longer an array, but a string.

Code:
[COLOR=white red]$offices[/color] = array ( 
'Foyer' => $foyer,
'Common' => $common,
'North' => $north,
'South' => $south
);

//print contents

//var_dump($offices);
[COLOR=white red]$offices[/color]=$_POST['offices'];


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
How would you suggest I call the array from the inside a switch statement

Thanks.
 
the select tag in the form has the name "office", so

Code:
switch($_POST['office'])

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I changed it as you suggested. Even with that only common is showing the print out and it is not showing any of the variables in the array.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Employee Office Array </title>
  
 </head>
 <body>

<form action="emp_office_array.php" method="post">

<p>Office:
<select name ="office">
<option value="">Pick One</option>
<option value="foyer">Foyer</option>
<option value="common">Common</option>
<option value="north">North</option>
<option value="south">South</option>
</select></p>

<input type="submit" name="submit"
 value="Query" />

<?php 
//Create first array:
$foyer = array(0=> "1. John Smith");

//create second array:
$common = array(0=> "1. Fred Flintstone" , "2. Ann Jones");

//create third array:
$north = array(0=> "1. Pebbles Flintstone", "2. Dino Flintstone");

//create fourth array:
$south = array(0=> "1. Betty Rubble", "2. Barney Rubble", "3. Great Gazoo");

//creat associtive array:
$offices = array ( 
'Foyer' => $foyer,
'Common' => $common,
'North' => $north,
'South' => $south
);

//print contents

//var_dump($offices);
//$offices_switch=$_POST['offices'];
switch($_POST['office']){
	case 'Foyer':
	print "<p>Employee in Foyer is<i>{$offices['Foyer'][0]}</i>.</p>";
	break;
	case 'common':
	print "<p> Employee in Common are<i>{$offices['Common'][0][1]}</i>.</p>";
	break;
	print "<p> Employee in North are</p>";
	print "<p><i>{$offices['North'][0][1]}</i>.</p>";
	break;
	print "<p> Employee in South are</p>";
	print "<p><i>{$offices['South'][0][1][2]}</i>.</p>";
	break;
}//end of switch

?>
 </form>
</body>
</html>
 
Here is my latest code, please help me.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Employee Office Array </title>
  
 </head>
 <body>

<form action="emp_office_array.php" method="post">

<p>Office:
<select name ="office">
<option value="">Pick One</option>
<option value="foyer">Foyer</option>
<option value="common">Common</option>
<option value="north">North</option>
<option value="south">South</option>
</select></p>

<input type="submit" name="submit"
 value="Query" />

<?php 
//Create first array:
$foyer = array(0=> ' John Smith');

//create second array:
$common = array(0=> '1. Fred Flintstone' , '2. Ann Jones');

//create third array:
$north = array(0=> '1. Pebbles Flintstone', '2. Dino Flintstone');

//create fourth array:
$south = array(0=> '1. Betty Rubble', '2. Barney Rubble', '3. Great Gazoo');

//creat associtive array:
$offices = array ( 
'Foyer' => $foyer,
'Common' => $common,
'North' => $north,
'South' => $south
);

//print contents

//var_dump($offices);
//$offices_switch=$_POST['offices'];
switch($_POST['office']){
	case 'foyer':
	print "<p>Employee in Foyer is<i>{$offices['Foyer'] [0]}</i>.</p>";
	break;
	case 'common':
	print "<p> Employee in Common are<i>{$offices['Common'][0][1]}</i>.</p>";
	break;
	case 'north':
	print "<p> Employee in North are<i>{$offices['North'][0][1]}</i>.</p>";
	break;
	case 'south':
	print "<p> Employee in South are<i>{$offices['South'][0][1][2]}</i>.</p>";
	break;
}//end of switch
?>
 </form>
</body>
</html>
 
you should try it perhaps this way:

$offices = array (
'foyer' = array(0 => ' John Smith'),
'common' = array(0 => '1. Fred Flintstone' , 1 => '2. Ann Jones'),
'north' = array(0=> '1. Pebbles Flintstone', 1 => '2. Dino Flintstone'),
'South' = array(0=> '1. Betty Rubble', 1 => '2. Barney Rubble', 2 => '3. Great Gazoo'));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top