Slips Posted August 22, 2009 Share Posted August 22, 2009 Hi all, this is my first post here, im a beginner with php and im havnig this issue which seems quite simple to resolve, but i havent been able to figure out.. Heres Im using a variable variable to display an array but when it displays all elements as $i , but when i use the direct array variable it, it displays fine. So heres a breakdown of whats going on in this script: - Its basically for listing the top 10 cities of the respective country, based on the number of pics in that city's -gallery.. -So if someone clicks say, Japan - the top 10 cities in japan with the most no. of pics in their gallerys will be load(ajax call). - The script works like this: Each city has its mysql table. The total no. of pics are the total no. of rows. When someone clicks on a country link, the script first gets a list of all its cities from its cities array. Clicking japan will make the script use $japan_cities('japancity1' = > 'Full city name')...etc from an external file called countryarrays.php which has the list of all country and city arrays. Yes i know , i should be using multidimensional arrays but for now , im just testing the code with a few arrays before i fill up all the arrays. The problem : When i use $fullcityname = "$$citiesarray[$cityshortkey]"; the array elements are displayed as $i,$i,$i but if i use the cities array name directly it will display fine, $fullcityname = "$japan_cities[$cityshortkey]"; will display fine Tcountryarrays.php contains arrays for all cities, just like the one below. $japan_cities=array( 'tokyo'=>'Tokyo', 'okinawa'=>'Okinawa'); <?php require '../countryarrays.php'; require '../dbconnect.php'; $country = $_GET['country']; //Passed on from an ajax call $citiesarray = $country.'_cities'; //Ex: For japan the $citiesarray would become 'japan_cities' $array_totalpics = array(); //Array to hold values as the total pics of each japan city $array_citykey = array(); //Array to hold values of each city's keyname(New York is newyork). $combined=array(); //Full names and keys are assigned in countryarrays.php which is call foreach ($$citiesarray as $citykeyname=>$fullcityname) { mysql_select_db($gallerydb,$link); //SELECT DB $GALLERYDB $citytable = $citykeyname.'_gallery'; //Because all tables in the db are in the format city_gallery) $totalpicsquery = mysql_query("SELECT COUNT(*) FROM $citytable"); //QUERY TO GET THE TOTAL PICS list($totalpics) = mysql_fetch_row($totalpicsquery); //FUNCTION TO ASSIGN NUMBER OF PICS TO $totalpics array_push($array_totalpics,"$totalpics"); //Push each value of totalpics into the array array_push($array_citykey,"$citykeyname"); //Push each value of cityshort into the array //The reason i created a new array for the totalpics and citykeyname was //to get an array in format (citykeyname => totalpics), but that wasnt possible //for associative arrays with array_push. Both these arrays get combined below } $combined = array_combine($array_citykey,$array_totalpics); //Combined array arsort($combined); //Sort the array based on total pics from highest to lowest $sliced_combined = array_slice($combined,0,20); //Display only a max of 20 cities print "<br><br>"; foreach($sliced_combined as $cityshortkey=>$pics) { $fullcityname = "$$citiesarray[$cityshortkey]"; //The key is used to get the array Full city name value from the //list of cities array in countryarrays.php. Ex: japan_cities[tokyo] will output 'Tokyo' print "<a href=\"\" title=\"$fullcityname\">$fullcityname ($pics)</a><br>"; } ?> Thanks in advance, and any suggestions on how to improve my code is appreciated Link to comment https://forums.phpfreaks.com/topic/171403-solved-variable-variable-array-outputs-all-elements-as-iii/ Share on other sites More sharing options...
Garethp Posted August 22, 2009 Share Posted August 22, 2009 I think what's happening is that it's setting the variable name to $citiesarray[$cityshortkey], rather than $citiesarray; So try $Temp = $$citiesarray; $fullcityname = $Temp[$cityshortkey]; Or something like that Link to comment https://forums.phpfreaks.com/topic/171403-solved-variable-variable-array-outputs-all-elements-as-iii/#findComment-903946 Share on other sites More sharing options...
Slips Posted August 22, 2009 Author Share Posted August 22, 2009 lol, damn i tried everything except that, but thanks a lot , that saved me a lot of time! Link to comment https://forums.phpfreaks.com/topic/171403-solved-variable-variable-array-outputs-all-elements-as-iii/#findComment-903971 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.