AdRock Posted April 7, 2010 Share Posted April 7, 2010 I have a longl list of if statements and the only thing that is different is the variable name. The variable name itself is the same but just a number at the end of the variable changes experiece0, experience1, experience2.....experience9 This is the current code if(!empty($experience1)) { $experience1 = explode(",",$experience1); echo "<tr><td> </td>"; foreach($experience1 as $exp1) { echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp1))."</td>"; } echo "</tr>"; } if(!empty($experience2)) { $experience2 = explode(",",$experience2); echo "<tr><td> </td>"; foreach($experience2 as $exp2) { echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp2))."</td>"; } echo "</tr>"; } ... ... ... ... if(!empty($experience3)) { $experience3 = explode(",",$experience3); echo "<tr><td> </td>"; foreach($experience3 as $exp3) { echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp3))."</td>"; } echo "</tr>"; } i want to make it more efficient by using a for loop and incrementing the variable number like this but it doesn't work for($i=1;$i<9;$i++) { if(!empty($experience.$i)) { $experience.$i = explode(",",$experience.$i); echo "<tr><td>&nbs;</td>"; foreach($experience.$i as $exp.$i) { echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp.$i))."</td>"; } echo "</tr>"; } } i get this error message Parse error: parse error, expecting `')'' in C:\Apache\htdocs\folk\view-volunteer.php on line 119 Quote Link to comment https://forums.phpfreaks.com/topic/197938-make-thi-smore-efficient-using-a-for-loop/ Share on other sites More sharing options...
teamatomic Posted April 7, 2010 Share Posted April 7, 2010 That wont work, as you found out. Put them into an array and your second solution will work fine. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/197938-make-thi-smore-efficient-using-a-for-loop/#findComment-1038658 Share on other sites More sharing options...
AdRock Posted April 7, 2010 Author Share Posted April 7, 2010 Can i use ${$var1.$var2} Quote Link to comment https://forums.phpfreaks.com/topic/197938-make-thi-smore-efficient-using-a-for-loop/#findComment-1038659 Share on other sites More sharing options...
AdRock Posted April 7, 2010 Author Share Posted April 7, 2010 What do you mean put them into an array....i don't think i understand....put what into an array? Quote Link to comment https://forums.phpfreaks.com/topic/197938-make-thi-smore-efficient-using-a-for-loop/#findComment-1038662 Share on other sites More sharing options...
teamatomic Posted April 7, 2010 Share Posted April 7, 2010 $experience=array(1=>'whatever $experience1 is',2=>'whatever $experience2 is', and so on for each $experience[x]. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/197938-make-thi-smore-efficient-using-a-for-loop/#findComment-1038673 Share on other sites More sharing options...
premiso Posted April 8, 2010 Share Posted April 8, 2010 How are your variable names being populated? Manually or from GET/POST, if from GET/POST that means you either extracted the array of have register_globals on, both of which are not good practice. That and you can easily manipulate / use them in an array better then you can as a variable. Incase that is the case here is an example: for($i=1;$i<9;$i++) { if(!empty($_POST['experience' . $i])) { $experience = explode(",",$_POST['experience' . $i); echo "<tr><td>&nbs;</td>"; foreach($experience as $exp) { echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp))."</td>"; } echo "</tr>"; } } If register_globals are not on and the original data is not in array but is defined this would work: for($i=1;$i<9;$i++) { $experience = ${$experience . $i}; if(!empty($experience) { $experience = explode(",",$experience); echo "<tr><td>&nbs;</td>"; foreach($experience as $exp) { echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp))."</td>"; } echo "</tr>"; } } Both untested, but hopefully gives you a working theory. Quote Link to comment https://forums.phpfreaks.com/topic/197938-make-thi-smore-efficient-using-a-for-loop/#findComment-1038695 Share on other sites More sharing options...
AdRock Posted April 8, 2010 Author Share Posted April 8, 2010 for($i=1;$i<9;$i++) { $experience = ${$experience . $i}; if(!empty($experience) { $experience = explode(",",$experience); echo "<tr><td>&nbs;</td>"; foreach($experience as $exp) { echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp))."</td>"; } echo "</tr>"; } } Didn't work I tried something similar but it displays nothing Quote Link to comment https://forums.phpfreaks.com/topic/197938-make-thi-smore-efficient-using-a-for-loop/#findComment-1038700 Share on other sites More sharing options...
premiso Posted April 8, 2010 Share Posted April 8, 2010 I have 10 fields in the database Given that you have 10 fields in the database you can get this data out via an array. Somewhat psuedo code here is how it would work: while ($row = mysql_fetch_assoc($result)) { for($i=1;$i<9;$i++) { $experience = $row['experience' . $i]; if(!empty($experience) { $experience = explode(",",$experience); echo "<tr><td>&nbs;</td>"; foreach($experience as $exp) { echo "<td class='fields'>".ucwords(str_replace('_',' ',$exp))."</td>"; } echo "</tr>"; } } } And it should work, given that you have valid queries and are fetching the data properly. Without seeing the full script, I just have to make guesses so yea. Quote Link to comment https://forums.phpfreaks.com/topic/197938-make-thi-smore-efficient-using-a-for-loop/#findComment-1038706 Share on other sites More sharing options...
AdRock Posted April 8, 2010 Author Share Posted April 8, 2010 Thanks premiso....it now works. I was using extract on teh $row and it didn't like it....put the code in the while loop and it worked Quote Link to comment https://forums.phpfreaks.com/topic/197938-make-thi-smore-efficient-using-a-for-loop/#findComment-1038710 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.