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! Quote Link to comment 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] Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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.