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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Only prinintg one element, please help 1

Status
Not open for further replies.

tekpr00

IS-IT--Management
Jan 22, 2008
186
CA
Hello All,

My array is only printing one element instead of mutiple elements where appropriate, please help identify error

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=> 'Fred Flintstone' , 'Ann Jones');

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

//create fourth array:
$south = array(0=> ' Betty Rubble', ' Barney Rubble', '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]}</i>.</p>";
	break;
	case 'north':
	print "<p> Employee in North are<i>{$offices['North'][0]}</i>.</p>";
	break;
	case 'south':
	print "<p> Employee in South are<i>{$offices['South'][0]}</i>.</p>";
	break;
}//end of switch
?>
</form>
</body>
</html>
 
Hi

Personally I prefer to structure the data in a way to avoid such [tt]switch[/tt]es :
PHP:
[teal]<?php[/teal]

[navy]$offices[/navy] [teal]=[/teal] [b]array[/b][teal]([/teal]
  [green][i]'foyer'[/i][/green][teal]=>[/teal][b]array[/b][teal]([/teal]
    [green][i]'name'[/i][/green] [teal]=>[/teal] [green][i]'Foyer'[/i][/green][teal],[/teal]
    [green][i]'employee'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal][green][i]'John Smith'[/i][/green][teal]),[/teal]
   [teal]),[/teal]
  [green][i]'common'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal]
    [green][i]'name'[/i][/green] [teal]=>[/teal] [green][i]'Common'[/i][/green][teal],[/teal]
    [green][i]'employee'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal][green][i]'Fred Flintstone'[/i][/green][teal],[/teal] [green][i]'Ann Jones'[/i][/green][teal]),[/teal]
  [teal]),[/teal]
  [green][i]'north'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal]
    [green][i]'name'[/i][/green] [teal]=>[/teal] [green][i]'North'[/i][/green][teal],[/teal]
    [green][i]'employee'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal][green][i]'Pebbles Flintstone'[/i][/green][teal],[/teal] [green][i]'Dino Flintstone'[/i][/green][teal]),[/teal]
  [teal]),[/teal]
  [green][i]'south'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal]
    [green][i]'name'[/i][/green] [teal]=>[/teal] [green][i]'South'[/i][/green][teal],[/teal]
    [green][i]'employee'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal][green][i]'Betty Rubble'[/i][/green][teal],[/teal] [green][i]'Barney Rubble'[/i][/green][teal],[/teal] [green][i]'Great Gazoo'[/i][/green][teal]),[/teal]
  [teal]),[/teal]
[teal]);[/teal]

[b]echo[/b] [green][i]"<p>Employee in "[/i][/green][teal],[/teal][navy]$offices[/navy][teal][[/teal][navy]$_POST[/navy][teal][[/teal][green][i]'office'[/i][/green][teal]]][[/teal][green][i]'name'[/i][/green][teal]],[/teal] [green][i]" is <i>"[/i][/green][teal],[/teal] [COLOR=darkgoldenrod]implode[/color][teal]([/teal][green][i]', '[/i][/green][teal],[/teal] [navy]$offices[/navy][teal][[/teal][navy]$_POST[/navy][teal][[/teal][green][i]'office'[/i][/green][teal]]][[/teal][green][i]'employee'[/i][/green][teal]]),[/teal] [green][i]"</i>.</p>"[/i][/green][teal];[/teal]


Feherke.
 
You are only telling it print one element. Element 0. You never actually select any more than the first element in your array's sub-arrays.

You can either specifically tell it to print the other elements by calling them sequentially:

Code:
case 'north':
    print "<p> Employee in North are<i>{$offices['North'][0]}
    print "<p> Employee in North are<i>{$offices['North'][[COLOR=white red]1[/color]]}

Or use a loop to print all elements inside an array.

However I'm not sure your current skill level is enough to understand how to use a loop.

I strongly suggest a tutorial on PHP and arrays.



----------------------------------
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top