PHP build array after request for template

Request data back, for example:

| name_1 | name_2 | value_1 | value_2 |  obj  | i_id | // <- cols name
--------------------------------------------------     
|   BB   |   AA   |   3     |    6    | obj_1 |   1  |
|   BB   |   AA   |   2     |    1    | obj_2 |   1  |
|   BB   |   AA   |   2     |    4    | obj_3 |   1  |
|   CC   |   DD   |   3     |    3    | obj_1 |   2  |
|   CC   |   DD   |   2     |    3    | obj_2 |   2  |
and more...

      

Here I need to build an array for the template, for example:

$arr = array();
while($r = $stmt->fetch(PDO::FETCH_ASSOC) {
    $arr = ?   <---------------------  // HELP WITH BUILD ARRAY HERE!
}

      

Here is the template I need, for example:

<?php foreach(): ?> ------ // for each i_id 
    <?php if(obj == 'obj_1'): ?> // WE NEED obj_1 VALUES FOR CHOICE BOLD COLOR

        <?php if(value_1 > value_2): ?> 
           <?php $name_1_BOLD = "style='color: black;'" ?>
        <?php else: ?>
           <?php $name_2_BOLD = "style='color: black;'" ?>
        <?php endif; ?>
    <?php endif; ?>

<div <?php echo $name_1_BOLD; ?>>   // ------  here can be bold color
    <?php echo $name_1; ?> --------- // here pass NAME 1
</div>

<div>
    <?php foreach(): ?>  //  ------  all values for each i_id
        <div>
            <?php echo $value_1.' - '.$value_2; ?> // here pass ALL VALUES FOR unique i_id
        </div>
    <?php endforeach; ?>
</div>
<div <?php echo $name_2_BOLD; ?>>   // ------  here can be bold color
    <?php echo $name_2; ?> --------- // here pass NAME 2
</div>

      

Guys please help me with this or give a good guide. update:

enter image description here

from image 2-3 (3-6 from table) BB is larger than AA and has bold color and others

+3


source to share


2 answers


Well, you want an array of arrays.

The first array is associative, where the key will be an identifier in your DB ( i_id

). And the value will be the rest of the data for that line ( name_1

, name_2

etc.)

Then you will iterate while trying to get each array and print the contents.

With a little pseudocode:

$arr = array();
while($r = $stmt->fetch(PDO::FETCH_ASSOC) {
    $key = $r->i_id;   <---the key is i_id, we will put the contents now..
    $arr[$key]['name_1'] = $r->name_1;
    $arr[$key]['name_2'] = $r->name_2;
    //etc
}

      



Your template (with what I think you want to do):

<?php 
    foreach($arr as $r)// for each i_id 
    {
       if($r->obj == 'obj_1')// WE NEED obj_1 VALUES FOR CHOICE BOLD COLOR
           if($r->value_1 > $r->value_2)
             $$name_1_BOLD = "style='color: black;'";
        else
              $name_2_BOLD = "style='color: black;'";

      echo $name_1_BOLD;   // ------  here can be bold color
      echo $name_1; // here pass NAME 1

      echo '</div>
        <div>
        <div>';
      echo $r->value_1.' - '.$r->value_2; // here pass ALL VALUES FOR unique i_id
      echo '</div>
         </div>
        <div ';
     echo $name_2_BOLD; // ------  here can be bold color
     echo $r->name_2;  // here pass NAME 2
     echo '</div>';
    }
?>

      

Please note that you are abusing opening / closing tags <php

Other: I will most likely print the tags div

you want, you want to control that.

0


source


Not a bad practice to echo HTML through PHP. You should eliminate the need to integrate HTML into PHP. It is also a way to easily manipulate the style on your page.

<?php foreach($arr as $r): ?>
    <?php if($r->obj == 'obj_1'): ?>

        <div style="<?php if($r->value_1 > $r->value_2): ?>color: black<?php else: ?>color: red<?php endif; ?>">
            <?php echo $r->name_1; ?>
        </div>

    <?php endif; ?>
<?php endforeach; ?>

<div>
    <?php foreach($arr as $r): ?>
        <div>
            <?php echo $r->value_1; ?> - <?php echo $->value_2; ?>
        </div>
    <?php endforeach; ?>
</div>

      



I'm not sure what you are trying to achieve with name_2 ...

0


source







All Articles