JSHINER Posted May 1, 2007 Share Posted May 1, 2007 I have the following <?php $APPLIANCES = ARRAY ( // Breaks up A,C to two separate variables to be used later $a => substr($page['details']['APPLIANCES'], 0, 1), $b => substr($page['details']['APPLIANCES'], 2, 1) ); // Pulls from a table which defines the Short and Long meanings of AMENITIES foreach ($page['references_amenities'] as $z) { $$z['Short'] = $z['Long']; } //Groups the A,C variables from above with their Long meanings from above. So if $a = C it should display the Long meaning based on the Short being equal to C foreach ($PPLIANCES as $y) { if($y=$z) { echo $z['Long'];} } ?> Where is my error above? It is not working. I first set up JUST the foreach with $z['Short'] and $z['Long'] and it worked - displayed "A - First, "B - Second" and so on. Quote Link to comment Share on other sites More sharing options...
paul2463 Posted May 1, 2007 Share Posted May 1, 2007 you have a spelling mistake in your foreach statement, you have a missing "A" from your $APPLIANCES Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 1, 2007 Author Share Posted May 1, 2007 Thanks. However the last foreach is still not working correctly. It is just displaying the last Long from the previous foreach. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted May 1, 2007 Share Posted May 1, 2007 post the values of all your variables. what is it doing. what is it supposed to be doing? Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 1, 2007 Author Share Posted May 1, 2007 The $APPLIANCES array is taking a value from a database that is stored as "A,B,D,E" or something like that - letters vary. I need to split each letter of that up into its own variable, so I created that ARRAY so $a = A, $b = B, $c = D, etc I then need to cross reference those $a, $b variables with the results of the $page[references_amenities] which each result looks like A - Long Name .... their fields are $z['Short'] - $z['Long'] So based on the $APPLIANCES array, I need the $a to match up to the corresponding $z['Short'] and then display the $z['Long'] value for that. I hope that makes sense. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted May 1, 2007 Share Posted May 1, 2007 sorry, that doesn't make any sense. you probably need to post more code. Quote Link to comment Share on other sites More sharing options...
Koobi Posted May 1, 2007 Share Posted May 1, 2007 i make this mistake a lot too which is probably why i caught this out <?php if($y=$z) { echo $z['Long'];} ?> in the above, you are trying to assign $z to $yz and an assignation will always work...unless you are trying to assign a value to a previously defined constant and therefore, your if condition will ALWAYS be true. you have to use either '==' or '===' depending on your needs. what happens in between the if conditions brackets is that the code in there is evaluated (think var_dump()) and the if condition itself receives the boolean result of what is in the brackets. so, you assigning $z to $y will always return a true to IF since that is a perfectly legal thing to do in PHP. btw, you can correct your code by making it like this: <?php if($y == $z) { echo $z['Long'];} ?> Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 1, 2007 Author Share Posted May 1, 2007 Thanks. However, the if($y == $z) echo $z['Long'] is still not working correctly. I am not sure if I am doing this correctly. Here is my current code: <?php $APPLIANCES = ARRAY ( $a => substr($page['details']['APPLIANCES'], 0, 1), $b => substr($page['details']['APPLIANCES'], 2, 1) ); foreach ($page['references_amenities'] as $z) { $z['Short'] = $z['Long']; } foreach ($APPLIANCES as $y) { if($y == $z) { echo $z['Long'];} } ?> Quote Link to comment Share on other sites More sharing options...
Koobi Posted May 1, 2007 Share Posted May 1, 2007 try working backwards then. check the results of $z like this: <?php echo '<pre>' . print_r($z, true) . '</pre>'; ?> and if $z does contain what you expect, then go backwards and do the same for $page, etc until you see something that we might find useful to solve this problem and post it here. also, you had this earlier: <?php $$z['Short'] = $z['Long']; ?> but now you have this: <?php $z['Short'] = $z['Long']; ?> just pointing it out because your code would have failed earlier anyway. Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 Using the following: $APPLIANCES = ARRAY ( $a => substr($page['details']['APPLIANCES'], 0, 0), $b => substr($page['details']['APPLIANCES'], 2, 1) ); foreach ($APPLIANCES as $y) { echo '', $y, ' > '; } Now when I simply do "echo $page['details']['APPLIANCES']" I get A, C However, when I do that foreach echo '', $y, '>'; It only displays C > What am I missing here? Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 What does... print_r($APPLIANCES); produce? Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 print_r($APPLIANCES); Produces... Array ( [] => C ) Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 OK so I changed the ARRAY to: $APPLIANCES = ARRAY ( '$a' => substr($page['details']['APPLIANCES'], 0, 0), '$b' => substr($page['details']['APPLIANCES'], 2, 1) ); foreach ($APPLIANCES as $y) { echo '', $y, ' > '; } put ' ' around $a and $b Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 And? What does... print_r($APPLIANCES); produce now? Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 print_r($APPLIANCES); Now produces: Array ( [$a] => A [$b] => C ) Which is now correct. Now how can I get that to work with the $z['Short'] and $z['Long'] So if $a == $z['Short'] it will display the corresponding $z['Long'] Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 Now how can I get that to work with the $z['Short'] and $z['Long'] So if $a == $z['Short'] it will display the corresponding $z['Long'] Sorry? What do you want to do? Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 I have a function that pulls the values for Short and Long from a database. So for example a Short would be A, Long would be Tiger So I want that if the $a is A, it would display Tiger instead of A I'm thinking of using a foreach to set the values for Short and Long. I tried: foreach ($page['references_amenities'] as $z) { $z['Short'] = $z['Long']; } But that does not work. Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 Example. <?php // connect to db $sql = "SELECT short,long FROM tbl"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $values = array(); while($row = mysql_fetch_assoc($result)) { $values[$row['short']] = $row['long']; } } } ?> Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 This is what I currently use to bring the array of Long and Short in. It pulls from the database. Is there anyway I can create a relationship between the two here? foreach ($page['references_amenities'] as $z) { $z['Short'] = $z['Long']; } Then - how do I make it so if the $a from the $APPLIANCES array = Short, it will display Long ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.