dannybrazil Posted March 8, 2010 Share Posted March 8, 2010 Hello I need help, I guess Im doing something wrong I have an array of strings : *it will be like that cus i will have to use 2 languages $type["1"]="London"; $type["2"]="New York"; etc... now when someone is searching for a city it gets the number from the database $city="1"; (for example) then I want to print the city's name: <? echo ' $type["$city"] '; ?> and it doesnt work well I know its not the real way....but Im trying any help ? Quote Link to comment https://forums.phpfreaks.com/topic/194497-string-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 8, 2010 Share Posted March 8, 2010 echo ' $type["$city"] '; Variables inside of single-quotes are not parsed and replaced by their actual value (i.e. the initial and final single-quotes.). Also, if the only thing inside of a string is a variable, no need for any quotes (i.e. the double-quotes around $city.) echo $type[$city]; Quote Link to comment https://forums.phpfreaks.com/topic/194497-string-help/#findComment-1022982 Share on other sites More sharing options...
dannybrazil Posted March 8, 2010 Author Share Posted March 8, 2010 I tried it manually and it doesnt work as well its like that <head> $type["1"]="London"; $type["2"]="New York"; </head> <body> <? echo ' <div> '.$type["1"].' </div> '; ?> </body> I guess it suppose to print London , isnt it ? Quote Link to comment https://forums.phpfreaks.com/topic/194497-string-help/#findComment-1022991 Share on other sites More sharing options...
PFMaBiSmAd Posted March 8, 2010 Share Posted March 8, 2010 The 'code' you just posted does not have any php tags around the lines setting $type and produces this output - $type["1"]="London"; $type["2"]="New York"; Notice: Undefined variable: type in your_file.php on line 9 If you put php tags around the php code, it works as expected and produces - London Quote Link to comment https://forums.phpfreaks.com/topic/194497-string-help/#findComment-1022997 Share on other sites More sharing options...
dannybrazil Posted March 8, 2010 Author Share Posted March 8, 2010 there is my real code while($row = mysql_fetch_array($result)) { $id=$row['id']; $t=$row['type']'; //TYPE $type["1"] = "דירה"; $type["2"] = "דירת גג"; $type["3"] = "לופט"; $type["4"] = "דירת גן"; $type["5"] = "דירת סטודיו"; $type["6"] = "דו משפחתי"; $type["7"] = "בית פרטי"; $type["8"] = "פנטהאוז"; $type["9"] = "דופלקס"; $type["10"] = "טריפלקס"; $type["11"] = "וילה"; $type["12"] = "קוטג"; $type["13"] = "קראוון"; $type["14"] = "יחידת דיור"; $type["15"] = "מגרש למגורים"; $type["16"] = "משק חקלאי"; echo '<table border="0" width="800px" style="direction:rtl; margin-left:auto; margin-right:auto; background-color:#0188A6; margin-top:20px;text-align:right;">'; echo '<tr>'; echo '<td>'; $pic_1=$row['upload_1']; echo '<a href="#" class="img_brdr" style=" float:right;"><img border="0" src="imgsize.php?h=100&img='.$pic_1.'"/></a> <div style=" float:right; margin-right:20px; font-size:12px;"> '.$type[$t].' </div>'; what I get no is ONLY THE FIRST LETTER of the string any idea why ? Quote Link to comment https://forums.phpfreaks.com/topic/194497-string-help/#findComment-1023003 Share on other sites More sharing options...
PFMaBiSmAd Posted March 8, 2010 Share Posted March 8, 2010 Since you have marked this as solved, I'll guess you found the problem, but the posted code cannot be your actual code because it has an extra single-quote that would cause a fatal parse error. Once that problem was fixed, your code worked for me as expected. Quote Link to comment https://forums.phpfreaks.com/topic/194497-string-help/#findComment-1023014 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.