Jump to content

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


glenelkins

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!

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.