p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Question on Beginning PHP4 pg. 172


Message #1 by "Adam Simpson" <adam@g...> on Mon, 17 Dec 2001 09:40:40
I just finished the array example but now I'm stuck.  On page 172 

under "Improvements to our Program" it talks about using one associative 

array to make the program simpler.  I can't figure out how to do this, 

though.  Does anyone know what this improved program would look like?
Message #2 by "enapoleone" <john@e...> on Mon, 17 Dec 2001 09:44:28 -0500
What an associate Array does is it simplifies Comparing the elements of Two

seperate Arrays of associative data  into One Array.



Therefore instead of :

1. Iterating through one Array: looking for the capital of Arkansas

2. Finding the index value for the element/value in question: which would be

3 or $StatesOf TheUsa[3]

3. And comparing the index value with the index value of an Array of

associative data: $StateCapital[3] = Little Rock



You simply:



$StatesOfTheUsa["Arkansas"]



i.e. echo $StatesOfTheUsa["Arkansas"]



result: Little Rock



I hope this was if some help.





----- Original Message -----

From: "Adam Simpson" <adam@g...>

To: "beginning php" <beginning_php@p...>

Sent: Monday, December 17, 2001 9:40 AM

Subject: [beginning_php] Question on Beginning PHP4 pg. 172





> I just finished the array example but now I'm stuck.  On page 172

> under "Improvements to our Program" it talks about using one associative

> array to make the program simpler.  I can't figure out how to do this,

> though.  Does anyone know what this improved program would look like?




$subst('Email.Unsub').

>



Message #3 by "Adam Simpson" <adam@g...> on Mon, 17 Dec 2001 16:17:51
> Therefore instead of :

> 1. Iterating through one Array: looking for the capital of Arkansas

> 2. Finding the index value for the element/value in question: which 

would be 3 or $StatesOf TheUsa[3]

> 3. And comparing the index value with the index value of an Array of

> associative data: $StateCapital[3] = Little Rock

> 

> You simply:

> 

> $StatesOfTheUsa["Arkansas"]

> 

> i.e. echo $StatesOfTheUsa["Arkansas"]

> 

> result: Little Rock



Thank you so much for replying to my question.  I'm still not quite sure, 

though.  For the states.php file I replaced the example's array with the 

the suggested associative array ("Alabama" => "Montgomery" etc.).



I saved this as states2.php but when I open it my HTML pull-down selector 

has 50 blank choices.  How do I make this new array work with the

echo "<option>$StatesOfTheUSA[$counter]</option>";

line?



Or should I put the new associative array in the capitals.php file and 

leave states.php as it was?
Message #4 by "enapoleone" <john@e...> on Mon, 17 Dec 2001 21:05:35 -0500
Hmmm. I don't know if I quite follow you here. Let me see if I understand

you. You want to iterate through the new associative array and echo each

state capital as an option to a select element?



Well, unless you indicate a string index value for an array element, PHP

will assign it an integer value starting with 0(zero). In the first  example

when the "for loop" iterates through the $StatesOfTheUsa array, the array is

an numerically indexed array. But as soon as you make the array an

associative one, referencing elements using numerical indexes won't work,

they are now strings. I believe this to be true, perhaps someone can correct

me if I am wrong.



Instsead try using the foreach loop to iterate through each element, which

loops through each element in the array until the length of the array.



foreach($StatesOfTheUsa as $var) echo "<option>$var</option>\n";

---- Original Message -----

From: "Adam Simpson" <adam@g...>

To: "beginning php" <beginning_php@p...>

Sent: Monday, December 17, 2001 4:17 PM

Subject: [beginning_php] Re: Question on Beginning PHP4 pg. 172





> > Therefore instead of :

> > 1. Iterating through one Array: looking for the capital of Arkansas

> > 2. Finding the index value for the element/value in question: which

> would be 3 or $StatesOf TheUsa[3]

> > 3. And comparing the index value with the index value of an Array of

> > associative data: $StateCapital[3] = Little Rock

> >

> > You simply:

> >

> > $StatesOfTheUsa["Arkansas"]

> >

> > i.e. echo $StatesOfTheUsa["Arkansas"]

> >

> > result: Little Rock

>

> Thank you so much for replying to my question.  I'm still not quite sure,

> though.  For the states.php file I replaced the example's array with the

> the suggested associative array ("Alabama" => "Montgomery" etc.).

>

> I saved this as states2.php but when I open it my HTML pull-down selector

> has 50 blank choices.  How do I make this new array work with the

> echo "<option>$StatesOfTheUSA[$counter]</option>";

> line?

>

> Or should I put the new associative array in the capitals.php file and

> leave states.php as it was?




$subst('Email.Unsub').

>



Message #5 by "Adam Simpson" <adam@g...> on Tue, 18 Dec 2001 10:52:22
> Hmmm. I don't know if I quite follow you here. Let me see if I understand

> you. You want to iterate through the new associative array and echo each

> state capital as an option to a select element?



Actually, let's forget my last post.  I was just describing one of the 

things I did to try and modify the example.  I obviously modified it in 

the wrong way.



On page 172 it shows the new array that they suggest using to improve and 

simplify the state capitals program.  My question is where do you put that 

array?  Do you work it into states.php or do you work it into 

capitals.php?  Also, what else in the program changes to allow this new, 

associative array to work?



In the improved program, would the user still select a state of the U.S. 

from a pull-down selection menu?
Message #6 by "enapoleone" <john@e...> on Tue, 18 Dec 2001 10:12:37 -0500
Adam,



Well there are two ways to do this. But for the sake of this example and the

stage that you are in the book we will use the book.



Put the associative array in states.php, dynamically create a select list

using the index values(as I can remember--which are the states). Don't worry

about creating a complimentary array in capitals.php, of course we do not

need this anymore. From capitals.php simply echo the value for the name of

the select list in the form of a variable(this should be obvious considering

that they spent all of "getting data from the client" explaining it).



There is the scenario that you can select more than one option from a select

list. That requires:

1.coding the select element using the multiple attribute

2.initializing the name for the select element as an array i.e. $x[] (I

think this has to be scripted)

If you desire multiple results and you do only one of these then I would

guess that the user would only yield the first element in the list of

multiple elements. Retrieving the data from this array is I guess obvious

and redundant.



----- Original Message -----

From: "Adam Simpson" <adam@g...>

To: "beginning php" <beginning_php@p...>

Sent: Tuesday, December 18, 2001 10:52 AM

Subject: [beginning_php] Re: Question on Beginning PHP4 pg. 172





> > Hmmm. I don't know if I quite follow you here. Let me see if I

understand

> > you. You want to iterate through the new associative array and echo each

> > state capital as an option to a select element?

>

> Actually, let's forget my last post.  I was just describing one of the

> things I did to try and modify the example.  I obviously modified it in

> the wrong way.

>

> On page 172 it shows the new array that they suggest using to improve and

> simplify the state capitals program.  My question is where do you put that

> array?  Do you work it into states.php or do you work it into

> capitals.php?  Also, what else in the program changes to allow this new,

> associative array to work?

>

> In the improved program, would the user still select a state of the U.S.

> from a pull-down selection menu?




$subst('Email.Unsub').

>



Message #7 by "Adam Simpson" <adam@g...> on Wed, 19 Dec 2001 11:07:21
> Well there are two ways to do this. But for the sake of this example and 

the

> stage that you are in the book we will use the book.



Ah!  Now I've got the solution.  After thinking over your last post and 

trying a few things I've got my answer.



A previous Web page passes the value for $state to the following program.



<?php

$statecapital = array ("Alabama" => "Montgomery", "Alaska" => "Juneau");



echo "The state capital is $statecapital[$state]";

?>



After choosing Alabama or Alaska from a pull-down list, the user gets the 

answer and the program works well.



Before, I failed to remove the FOR loop and the IF statement from the 

example program (capitals.php on page 169).  You see, I thought that the 

program needs to run a loop to look through the associative array.  As it 

turns out, no loop is necessary - the program already "knows" the contents 

of the associative array.



Thanks for your time.  I'm a beginner but I'm really loving PHP.

  Return to Index