foreach formatting, varible name withen a varible

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
farfromrest
Forum Newbie
Posts: 11
Joined: Sun Jul 20, 2003 7:09 pm

foreach formatting, varible name withen a varible

Post by farfromrest »

Code: Select all

print"
		<tr>
			<td>
				<table cellpadding="0" cellspacing="0" id="sections" width="100%">
					<tr>
						<td align="left" class="header">
						<ul>";
	$countys=array(Oakland, Macomb);
	$Macomb_Cities=array(Shleby, Utica, Romeo, Washington);
	$Oakland_Cities=array(Rochester, Oakland);
	foreach($countys as $value)&#123;
		$result=mysql_query("select * from zapfee_listings where county = '$value'");
		$head = mysql_num_rows($result);
		if($head > 0)&#123;
			print "<li>$value</li>";
			print "<ul>";
			foreach($Oakland_Cities as $value1)&#123;
				$result1=mysql_query("select * from zapfee_listings where city = '$value1'");
				$head1 = mysql_num_rows($result1);
				if($head1 > 0)&#123;
					print "<li>$value1 (".$head1.")</li>";
				&#125;
			&#125;
			print "</ul>";
		&#125;
	&#125;
	print"
						</td>
					</tr>
				</table>
			</td>
		</tr>
	";
There is my code i know this is possible, but i cant remember the correct way to do this.
My second foreach statment is withen a foreach.
If you could use quotes in a foreach for the varible name the secound foreach would look like this...

Code: Select all

foreach("$".$value."_Cities" as "$value1")
i hope some one understands what im tryin to do here, thanks.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Code: Select all

foreach ($arr as $key => $value) {
   echo "Key: $key; Value: $value<br />\n";
}
http://us4.php.net/manual/en/control-st ... oreach.php

hope that helps
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

I'm not sure what you need but if the problem is how to get some variable dynamically, you can use:

Code: Select all

echo $GLOBALS&#1111;"variable_name_without_dolar_sign"];
farfromrest
Forum Newbie
Posts: 11
Joined: Sun Jul 20, 2003 7:09 pm

Post by farfromrest »

PHP:



foreach ($arr as $key => $value) {
   echo "Key: $key; Value: $value<br />\n";
}






http://us4.php.net/manual/en/control-st ... oreach.php

hope that helps
no :cry:
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post by RadixDev »

So basically what you want to do is "dynamically" create a varible name? right?

Could you not make an array containing the data such as

Code: Select all

<?php
$cities[$variable];
?>
Would it not be simpler? :roll:
farfromrest
Forum Newbie
Posts: 11
Joined: Sun Jul 20, 2003 7:09 pm

Post by farfromrest »

i need to be able to format the array name the foreach is pullin data from. I need to format it with the $value used in the foreach that this foreach is within.
foreach($something as $value1)
{
foreach($value1's_value_with_something_added_on as $value2)
{
}
}
so say if $value1 is Oakland, then i need the second foreach pulling from a array name $Oakland_Cities. please help guys and thanks for tryin.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Here's an example.

Code: Select all

<?php
$loop1 = array('one', 'two', 'three');
$one_cities = array('one_1', 'one_2', 'one_3');
$two_cities = array('two_1', 'two_2', 'two_3');
$three_cities = array('three_1', 'three_2', 'three_3');
foreach($loop1 as $value){
    foreach(${$value.'_cities'} as $value2){
        echo $value2.'<br />';
    }
}
?>
That what you mean?
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Well, the one would be as I said:

Code: Select all

forEach ($GLOBALS&#1111;$value1."_Cities"] as $value2)
But the array solution (as RadixDev said) would be more clear.
farfromrest
Forum Newbie
Posts: 11
Joined: Sun Jul 20, 2003 7:09 pm

Post by farfromrest »

Here's an example.


PHP:



<?php
$loop1 = array('one', 'two', 'three');
$one_cities = array('one_1', 'one_2', 'one_3');
$two_cities = array('two_1', 'two_2', 'two_3');
$three_cities = array('three_1', 'three_2', 'three_3');
foreach($loop1 as $value){
    foreach(${$value.'_cities'} as $value2){
        echo $value2.'<br />';
    }
}
?>




That what you mean?
that works perfect, thanks so much and thanks to everyone else for tring. your guy's help is greatly appreachated. :P
Post Reply