glenelkins Posted October 9, 2006 Share Posted October 9, 2006 HiI was wondering, is there an easier way...or should I say quicker rather than easier, way, to have a bunch of checkboxes to be checked when I take a 1 or 0 from the database... at the moment im painstakingly doing the following:[code]...bla bla got all the vars and data from the database.....<? if ($random_variable1 == 1) { ?> <input type="checkbox" name="boring" checked> <? } else { ?> <input type="checkbox" name="boring"> <? } if ($random_variable2 == 1) { ?> <input type="checkbox" name="gettingstupidnow" checked> <? } else { ?> <input type="checkbox" name="gettinstupidnow"> <? }?>[/code]Generating this in PHP in a loop sounds like a brilliant idea doesnt it...well its not in this case its not fiesable for this project...and I have about 50 of these.....PLEASE SOMEONE EASE MY SUFFERING! Link to comment https://forums.phpfreaks.com/topic/23411-not-really-a-big-issuebut-a-pain-in-the-back-side/ Share on other sites More sharing options...
xsist10 Posted October 9, 2006 Share Posted October 9, 2006 Firstly you can reduce this:[code]if ($random_variable1 == 1) { ?> <input type="checkbox" name="boring" checked> <? } else { ?> <input type="checkbox" name="boring"> <? }[/code]to[code]<input type="checkbox" name="boring" <?php ($random_variable ? echo "checked" : ""); ?>>[/code] Link to comment https://forums.phpfreaks.com/topic/23411-not-really-a-big-issuebut-a-pain-in-the-back-side/#findComment-106171 Share on other sites More sharing options...
glenelkins Posted October 9, 2006 Author Share Posted October 9, 2006 Ok that code makes absolutely no sense what so ever, can you please explain what you have done there? Link to comment https://forums.phpfreaks.com/topic/23411-not-really-a-big-issuebut-a-pain-in-the-back-side/#findComment-106176 Share on other sites More sharing options...
chris9902 Posted October 9, 2006 Share Posted October 9, 2006 what he posted was shorthand for the IF ELSE statment.<?php ($var ? TRUE : FALSE); ?>what this is saying iswhat is $var TRUE or FALSE. if TRUE do the first thing else or do the second thing.so <?php ($random_variable1 ? echo "checked" : ""); ?> means,if $random_variable1 is TRUE echo "checked" else do nothing.or another way to think of it is how it looks.$random_variable1 ?so you're asking what is $random_variable1, TRUE or FALSE.if it's TRUE do the first thing or (or is represented with a [b]:[/b] ) do the other thing. Link to comment https://forums.phpfreaks.com/topic/23411-not-really-a-big-issuebut-a-pain-in-the-back-side/#findComment-106184 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.