Page 1 of 1
foreach formatting, varible name withen a varible
Posted: Tue Apr 13, 2004 3:26 pm
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){
$result=mysql_query("select * from zapfee_listings where county = '$value'");
$head = mysql_num_rows($result);
if($head > 0){
print "<li>$value</li>";
print "<ul>";
foreach($Oakland_Cities as $value1){
$result1=mysql_query("select * from zapfee_listings where city = '$value1'");
$head1 = mysql_num_rows($result1);
if($head1 > 0){
print "<li>$value1 (".$head1.")</li>";
}
}
print "</ul>";
}
}
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.
Posted: Tue Apr 13, 2004 4:00 pm
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
Posted: Tue Apr 13, 2004 4:06 pm
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ї"variable_name_without_dolar_sign"];
Posted: Tue Apr 13, 2004 4:06 pm
by farfromrest
no

Posted: Tue Apr 13, 2004 4:17 pm
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
Would it not be simpler?

Posted: Tue Apr 13, 2004 4:19 pm
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.
Posted: Tue Apr 13, 2004 4:21 pm
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?
Posted: Tue Apr 13, 2004 4:22 pm
by MarK (CZ)
Well, the one would be as I said:
Code: Select all
forEach ($GLOBALSї$value1."_Cities"] as $value2)
But the array solution (as RadixDev said) would be more clear.
Posted: Tue Apr 13, 2004 4:28 pm
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.
