Jump to content


Photo

Not Really A Big Issue...But a pain in the back side!


  • Please log in to reply
3 replies to this topic

#1 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 09 October 2006 - 10:10 AM

Hi

I 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:

...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">
      <?
   }
?>

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!


http://thewebsolutionprovider.com - A Solution For Everything Web!

#2 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 09 October 2006 - 10:22 AM

Firstly you can reduce this:

if ($random_variable1 == 1) {
      ?>
      <input type="checkbox" name="boring" checked>
      <?
  } else {
      ?>
      <input type="checkbox" name="boring">
      <?
  }

to

<input type="checkbox" name="boring" <?php ($random_variable ? echo "checked" : ""); ?>>

SA PHP Archives - www.phparchives.za.org

#3 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 09 October 2006 - 10:30 AM

Ok that code makes absolutely no sense what so ever, can you please explain what you have done there?
http://thewebsolutionprovider.com - A Solution For Everything Web!

#4 chris9902

chris9902
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationUK

Posted 09 October 2006 - 10:55 AM

what he posted was shorthand for the IF ELSE statment.

<?php ($var ? TRUE : FALSE); ?>

what this is saying is

what 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 : ) do the other thing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users