Hey, I have an associative array $item[] with values for an item. I want the user to be able to select which field value of an item is shown... Here's an example to clarify things: If field = "Price", then I require the command line: $value = $item['Price']; PHP: If field = "Name", then I require the command line: $value = $item['Name']; PHP: I've tried the following command, with $field representing the part that changes as the visitor sets it to different values, but for some reason it doesn't work... How do I correct it so it does as specified above? // $field is the variable that is assigned by the user $value = $item['$field']; PHP: It's probably a really stupid thing but I'm relatively new to PHP so please bare with me! Thanks, Hodge
I guess you can use an if statement if ($field == 'Name') { $value = $item['Name']; } else if ($field == 'Price') { $value = $item['Price']; } else { $value = $item['blah']; } PHP: Im not too sure if this is the best way or if it's going to work at all but try it anyway, good luck
Or if (array_key_exists($field, $item)) { $value = $item[$field]; } else { $value = $item['default']; } PHP:
I had thought of that approach but as this is going to be placed within a loop I didn't think it would be efficient to check it with the if statement each time... Thanks for your input though That didn't solve the problem - I think the problem is the fact that I am using a variable within the [' '] of the associative array. The reason I say this is because if I replace the $field variable with the actual literal value, it works fine. But the problem with this approach as stated above is that I don't think it's very efficient...
If you're doing like in your example: $value = $item['$field']; PHP: ... it won't work. Take out the single quotes. $value = $item[$field]; PHP:
Thanks for the advice but tried it and it still doesn't work... Just to clarify things further here's the simplified version of the code I'm using. The table items has the structure as follows: The Code: // Sort is a variable passed from previous page (defined by user). //It specifies which characteristic to use to sort the results (price, alphabetical etc...) // Include config.php - contains DB settings include ("config.php"); // Select All Weapons Info From 'items' $sql = "SELECT * FROM items WHERE type = 'Weapon" ORDER BY '$sort'"; $result = mysql_query ($sql , $conn); if ($result){ // For each result while ($item = mysql_fetch_assoc ($result)){ // Get item name from field "Name" $itemName = $item['Name']; // If sort isn't via name then show value of sort if ($sort != "Name"){ $value = $item[$sort]; } // Show item name and value print "<p>$itemName :: $value</p>"; } } PHP: It's not the exact code I'm using but I think you'll get the idea. Basically if the user sorts the results by name (alphabetically), then I don't want any values to appear. If they sort by anything else, such as price, I want that field value to appear next to the item name. In case you want to see the actual page to see what I mean about sort by thing check out : http://www.monatoesprit.org/stats/weapons/class/All/By-Name/0.php I used Mod_rewrite as well on the url. Anyway any input is appreciated! Thanks, Hodge
Firstly nico_swd was right in telling you to remove the single quotes. The fact that it still doesn't work implies to me, at least, that $field is not being set. What happens if you add the line: echo '$field value is: ' . $field . '!'; somewhere? How and where are you actually setting $field?
Ok basically the user can decide whether to sort the items using their price, max attack etc... This is done using a variable, $sort, which is set to the name of the field that I want to order the results by. The $sort is used to set the SQL query as follows: $sql = "SELECT * FROM items WHERE type = 'Weapon" ORDER BY '$sort'"; PHP: And this works fine. As this SQL query will return multiple rows of results, I place them into an associative array and process them one by one. When the results are being sorted with anything other than their names, i.e. $sort != "Name", I want the actual value to be shown. For example if the user sorts the items by price, I want the price to be outputted with the item names. If it's ordered by max attack, I want the max attack value of each item to be shown. That's why I used the line: $value = $item[$sort]; PHP: $sort is set in the url. I used a redirect so you can't see it easily but it's the part that says "By-Name" or "By-Price" etc... I know that $sort is being set properly because I checked by printing out $sort in the footer (removed now) and it was correct. Also the selection bar for the classes and "sort by" is generated using $sort as well so it wouldn't work unless $sort is being set. This problem is really bugging me... Thanks, Hodge
OK, I'm a little confused: I take it you didn't copy and paste that code there because there is no way that that code you pasted worked. At the end of 'Weapon' you have a double quote that closes the string and so you'd end up with essentially unparsable PHP. Other than that, you are also apparently assuming register_globals is on, which is going to get you in trouble in the long run. Anyway... off the top of my head, I'm wondering if there's a case sensitivity issue with the keys in the array? Just inside your while loop, add this code and let us know some / all of the output: print '<pre>'; print_r( $item ); print '</pre>';
An example of the output: [id] => 196 [Lv] => 1 [Name] => Dagger [Type] => 52 [Gender] => 2 [Job] => 1 [Durability] => 150 [Price] => 50 [AttSpeed] => 1000 [MinAtt] => 60 [MaxAtt] => 75 [MinMagAtt] => 30 [MaxMagAtt] => 40 [Def] => 0 [MagDef] => 0 [Tradable] => 1 [Equipable] => 1 [Icon] => Weapon/Dagger_01.jpg Code (markup): And sorry the double quote thing was a typo, the actual code was correct. And also I'm not assuming register_globals is on, I actually used the $_GET[] command to get the variables earlier but I forgot to show that Thanks for your help so far btw
Oh my god... It started working... I was just fiddling around with the code a little. I think I must have made a stupid mistake somewhere because I haven't slept in over 24 hours... Sorry guys and thanks for all the help you've given me! Hodge