0

I pass $data["results"] array from my controller to my view and I want to echo NAMEs of those array elements which equals 1.

For example, if {$first ==1, $second == 0, $third == 0} I want to display "first".

Could you please check my code below and help me to find my mistake.

    foreach($results as $row){                  
                                $first= $row->first;
                                $second= $row->second;
                                $third= $row->third;

            if ($first == 1) {$digits['first'] = $first;}  
            if ($second == 1) {$digits['second'] = $second;} 
            if ($third == 1) {$digits['third'] = $third;} 

print_r($digits);  //  Displays 'Array ( [first] => 1 )' instead of 'first'
    }

Update:

I generate html tables through the loop and display them with TCPDF.

Updated code below normally displays 'first' if {$first ==1, $second == 0, $third == 0} for the first table.

For the second table if {$first == 0, $second == 0, $third == 1} it should display 'third' but it displays 'first, third' because it adds the new value to previous one instead of replacing it.

$digits = array();

foreach($results as $row){
    $first= $row->first;
    $second= $row->second;
    $third= $row->third;

    if ($first == 1) { $digits[] = 'first'; }
    if ($second == 1) { $digits[] = 'second'; }
    if ($third == 1) { $digits[] =  'third'; }

$abc = implode(', ', $digits);

$tbl.=<<<EOD
<table>
 <tr>
  <td>
    $abc
  </td>    
 </tr>
</table>
<br><br>
EOD;

}
3
  • you are at right way Commented Nov 5, 2014 at 16:16
  • Try and wrap the 1 in quotes. ($first == '1') Commented Nov 5, 2014 at 16:29
  • quotes did not help :( Commented Nov 5, 2014 at 16:36

4 Answers 4

1

The following code will loop through the array and add 'first', 'second' or 'third' to the $digits array.

Is this what you are trying to achieve?

foreach($results as $row){
    $digits = array();
    $first= $row->first;
    $second= $row->second;
    $third= $row->third;

    if ($first == 1) { $digits[] = 'first'; }
    if ($second == 1) { $digits[] = 'second'; }
    if ($third == 1) { $digits[] =  'third'; }
    print_r($digits);
    // echo implode(', ', $digits);
}
Sign up to request clarification or add additional context in comments.

7 Comments

almost, thank you. the only problem is that 'print_r($digits);' displays 'Array ( [0] => first )' but I want to display 'first'
yes, but there is one small problem. I have to use implode() function inside the loop and it adds new elements to previous ones each time I run the loop.
I don't understand what you are trying to do. How many elements are in $results? What do you want the output to be? Can you give a sample input and sample output?
Put $digits = array(); inside the foreach. I have updated my answer
thanks for your help. Actually I need an array. Could you please have a look on updated part of my question. Perhaps this time I could formulate my question correctly:)
|
0

Try to see what you have in $digits..

echo var_dump($digits);

Comments

0

why use an array $digits?

is not enought?

$digits = '';

foreach($results as $row){
    $first= $row->first;
    $second= $row->second;
    $third= $row->third;

    if ($first == 1) { $digits = 'first'; break; }
    if ($second == 1) { $digits = 'second'; break; }
    if ($third == 1) { $digits =  'third'; break; }
}

echo $digits;

Comments

0

Try this code.

$digits = array();

foreach ($results as $row) {

 if ($row->first == 1) {
    $digits['first'] = 'first';
 }
 if ($row->second == 1) {
    $digits['second'] = 'second';
 }
 if ($row->third == 1) {
    $digits['third'] = 'third';
 }
}

print $digits['first'];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.