Seven_Rings Posted October 14, 2008 Share Posted October 14, 2008 I'm just wondering how I would use a string as part of the name of a new string, like this... $*insert string*sel = "selected"; Meaning that I have a string, and say the value is "blue". It's pulling the value "blue" from a database, and depending whats in the database, I need a string named $bluesel. I'm acually having a few problems like this, and I cant find anything on Google that puts my in the right direction. Thanks, -Seven_Rings Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/ Share on other sites More sharing options...
Bendude14 Posted October 14, 2008 Share Posted October 14, 2008 well to concatanate one string to another you can use a period like so <?php $var1 = "hello my name is"; $name = "Ben"; echo "$var1 " . $name; ?> Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664919 Share on other sites More sharing options...
Seven_Rings Posted October 14, 2008 Author Share Posted October 14, 2008 Hey. I didnt think about it this way. And I think this might work. You know how when you are coding, its like a plan, and I overlooked this. I really appreciate the help. While I'm here, I might ask one more thing... $result = mysql_query("SELECT * FROM table") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { //How do I use a string in here? echo $row[$string . 'level']; } I have tried a few differant things, none of which work. These are two examples. echo $row['$stringlevel']; echo $row[$string . 'level']; I am trying to pull certain results from my table. And those results need to be defined by that string. If the string = seven, than I want to pull all entrys from sevenlevel. Any ideas? *This is acually from a post I made last night, that nobody has responded too. Thanks, -Seven_Rings Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664922 Share on other sites More sharing options...
Zhadus Posted October 14, 2008 Share Posted October 14, 2008 I think I understand what you're trying to do there, you can try: $stringlevel = $string . 'level'; $result = mysql_query("SELECT * FROM table") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { //How do I use a string in here? echo $row['$stringlevel']; } Then if $string is blue, it would be: echo $row['bluelevel']; Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664928 Share on other sites More sharing options...
.josh Posted October 14, 2008 Share Posted October 14, 2008 If you have a column called for instance 'sevenlevel' and $string == 'seven' then echo $row[$string . 'level']; Should have worked. Alternatively you could do echo $row["{$string}level"]; Or concat it beforehand like Zhadus mentioned. Perhaps $string does not contain the value you are expecting? Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664930 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 If I understand correctly you should be defining $String before you try to extract information from the database, like: $color = "blue"; $string = $color . "level"; then: echo $row['$string']; Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664931 Share on other sites More sharing options...
.josh Posted October 14, 2008 Share Posted October 14, 2008 @Maq: if you notice at the top of the code snippet, $stringlevel is defined, doing the same thing you suggested. Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664932 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 @Maq: if you notice at the top of the code snippet, $stringlevel is defined, doing the same thing you suggested. I don't see anything... Yeah I know I just saw that myself. *edited* Sorry Zhadus Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664935 Share on other sites More sharing options...
Seven_Rings Posted October 14, 2008 Author Share Posted October 14, 2008 Alright... I figured out that problem atleast, and you guys may be disappointed with the results. The table name wasnt capitalized, and the string value was. How can I get around this? How can I make the string lowercase? I am getting the value of the string from... <select> <option>Blue</option> <option>Green</option> </select> If I changed that too... <select> <option value="blue">Blue</option> <option value="green">Green</option> </select> Would that work? Thanks, -Seven_Rings Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664941 Share on other sites More sharing options...
trq Posted October 14, 2008 Share Posted October 14, 2008 strtolower() might help you. Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664946 Share on other sites More sharing options...
Seven_Rings Posted October 14, 2008 Author Share Posted October 14, 2008 *Thorpe, though the problem is fixed, I still might do that to save alot of code. So thanks Let me answer myself, It is working. And its all thanks to you guys The last big thing on my plate right now is this, I'm sorry to do this to you guys, but I am still having trouble with the first thing. But this time, I will give alot more example. My code is something like this... <? $color = $_POST["color"]; $submit = "INSERT INTO test (currentcolor) VALUES('$color')"; mysql_query($currentskillsubmit); $result = mysql_query("SELECT currentcolor FROM test WHERE id = 1") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { $*$row[currentcolor]*sel = "selected"; } ?> <form enctype="multipart/form-data" action="index.php" method="POST"> <select name="color"> <option <? echo $bluesel; ?>>Blue</option> <option <? echo $greensel; ?>>Green</option> <select> </form> When a color is changed and submitted, its stored in the database. And the next time the page is refreshed, the database is read, and the last color selected needs to be "selected" in the drop down. So if the database reads "blue", I need to set a string $bluesel = "selected"; The problem is, that I need to make a string, based on the value of anouther string, and I dont know how to do that. // This would make the current color from the database selected. The problem is that this isnt corrent syntax. $$row[currentcolor]sel = "selected"; I hope I'm not annoying you guys with my questions, but I really cant find answers anywhere else. Thanks a lot, -Seven_Rings Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664951 Share on other sites More sharing options...
thebadbad Posted October 14, 2008 Share Posted October 14, 2008 I think you're looking for a variable variable: <?php ${$row['currentcolor'] . 'sel'} = "selected"; ?> If $row['currentcolor'] is e.g. 'blue', $bluesel will be set to 'selected'. Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664955 Share on other sites More sharing options...
thebadbad Posted October 14, 2008 Share Posted October 14, 2008 Another small thing; the right (X)HTML syntax is selected="select". <?php ${$row['currentcolor'] . 'sel'} = 'selected="selected"'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664958 Share on other sites More sharing options...
trq Posted October 14, 2008 Share Posted October 14, 2008 <?php $color = $_POST["color"]; $submit = "INSERT INTO test (currentcolor) VALUES('$color')"; mysql_query($currentskillsubmit); if ($result = mysql_query("SELECT currentcolor FROM test WHERE id = 1")) { if (mysql_num_rows($result)) { $row = mysql_fetch_array( $result )) { } } ?> <form enctype="multipart/form-data" action="index.php" method="POST"> <select name="color"> <option <?php echo (isset($row['currentcolor']) && $row['currentcolor'] == 'Blue') ? '"selected=select"' : ""; ?>>Blue</option> <option <?php echo (isset($row['currentcolor']) && $row['currentcolor'] == 'Green') ? '"selected=select"' : ""; ?>>Green</option> <select> </form> Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664964 Share on other sites More sharing options...
Seven_Rings Posted October 14, 2008 Author Share Posted October 14, 2008 It works great, thanks guys. Now I just have to figure out how to subtract an integer(in a string) from anouther one, and display the result. Thanks for the help, -Seven_Rings Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-664993 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 Now I just have to figure out how to subtract an integer(in a string) from anouther one, and display the result. Example please, this is really easy in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-665007 Share on other sites More sharing options...
CroNiX Posted October 14, 2008 Share Posted October 14, 2008 $one="1"; //string $four="4"; //string echo (int)$four - (int)$one; //cast them as integers and subtract them Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-665057 Share on other sites More sharing options...
.josh Posted October 14, 2008 Share Posted October 14, 2008 Actually, since PHP is a loosely typed language, you don't need to force type casts if that's all that's in your vars are the numbers. Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-665059 Share on other sites More sharing options...
Seven_Rings Posted October 19, 2008 Author Share Posted October 19, 2008 I have actually had this working for a bit, didn't see the page two. I really appreciate you guys. Will a mod mark this solved so people can learn from it? Thanks, -Seven_Rings Quote Link to comment https://forums.phpfreaks.com/topic/128364-solved-using-a-string-as-part-of-a-string-name/#findComment-669280 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.