![]() |
variable name change in a loop - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: variable name change in a loop (/showthread.php?tid=12676) Pages:
1
2
|
variable name change in a loop - El Forum - 10-27-2008 [eluser]earlyriser[/eluser] Hi. I would like to change a variable name in a loop, from a view file but I cann't get it work. My code is Code: <?=$row->keyword1?>,<?=$row->keyword2?>,<?=$row->keyword3?>,<?=$row->keyword4?> The first line is for testing purposes and it writes some keywords from the query results rows. It works fine. With the FOR loop I want to have the same result, but I got and error: Severity: Notice Message: Undefined variable: row->keyword1 What is the correct way to do this? I have tried also echo ${'$row->keyword'.$counter}; with the same error Thanks. variable name change in a loop - El Forum - 10-27-2008 [eluser]roj[/eluser] Would it not simply be echo $row->keyword.$counter within the loop? variable name change in a loop - El Forum - 10-27-2008 [eluser]earlyriser[/eluser] [quote author="roj" date="1225169878"]Would it not simply be echo $row->keyword.$counter within the loop?[/quote] Thanks for your reply roj, I also tried that, and I got the error: Severity: Notice Message: Undefined property: keyword variable name change in a loop - El Forum - 10-27-2008 [eluser]wr5aw[/eluser] Maybe something like this: Code: <?php for ( $counter = 1; $counter < 5; $counter++) variable name change in a loop - El Forum - 10-28-2008 [eluser]roj[/eluser] Was going to add that too as it's essentially the same thing so i'm guessing it won't work either? Question now: is the testing bit that works being executed in the same file as the loop? In the model, controller or view? variable name change in a loop - El Forum - 10-28-2008 [eluser]bigtimslim[/eluser] Maybe this is a design problem and your db results should be filtered to 1-5 before they hit the view. What do the controller and model look like? variable name change in a loop - El Forum - 10-28-2008 [eluser]dmiden[/eluser] Hi, Use wr5aw's code. Code: $keyword = 'keyword'.$counter; Code: echo ${'row->keyword'.$counter}; Basically you can't use "->" in ${""}. variable name change in a loop - El Forum - 10-28-2008 [eluser]roj[/eluser] Just for my own clarity: you could use Code: echo $row->keyword.$counter; right? variable name change in a loop - El Forum - 10-28-2008 [eluser]dmiden[/eluser] No. Not in the way you think. Using the . will just merge them e.g. Code: $this->var1 = 'foo'; So it's basically $row->keyword . $counter; and $row->keyword is empty, so it just echoes $counter. variable name change in a loop - El Forum - 10-28-2008 [eluser]roj[/eluser] Ah, that's what i get for trying to be smart....! Thanks for the clarity. |