Jump to content

is there a faster way?


Monkuar

Recommended Posts

my code:

echo $Profile['display'];

 

 

ECHO's out:

 

0|1|0|1

 

 

Now I want to explode these, which is easy

 

$display = explode("|", $Profile['display']);

 

But Now I want to use a php function to check if any of the array's = "1" and have the value set to $value= "checked";

 

 

I could do this by doing

 

 if ($display['0'] == "1"){ $value = "checked"; } 

 

and then 1 2 3 and so on..

 

 

 

but I want a faster way, maybe a for loop, or a simpliar easier way to just check if each array = 1, make $value = checked

 

Possible right? :D

 

Link to comment
Share on other sites

How about

$value = $display[0] == 1 ? 'checked' : '';

 

o.o

 

simply terny operator would work

 

but then i would have to use

 

$value = $display[0] == 1 ? 'checked' : '';

$value = $display[1] == 1 ? 'checked' : '';

$value = $display[2] == 1 ? 'checked' : '';

 

but hell w/e i guess it works

 

thx

Link to comment
Share on other sites

Remember that if you do that, you are changing the assignment of $value each time... so it's only going to be assigned to the last ternary statement.

 

Is this for checkboxes or something? If so you could do something like:

echo '<input type="checkbox" ' . ($display[0] == 1 ? 'checked="checked"' : '') . ' />';

Link to comment
Share on other sites

Remember that if you do that, you are changing the assignment of $value each time... so it's only going to be assigned to the last ternary statement.

 

Is this for checkboxes or something? If so you could do something like:

echo '<input type="checkbox" ' . ($display[0] == 1 ? 'checked="checked"' : '') . ' />';

 

yea but that would still only do $display['0'] :(

im looking for a forloop method to iniative all 4 arrays and go through each one

 

it's fine tho the ternary operator works fine, i just changed $value to $value1 $value2 $value3 then used them on my html return <<<EOF functon

Link to comment
Share on other sites

Like this?

 

<?PHP

  //### Dummy Data
  $Profile['display'] = '1|0|1|0';

  $display = explode("|", $Profile['display']);

  foreach($display AS $key => $value) {
    $show = $value == 1 ? 'checked' : '' ;

    echo '<input type="checkbox" name="check" '.$show.'> '.$key.' <br>';
  }

?>

 

Regards, PaulRyan.

Link to comment
Share on other sites

Remember that if you do that, you are changing the assignment of $value each time... so it's only going to be assigned to the last ternary statement.

 

Is this for checkboxes or something? If so you could do something like:

echo '<input type="checkbox" ' . ($display[0] == 1 ? 'checked="checked"' : '') . ' />';

 

yea but that would still only do $display['0'] :(

im looking for a forloop method to iniative all 4 arrays and go through each one

 

it's fine tho the ternary operator works fine, i just changed $value to $value1 $value2 $value3 then used them on my html return <<<EOF functon

 

But you are writing all the checkboxes anyway, right?

 

 

Link to comment
Share on other sites

Remember that if you do that, you are changing the assignment of $value each time... so it's only going to be assigned to the last ternary statement.

 

Is this for checkboxes or something? If so you could do something like:

echo '<input type="checkbox" ' . ($display[0] == 1 ? 'checked="checked"' : '') . ' />';

 

yea but that would still only do $display['0'] :(

im looking for a forloop method to iniative all 4 arrays and go through each one

 

it's fine tho the ternary operator works fine, i just changed $value to $value1 $value2 $value3 then used them on my html return <<<EOF functon

 

But you are writing all the checkboxes anyway, right?

 

yeah each 1 so i guess the ternary operator is awesome

 

makes the code small and compact, i love it thanks for sharing!

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.