How can I display data in two columns?

I need to show some data in two columns, I do a search and I find some things, and I try. I think what I am doing This is working, but I was rong it works partially.

This is my database

  **Type**
--------
    id_type | type  
------------|------------
     1      |  first Title
     2      |  second Title
     3      |  Third Title

**Content**
---------
id_content | content   | id_type
-----------|-----------|---------
1          | Content1  |   1
2          | Content2  |   2
3          | Content3  |   2
4          | Content4  |   3
5          | Content5  |   3
6          | Content6  |   3

      

my model

$query1 = "select * from type";
$query2 = "select * from content where id_type = $id_type";

      

Note: this request is part of the corresponding function

my controller

$list_content = array(array());
$contador = 0;
$list_type = array(array());
$cont_type = 0;
            $query1 = $obj->Search_Type();
  for ($j = 0; $j < $query1->num_rows; $j++) {

    $id_type = $list_type[$cont_type]['id_type'] = $query1->row($j)->id_type;

    $query2 = $obj->Search_Content($id_type);

      for ($i = 0; $i < $query2->num_rows; $i++) {

       id_content = $list_content[$contador]['id_content'] = $query2->row($i)->id_content;
         $list_content[$contador]['id_type'] = $query2->row($i)->id_type;
         $contador++;
         } //end for i

      $list_type[$cont_type]['id_receptor'] = $id_content; 
      $cont_type++;

            }//del for j

      

in my opinion

<table border='1'>
<?php $columna = 1;
 for ($j = 0; $j < $cont_type; $j++) {
    if ($list_type[$j]['id_type'] != '') {
 ?>
<tr>
    <td colspan = '2'> 
    <?php echo $list_tipo_receptor[$j]['tipo_receptor_TVD']; ?>
    </td>
</tr>
<?php
    }// if type no empty
    for ($i = 0; $i < $contador; $i++) {
       if ($list_type[$j]['id_type'] == $list_content[$i]['id_type']) {
          if ($columna == 1) {
          ?>
          <tr>
            <?php } ?>
             td> <?php echo $list_content[$i]['id_content']; ?></td>
              <?php if ($columna != 1) { ?>
          </tr>
             <?php
             $columna = 1;
              }// if columna != 1
              else {
                    $columna++;
                   }// else
                  }//if
             } //for i
    }// for j
    ?>
   </table>

      

show this result

-------------------------
|   First Title         |
-------------------------
|Content1  |            |
-------------------------
|    Second Title       |
-------------------------
|Content2  |            |
-------------------------
|Content3  |            |
-------------------------
|    Third Title        |
-------------------------
|Content4  |            |
-------------------------
|Content5  | Content6   |

      

and i want to show me something like this

    -------------------------
    |   First Title         |
    -------------------------
    |Content1  |            |
    -------------------------
    |    Second Title       |
    -------------------------
    |Content2  | Content3   |
    -------------------------
    |    Third Title        |
    -------------------------
    |Content4  |   Content5 |
    -------------------------
    | Content6 |            |

      

Thank you for your help.

More information about the answer Image of my view

view with changes view after changes

+3


source to share


2 answers


Set your task with join

.

For your understanding, I first create a simple MySQL query.

select * from Content c left join Type t on c.id_type = t.id_type

you can easily get the desired result in one query.



Now let's do the same in CI. inside the controller

$result = $this->db->select('c.content, c.id_type, t.type')
->from('Content c')
->join('Type t','c.id_type = t.id_type','left')
->order_by('id_type')
->get()
->result();

      

I am assuming you are passing this one $result

for viewing; view

<table> 
<?php 
$previous_id_type = 0; 
$col_count =0; 

foreach($result as $row){ 

if($row->id_type != $previous_id_type){ 
$previous_id_type = $row->id_type; 
$col_count = 0; 
?> 
<tr><td colspan="2"><?php echo $row->type; ?> </td></tr><tr> 

<?php 

}?> 

<td> <?php echo $row->content; ?> </td> 
<?php 
if( $col_count == 1){ 
echo '</tr>'; 
} 
$col_count++; 
} 
?> 

} ?> 
</tr> 
</table>

      

+2


source


hei you can use this connection request

    $this->db->select('*');    
    $this->db->from('content');
    $this->db->join('type', 'content.id_type = type.id');
    $query = $this->db->get();

      

if you want to use too loop you can return with

    return $query->result();

      



if not

    return $query->row();

      

hope to solve your problem

+2


source







All Articles