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
https://forums.phpfreaks.com/topic/255164-is-there-a-faster-way/
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

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"' : '') . ' />';

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

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.

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?

 

 

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!

Archived

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

×
×
  • 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.