Monkuar Posted January 16, 2012 Share Posted January 16, 2012 my code: echo $Profile['display']; ECHO's out: 0|1|0|1 Now I want to explode these, which is easy $display = explode("|", $Profile['display']); But Now I want to use a php function to check if any of the array's = "1" and have the value set to $value= "checked"; I could do this by doing if ($display['0'] == "1"){ $value = "checked"; } and then 1 2 3 and so on.. but I want a faster way, maybe a for loop, or a simpliar easier way to just check if each array = 1, make $value = checked Possible right? Link to comment https://forums.phpfreaks.com/topic/255164-is-there-a-faster-way/ Share on other sites More sharing options...
scootstah Posted January 16, 2012 Share Posted January 16, 2012 How about $value = $display[0] == 1 ? 'checked' : ''; Link to comment https://forums.phpfreaks.com/topic/255164-is-there-a-faster-way/#findComment-1308313 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 How about $value = $display[0] == 1 ? 'checked' : ''; o.o simply terny operator would work but then i would have to use $value = $display[0] == 1 ? 'checked' : ''; $value = $display[1] == 1 ? 'checked' : ''; $value = $display[2] == 1 ? 'checked' : ''; but hell w/e i guess it works thx Link to comment https://forums.phpfreaks.com/topic/255164-is-there-a-faster-way/#findComment-1308319 Share on other sites More sharing options...
scootstah Posted January 16, 2012 Share Posted January 16, 2012 Remember that if you do that, you are changing the assignment of $value each time... so it's only going to be assigned to the last ternary statement. Is this for checkboxes or something? If so you could do something like: echo '<input type="checkbox" ' . ($display[0] == 1 ? 'checked="checked"' : '') . ' />'; Link to comment https://forums.phpfreaks.com/topic/255164-is-there-a-faster-way/#findComment-1308322 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 Remember that if you do that, you are changing the assignment of $value each time... so it's only going to be assigned to the last ternary statement. Is this for checkboxes or something? If so you could do something like: echo '<input type="checkbox" ' . ($display[0] == 1 ? 'checked="checked"' : '') . ' />'; yea but that would still only do $display['0'] im looking for a forloop method to iniative all 4 arrays and go through each one it's fine tho the ternary operator works fine, i just changed $value to $value1 $value2 $value3 then used them on my html return <<<EOF functon Link to comment https://forums.phpfreaks.com/topic/255164-is-there-a-faster-way/#findComment-1308324 Share on other sites More sharing options...
PaulRyan Posted January 16, 2012 Share Posted January 16, 2012 Like this? <?PHP //### Dummy Data $Profile['display'] = '1|0|1|0'; $display = explode("|", $Profile['display']); foreach($display AS $key => $value) { $show = $value == 1 ? 'checked' : '' ; echo '<input type="checkbox" name="check" '.$show.'> '.$key.' <br>'; } ?> Regards, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/255164-is-there-a-faster-way/#findComment-1308325 Share on other sites More sharing options...
scootstah Posted January 16, 2012 Share Posted January 16, 2012 Remember that if you do that, you are changing the assignment of $value each time... so it's only going to be assigned to the last ternary statement. Is this for checkboxes or something? If so you could do something like: echo '<input type="checkbox" ' . ($display[0] == 1 ? 'checked="checked"' : '') . ' />'; yea but that would still only do $display['0'] im looking for a forloop method to iniative all 4 arrays and go through each one it's fine tho the ternary operator works fine, i just changed $value to $value1 $value2 $value3 then used them on my html return <<<EOF functon But you are writing all the checkboxes anyway, right? Link to comment https://forums.phpfreaks.com/topic/255164-is-there-a-faster-way/#findComment-1308328 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 Remember that if you do that, you are changing the assignment of $value each time... so it's only going to be assigned to the last ternary statement. Is this for checkboxes or something? If so you could do something like: echo '<input type="checkbox" ' . ($display[0] == 1 ? 'checked="checked"' : '') . ' />'; yea but that would still only do $display['0'] im looking for a forloop method to iniative all 4 arrays and go through each one it's fine tho the ternary operator works fine, i just changed $value to $value1 $value2 $value3 then used them on my html return <<<EOF functon But you are writing all the checkboxes anyway, right? yeah each 1 so i guess the ternary operator is awesome makes the code small and compact, i love it thanks for sharing! Link to comment https://forums.phpfreaks.com/topic/255164-is-there-a-faster-way/#findComment-1308329 Share on other sites More sharing options...
PFMaBiSmAd Posted January 16, 2012 Share Posted January 16, 2012 <?php $Profile['display'] = '1|0|1|0'; foreach(explode('|',$Profile['display']) as $key=>$value){ echo "<input type='checkbox' name='profile[$key]' " . ($value == 1 ? 'checked="checked"' : '') . ' />'; } Link to comment https://forums.phpfreaks.com/topic/255164-is-there-a-faster-way/#findComment-1308332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.