timmah1 Posted January 17, 2009 Share Posted January 17, 2009 I have some info in the database that is like this 54,9741,95,1 I'm trying to pull that info out with explode. Then, I'm trying to check it against check boxes, and if it equals the value, it checks the box. It does this fine, but the problem is that it loops everything 9 times because there are 9 items. Then it checks the appropriate boxes, but each group is looped. I don't know how to avoid this. Here is my code <table width="100%" cellpadding="4" cellspacing="0"> <?php $medical = explode(',', $charts['medication']); for($i = 0; $i < count($medical); $i++) { ?> <tr> <td colspan="2"><font size="2"><input <?php if (!(strcmp($medical[$i],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="1"> Clomid</font></td> <td colspan="2"><font size="2"><input <?php if (!(strcmp($medical[$i],128))) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="128">IVF Transfer</font></td> </tr> <td colspan="2"><font size="2"><input <?php if (!(strcmp($medical[$i],2))) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="2">Estrogen</font></td> <td colspan="2"><font size="2"><input <?php if (!(strcmp($medical[$i],32))) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="32">Metformin</font></td> </tr> <td colspan="2"><font size="2"><input <?php if (!(strcmp($medical[$i],256))) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="256">FSH</font></td> <td colspan="2"><font size="2"><input <?php if (!(strcmp($medical[$i],16))) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="16">Other Medications</font></td> </tr> <td colspan="2"><font size="2"><input <?php if (!(strcmp($medical[$i],512))) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="512">Femara/Letrozole</font></td> <td colspan="2"><font size="2"><input <?php if (!(strcmp($medical[$i],)) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="8">Progesterone</font></td> </tr> <td colspan="2"><font size="2"><input <?php if (!(strcmp($medical[$i],4))) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="4">HCG</font></td> <td colspan="2"> </td> </tr> <?php } ?> </table> How would I do this? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/141253-solved-for-looping-issue/ Share on other sites More sharing options...
Mark Baker Posted January 17, 2009 Share Posted January 17, 2009 You've extracted the comma-separated string into an array Get rid of your for loop, and use in_array(1,$medical) instead of strcmp($medical[$i],1) Quote Link to comment https://forums.phpfreaks.com/topic/141253-solved-for-looping-issue/#findComment-739340 Share on other sites More sharing options...
timmah1 Posted January 17, 2009 Author Share Posted January 17, 2009 Not sure I understand, because now it don't show that any were checked <table width="100%" cellpadding="4" cellspacing="0"> <?php $medical = explode(',', $charts['medication']); ?> <tr> <td colspan="2"><font size="2"><input <?php if (!in_array(1,$medical)) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="1"> Clomid</font></td> <td colspan="2"><font size="2"><input <?php if (!in_array(128,$medical)) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="128">IVF Transfer</font></td> </tr> <td colspan="2"><font size="2"><input <?php if (!in_array(2,$medical)) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="2">Estrogen</font></td> <td colspan="2"><font size="2"><input <?php if (!in_array(32,$medical)) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="32">Metformin</font></td> </tr> <td colspan="2"><font size="2"><input <?php if (!in_array(256,$medical)) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="256">FSH</font></td> <td colspan="2"><font size="2"><input <?php if (!in_array(16,$medical)) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="16">Other Medications</font></td> </tr> <td colspan="2"><font size="2"><input <?php if (!in_array(512,$medical)) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="512">Femara/Letrozole</font></td> <td colspan="2"><font size="2"><input <?php if (!in_array(8,$medical)) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="8">Progesterone</font></td> </tr> <td colspan="2"><font size="2"><input <?php if (!in_array(4,$medical)) {echo "checked=\"checked\"";} ?> type="checkbox" name="medication[]" value="4">HCG</font></td> <td colspan="2"> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/141253-solved-for-looping-issue/#findComment-739348 Share on other sites More sharing options...
timmah1 Posted January 17, 2009 Author Share Posted January 17, 2009 I got it. Thanks mark Quote Link to comment https://forums.phpfreaks.com/topic/141253-solved-for-looping-issue/#findComment-739354 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.