malikah Posted January 6, 2009 Share Posted January 6, 2009 I have a list of variables: $varibl01="abc"; $varibl02="def"; $varibl03="ghi"; $varibl04="jkl"; $varibl05="mno"; and I want to access each of them in a for loop: for($i=0;$i<5;$i++){ echo $varibl0x //where "x" is $i - is there a way to do this? } Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/ Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 Better to use an array for this but yea... for($i=0;$i<5;$i++){ $newvar = "varibl0" . $x; echo $$newvar; //where "x" is $i - is there a way to do this? } Not sure if that will work, I think it will but yea... Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730481 Share on other sites More sharing options...
DarkerAngel Posted January 6, 2009 Share Posted January 6, 2009 Is there no way you can make this an array and have $varibl = array(); $varibl[01]="abc"; $varibl[02]="def"; $varibl[03]="ghi"; $varibl[04]="jkl"; $varibl[05]="mno"; then you can simply use: foreach($varibl as $value) { echo $value; } There is a way to use the eval(); function but thats not very trustworthy coding. Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730483 Share on other sites More sharing options...
malikah Posted January 6, 2009 Author Share Posted January 6, 2009 Here's the original code I'm playing with: while (list($a1, $b1, $a2, $b2, $a3, $b3, $a4, $b4, $a5, $b5) = mysql_fetch_row($xyzQuery)) { //I want to refer to the $a's here.. } Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730487 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 while ($row = mysql_fetch_row($xyzQuery)) { foreach ($row as $val) { echo $val; } } That makes it easier =) Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730489 Share on other sites More sharing options...
malikah Posted January 6, 2009 Author Share Posted January 6, 2009 while ($row = mysql_fetch_row($xyzQuery)) { foreach ($row as $val) { echo $val; } } That makes it easier =) I don't want to change the original code - I want to access each of the $a's in a for loop by referring to their last digit. Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730492 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 Instead why not just access them via the array index??? while ($row = mysql_fetch_row($xyzQuery)) { for ($i=0;$i<5;$i++) { echo $row[$i]; } } Same principle, just easier and more efficient to do.... What is your logic behind assigning them to variables and not just using the array index? Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730494 Share on other sites More sharing options...
malikah Posted January 6, 2009 Author Share Posted January 6, 2009 Because I don't know what $row is - what happened to the list? Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730496 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 Because I don't know what $row is - what happened to the list? The list is not needed, that just broke out the array. $row is an array that contains all the data that list broke out inside of it's self to be accessed as shown above. I would suggest reading up on array's They are like a storage container for data to be easily access/iterated through. Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730497 Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 If you want to refer to them by there last digits then you need to take the sub string from the actual variable name. This, for example, will display all the variables with a 1 at the end of the variable. *NOT TESTED* while ($row = mysql_fetch_row($xyzQuery)) { for ($i=0;$i if(substr($$row[$i], 1) == 1) { echo $row[$i]; } } } or you can access by first letter... $string = $$row[$i]; if($string[1] == 'a') { echo $row[$i]; } Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730498 Share on other sites More sharing options...
malikah Posted January 6, 2009 Author Share Posted January 6, 2009 Premiso - $row worked, thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730503 Share on other sites More sharing options...
malikah Posted January 6, 2009 Author Share Posted January 6, 2009 Only problem now is that I have to go back and find out which table item each $row[$i] refers to - whereas unique names for each item would have made it simpler. I guess it's hard to win either way. Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730510 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 You can use mysql_fetch_assoc and call them by the column names, or simply pull them out of the DB in the order you want. Quote Link to comment https://forums.phpfreaks.com/topic/139622-for-loop-of-variables/#findComment-730511 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.