drdysdy Posted August 29, 2009 Share Posted August 29, 2009 So what I'm trying to do is combine multiple variables to create the name of one variable. A friend of mine explained to me how to do it years ago, but I don't really remember how it was done. He did it something like this: <? $rownum = "1"; $linenum = "1"; $row.$$rownum.$line.$$linenum = "blank.jpg"; ?> <<? echo "img src=$row1line1"; ?> alt="" name="Row1Line1" width="50" height="50" id="Row1Line1" /> I'm not sure what it is, but I'm doing something wrong. Quote Link to comment https://forums.phpfreaks.com/topic/172357-using-variable-variables/ Share on other sites More sharing options...
oni-kun Posted August 29, 2009 Share Posted August 29, 2009 I'm not sure what you're trying to do.. You can do $var1, $var2, $var3 = 'this'; If you mean combining them together, yes, use the fullstop.. $var 1 = 'a'; $var2 = 'b'; $var1.$var2 //outputs 'ab' Or create a variable out of the rest.. $varc = $vara.$varb Don't know what you're meaning though.. Quote Link to comment https://forums.phpfreaks.com/topic/172357-using-variable-variables/#findComment-908767 Share on other sites More sharing options...
drdysdy Posted August 29, 2009 Author Share Posted August 29, 2009 I'm not sure what you're trying to do.. You can do $var1, $var2, $var3 = 'this'; If you mean combining them together, yes, use the fullstop.. $var 1 = 'a'; $var2 = 'b'; $var1.$var2 //outputs 'ab' Or create a variable out of the rest.. $varc = $vara.$varb Don't know what you're meaning though.. I'm trying to create a variable called $row1line1, but I need to do it dynamically because I also want to create more variables like $row1line2. So I need to use variables as the name of another variable. Sorry, I fail at explaining right now, it's a little late. Quote Link to comment https://forums.phpfreaks.com/topic/172357-using-variable-variables/#findComment-908768 Share on other sites More sharing options...
PFMaBiSmAd Posted August 29, 2009 Share Posted August 29, 2009 If you need a set of related variables (your rownum and linenum), you should be using an array. Variable variables are 3 x slower than using an array, so you will waste time first making the variable variables, then you will waste more time when you have to keep track of what exactly was created so you can iterate through them when you use them in your code. If you post an example of what data you have (and where it is coming from) and what you are trying to produce, someone can show the most efficient way of producing the end result. Quote Link to comment https://forums.phpfreaks.com/topic/172357-using-variable-variables/#findComment-908770 Share on other sites More sharing options...
drdysdy Posted August 29, 2009 Author Share Posted August 29, 2009 If you need a set of related variables (your rownum and linenum), you should be using an array. Variable variables are 3 x slower than using an array, so you will waste time first making the variable variables, then you will waste more time when you have to keep track of what exactly was created so you can iterate through them when you use them in your code. If you post an example of what data you have (and where it is coming from) and what you are trying to produce, someone can show the most efficient way of producing the end result. Well what I'm trying to do is set what image should be displayed. I have a 9x9 table filled with images: <table border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><table border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><<? echo "img src=$row1line1"; ?> alt="" name="Row1Line1" width="50" height="50" id="Row1Line1" /></td> <td>< <? echo "img src=$row1line2"; ?> alt="" name="Row1Line2" width="50" height="50" id="Row1Line2" /></td> <td>< <? echo "img src=$row1line3"; ?> alt="" name="Row1Line3" width="50" height="50" id="Row1Line3" /></td> <td>< <? echo "img src=$row1line4"; ?> alt="" name="Row1Line4" width="50" height="50" id="Row1Line4" /></td> <td>< <? echo "img src=$row1line5"; ?> alt="" name="Row1Line5" width="50" height="50" id="Row1Line5" /></td> <td>< <? echo "img src=$row1line6"; ?> alt="" name="Row1Line6" width="50" height="50" id="Row1Line6" /></td> <td>< <? echo "img src=$row1line7"; ?> alt="" name="Row1Line7" width="50" height="50" id="Row1Line7" /></td> <td>< <? echo "img src=$row1line8"; ?> alt="" name="Row1Line8" width="50" height="50" id="Row1Line8" /></td> <td>< <? echo "img src=$row1line9"; ?> alt="" name="Row1Line9" width="50" height="50" id="Row1Line9" /></td> </tr> <tr> <td>< <? echo "img src=$row2line1"; ?> alt="" name="Row2Line1" width="50" height="50" id="Row2Line1" /></td> <td>< <? echo "img src=$row2line2"; ?> alt="" name="Row2Line2" width="50" height="50" id="Row2Line2" /></td> <td>< <? echo "img src=$row2line3"; ?> alt="" name="Row2Line3" width="50" height="50" id="Row2Line3" /></td> <td>< <? echo "img src=$row2line4"; ?> alt="" name="Row2Line4" width="50" height="50" id="Row2Line4" /></td> <td>< <? echo "img src=$row2line5"; ?> alt="" name="Row2Line5" width="50" height="50" id="Row2Line5" /></td> <td>< <? echo "img src=$row2line6"; ?> alt="" name="Row2Line6" width="50" height="50" id="Row2Line6" /></td> <td>< <? echo "img src=$row2line7"; ?> alt="" name="Row2Line7" width="50" height="50" id="Row2Line7" /></td> <td>< <? echo "img src=$row2line8"; ?> alt="" name="Row2Line8" width="50" height="50" id="Row2Line8" /></td> <td>< <? echo "img src=$row2line9"; ?> alt="" name="Row2Line9" width="50" height="50" id="Row2Line9" /></td> </tr> etc... I'm trying to create a 2d map, each image will be a tile in the map. The center tile is controlled by the user, and each other tile is set depending on where the user is. If there is an object at that coordinate, it will display an image, otherwise it will display the default image (blank.jpg) I'm not sure how I could use an array to do this. Quote Link to comment https://forums.phpfreaks.com/topic/172357-using-variable-variables/#findComment-908787 Share on other sites More sharing options...
oni-kun Posted August 29, 2009 Share Posted August 29, 2009 Use a FOR loop to iterate through the variables.. within the for loop, you'd put something like.. <td><<? <table..> <?php for( $i=1;$i<=20;$i++) { echo "img src=$row1line$i"; ?> alt="" name="Row1Line1" width="50" height="50" id="Row1Line<?php echo $i; ?>" /></td> <?php } ?> To be honest I'm not sure if $row1line$i would work, you may have to use eval() or some trick. Quote Link to comment https://forums.phpfreaks.com/topic/172357-using-variable-variables/#findComment-908802 Share on other sites More sharing options...
PFMaBiSmAd Posted August 29, 2009 Share Posted August 29, 2009 <?php $rows = 9; $lines = 9; $map = array(); // setup the array of rows/lines - for($row=1;$row <= $rows; $row++){ for($line=1;$line <= $lines; $line++){ $map[$row][$line] = 'blank.jpg'; // setup default image } } // you can then set any row/line as follows (or any other method that supplies a row/column and value)- $row = 4; $line = 6; $map[$row][$line] = 'some_other_image.jpg'; // similar idea specifying the numbers directly, row 3, line 1 $map[3][1] = 'some_other_image.jpg'; // output the map - echo "<table border='0' align='center' cellpadding='0' cellspacing='0'>\n"; for($row=1;$row <= $rows; $row++){ echo "<tr>\n"; for($line=1;$line <= $lines; $line++){ echo "<td><img src='{$map[$row][$line]}' alt='' name='Row{$row}Line{$line}' width='50' height='50' id='Row{$row}Line{$line}' /></td>\n"; } echo "</tr>\n"; } echo "</table>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/172357-using-variable-variables/#findComment-908820 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.