Jump to content

HOW TO Fetch data from multidimensional array without foreach loop


justin7410

Recommended Posts

Hey guys,

 

I am working for the first time without a database and MySQL while using PHP. anyway i am working on a real estate page and i am having issues on the "areas we cover", and this section would allow a user to view the neighborhoods or locations that the real estate agent has listings or does showings.

 

i created a multidimensional array with the data i need.

<?php
$cities = array('pasadena' => array(
"name" => "Pasadena",
"picture" => "pasadena.png",
"link" => "https://maps.google.com/maps?q=pasadena+ca&ie=UTF-8&ei=21uRUqGUKY3VoATvsILwDA&ved=0CAgQ_AUoAg",
"value" => "1",
"details" => "This is where the details will go",
'data'  => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Pasadena/"></script></div>',
'second_image'  => '<img src="altadena_home.jpg" height="195" width="200">',
'trulia'  => '<style type="text/css">  .module {overflow:visible;}/*For typepad*/  #tMinistats { position:relative; width:180px; }  #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; }  #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;">   <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Pasadena/"></script> </div>',
'second_image'  => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>',
'third_image'  => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>',
'fourth_image'  => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' 
),
'glendale'  => array(
"name" => "Glendale",
"picture" => "glendale.png",
"link" => "https://maps.google.com/maps?q=glendale+ca&ie=UTF-8&ei=wFuRUvSBFNLroASkvYKgDw&ved=0CAgQ_AUoAg",
"value" => "2",
"details" => "This is where the details will go",
'data'  => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Glendale/"></script></div>',
'second_image'  => '<img src="altadena_home.jpg" height="195" width="200">',
'trulia'  => '<style type="text/css">  .module {overflow:visible;}/*For typepad*/  #tMinistats { position:relative; width:180px; }  #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; }  #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;">   <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Glendale/"></script> </div>',
'second_image'  => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>',
'third_image'  => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>',
'fourth_image'  => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' 
),
'eagle rock'  => array(
"name" => "Eagle Rock",
"picture" => "eagle.png",
"link" => "https://maps.google.com/maps?q=eagle+rock+maps&ie=UTF-8&ei=cluRUtLDB9LuoASw8YDoDw&ved=0CAgQ_AUoAg",
"value" => "3",
"details" => "This is where the details will go",
'data'  => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Eagle_Rock/"></script></div>',
'second_image'  => '<img src="altadena_home.jpg" height="195" width="200">',
'trulia'  => '<style type="text/css">  .module {overflow:visible;}/*For typepad*/  #tMinistats { position:relative; width:180px; }  #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; }  #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;">   <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Eagle_Rock/"></script> </div>',
'second_image'  => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>',
'third_image'  => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>',
'fourth_image'  => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' 
),
'silverlake'  => array(
"name" => "Silverlake",
"picture" => "silverlake.png",
"link" => "https://maps.google.com/maps?q=Silverlake+Ca&ie=UTF-8&ei=VFuRUrOLJMXwoASt74CQDQ&ved=0CAoQ_AUoAg",
"value" => "4",
"details" => "This is where the details will go",
'data'  => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Silver_Lake/"></script></div>',
'second_image'  => '<img src="altadena_home.jpg" height="195" width="200">',
'trulia'  => '<style type="text/css">  .module {overflow:visible;}/*For typepad*/  #tMinistats { position:relative; width:180px; }  #tMinistats .tMinistatsS { text-align:center; position:absolute;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; }  #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;">   <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/90026"></script></div>',
'second_image'  => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>',
'third_image'  => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>',
'fourth_image'  => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' 
),
'atwater'  => array(
"name" => "Atwater Village",
"picture" => "atwater_village.png",
"link" => "https://maps.google.com/maps?q=atwater+village&ie=UTF-8&hq=&hnear=0x80c2c0e875cbce3b:0x99ebb9fd977ae16f,Atwater+Village,+Los+Angeles,+CA&gl=us&ei=kVGRUrzgHoTloAT_u4HADg&ved=0CKYBELYD",
"value" => "5",
"details" => "This is where the details will go",
'data'  => '',
'trulia'  => '<style type="text/css">  .module {overflow:visible;}/*For typepad*/  #tMinistats { position:relative; width:180px; }  #tMinistats .tMinistatsS { text-align:center; position:absolute;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; }  #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;">   <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/90026"></script></div>',
'second_image'  => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>',
'third_image'  => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>',
'fourth_image'  => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' 
),
'palmdale'   => array(
"name" => "Palmdale",
"picture" => "palmdale.png",
"link" => "https://maps.google.com/maps?q=Palmdale+Ca&ie=UTF-8&ei=G1qRUti1NonuoASHsIK4Cw&ved=0CAoQ_AUoAg",
"value" => "6",
"details" => "This is where the details will go",
'data'  => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Palmdale/"></script></div>',
'second_image'  => '<img src="altadena_home.jpg" height="195" width="200">',
'trulia'  => '<style type="text/css">  .module {overflow:visible;}/*For typepad*/  #tMinistats { position:relative; width:180px; }  #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; }  #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;">   <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Palmdale/"></script> </div>',
'second_image'  => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>',
'third_image'  => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>',
'fourth_image'  => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' 
),
'lancaster'  => array(
"name" => "Lancaster",
"picture" => "lancaster.png",
"link" => "https://maps.google.com/maps?q=Lancaster+CA&ie=UTF-8&ei=elqRUqT_D8HroASK-oGwDw&ved=0CAgQ_AUoAg",
"value" => "7",
"details" => "This is where the details will go",
'data'  => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Lancaster/"></script></div>',
'second_image'  => '<img src="altadena_home.jpg" height="195" width="200">',
'trulia'  => '<style type="text/css">  .module {overflow:visible;}/*For typepad*/  #tMinistats { position:relative; width:180px; }  #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; }  #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;">   <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Lancaster/"></script> </div>',
'second_image'  => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>',
'third_image'  => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>',
'fourth_image'  => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' 
),
'burbank'    => array(
"name" => "Burbank",
"picture"  => "burbank.png",
"link" => "https://maps.google.com/maps?q=Burbank+Ca&ie=UTF-8&ei=qlmRUuKXF5HroATysoJI&ved=0CAoQ_AUoAg",
"value"  => "8",
"details" => "This is where the details will go",
'data'  => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Burbank/"></script></div>',
'second_image'  => '<img src="altadena_home.jpg" height="195" width="200">',
'trulia'  => '<style type="text/css">  .module {overflow:visible;}/*For typepad*/  #tMinistats { position:relative; width:180px; }  #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; }  #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;">   <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Burbank/"></script> </div>',
'second_image'  => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>',
'third_image'  => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>',
'fourth_image'  => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' 
),
'altadena'   => array(
"name"  => "Altadena",
"picture"  => "altadena.png",
"link"  => "https://maps.google.com/maps?q=altadena+ca&ie=UTF-8&ei=01iRUrW1I8zloATwn4KgBA&ved=0CAoQ_AUoAg",
"value"  => "9",
"details" => "This is where the details will go",
'data'  => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Altadena/"></script></div>',
'second_image'  => '<img src="altadena_home.jpg" height="195" width="200">',
'trulia'  => '<style type="text/css">  .module {overflow:visible;}/*For typepad*/  #tMinistats { position:relative; width:180px; }  #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; }  #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;">   <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Altadena/"></script> </div>',
'second_image'  => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>',
'third_image'  => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>',
'fourth_image'  => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' 
),
'z_all'   => array(
"name" => "Sorry! Didn't find your city? We can show more.",
"picture" => "all.png",
"link" => "contact_us.php",
"value" => "10",
"details" => "This is where the details will go",
'data'  => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Altadena/"></script></div>',
'second_image'  => '<img src="altadena_home.jpg" height="195" width="200">',
'trulia'  => '<style type="text/css">  .module {overflow:visible;}/*For typepad*/  #tMinistats { position:relative; width:180px; }  #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; }  #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;">   <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Altadena/"></script> </div>',
'second_image'  => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>',
'third_image'  => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>',
'fourth_image'  => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' 
),
);
sort($cities);
?>

 

I originally created this page as a single page layout that would have all the areas listed then would have them looking like a nice organized list. i called this page area_showings.php

 

i created a foreach() loop that would call the array then the value

foreach($cities as $city => $data){} 

this worked perfectly and was calling the data to the variable value and i could set the page up. 

 

I then decided that the idea of a long list of cities was not very user friendly and i decided to make is a dynamic page with a different structure.

 

i wanted the user to go on area_showings.php and there he would select a link of his city interested in, and the page would then load the data based upon what the user had selected.

<?php 

foreach ($cities as $city => $data){

echo 

<a href="area_showings.php?city=' . $data['name'] .'">'. $data['name'] . 

'</br></div>';

}
?>

Now when a user selects the city he should be directed to the same page but now it would show the direct data based on the URL.

 

This works fine and is passed correctly with the $_GET.

 

MY ISSUE:

 

 

I have created my conditional to check if the $_GET is empty or matches the correct city.

 

but after the conditional i cant seem to understand how i can call the same $data[]; without a loop.

 

i am use to MySQL where you can call for the $data using fetch_assoc to create the variables.

 


if (empty($_GET['city']) === true){

?>
<div style="background-color:#A9B9C9;width:780px; height: auto; padding-left: 30px;padding-top: 30px; margin-left: 200px; margin-bottom: 20px; margin-top: 10px; border: black 5px solid;line-height: 25px;">

<?php 
foreach ($cities as $city => $data){


echo '<div style="height: 50px; width: auto; border: black solid 2px;font-size: 1.2em; padding: 10px 10px;">

<a href="area_showings.php?city=' . $data['name'] .'">'. $data['name'] . 
'</br></div>';

}
?>

</div>

<?php
} 


else if (empty($_GET['city']) === false) {
$name = $_GET['city'];


if($name == 'Burbank' || $name == 'Glendale' || $name == 'Atwater Village' || $name == 'Pasadena' || $name == 'Altadena' || $name == 'Eagle Rock' || $name == 'Silverlake' || $name == 'Lancaster' || $name == 'Palmdale' || $name == 'Echo Park' || $name == 'Sorry! Didn\'t find your city? We can show more.'){
echo '<div style="background-color: #fff;padding: 10px 10px;height: auto; width:750px;border: 2px solid #000; border-radius: 15px;">

echo 


'<div style="background-color: #fff;padding: 10px 10px;height: auto; width:750px;border: 2px solid #000; border-radius: 15px;">




<div style="font-family:Lato, sans-serif, Helvetica, sans-serif;color:#2B4450; font-size: 30px; font=weight:bolder;margin-bottom:15px;margin-top:15px; text-transform: uppercase;">'  . $data['name'] . 
'</div>


<div style="height:250px; width:200px; border: black 4px solid; margin-bottom: -200px;"><a href="' . $data['link'] .'" target="_blank"><img src="images/cities/' . $data['picture'] . '" height="250" width="200"></a></div>


<div style="position:relative;top:-55px;left:220px;height:250px; width: 0px;line-height:-50px;margin-top:-120px; padding-left: 10px; padding-top: 10px;border-radius: 10px;">' .$data['data'] . '<span style="position:relative; top: -200px; left: 320px;">' .$data['trulia'] .






'</span></div><div style="width:auto; height; 300px; border-bottom: 3px solid black;">'




. $data['second_image'] . $data['third_image'] . $data['fourth_image'] .


'</div>


<h2><span style="color:#2B4450; font-weight: bolder; font-family:Lato, sans-serif, Helvetica, sans-serif;">For more info</span>  <a href="contact_us.php" class="info_Button">Click Here</a></h2>




</div>




</br>';

</div> </br>'; } }

so just to clarify, i want to be able to use the correct array of data to the matching city of the $_GET with a multidimensional array without using a foreach loop. i want the info to be dynamic not listed.

 

any suggestions ? implode function ? array_walk ? 

Edited by justin7410
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.