Jump to content

Archived

This topic is now archived and is closed to further replies.

glenelkins

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

Recommended Posts

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:

[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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Ok that code makes absolutely no sense what so ever, can you please explain what you have done there?

Share this post


Link to post
Share on other sites
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 [b]:[/b] ) do the other thing.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.