Jump to content

Cannot get "foreach" to work from an array??


jhc1982

Recommended Posts

Hi everyone,

 

I am just starting out with php so I am sure this is a really silly question.

 

I have been working through php & mySQL for dummies  :-[ (latest edition) and each time I read a new chapter I try to implement the tutorial. I have been trying something really simple from the book and have now even tried copying it word for word but it still will not work.

 

I am basically trying to output a list of Cities and States with the following code:

 

<?php

$capitals = array ("CA" => "Sacramento", "TX" => "Austin", "OR" => "Salem" );

foreach ($capitals as $state => $city);

ksort ($capitals);

{

echo "$city, $state<br />";

}

?>

Instead of getting:

 

Sacramento, CA

Salem, OR

Austin, TX

 

I just get:

 

Salem, OR

 

I am running XAMPP on Mac OS X 10.6.5.

 

Any ideas or help would be awesome!

Hi Thorpe,

 

This is the code as copied from the book in an html page:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Untitled Document</title>

</head>

<body>

<?php

$capitals = array ("CA" => "Sacramento", "TX" => "Austin", "OR" => "Salem" );

ksort ($capitals);       

foreach ($capitals as $state => $city);

        {

            echo "$city, $state<br />";

        }   

?>

</body>

</html>

 

I now just get:

 

Austin, TX

 

I have a feeling I may have really missed the thread!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.