How to display records of select query with left join in table using foreach?

jned29

I have two tables (table A and B). I need to show them in one table, but have an issue with how I to display the records on my table. Please see my sample:

Table A:

id   name
1    John
2    Mark
3    Nick`

Table b:

id     job 
1    Encoder
2
3    Programmer`

I made a query to connect the two tables

select a.id, a.name, b.job from table_a a
left join table_b b on b.id=a.id
order by a.id;

Using codeigniter,

This is the controller:

$this->db->select('a.id, a.name, b.job);
$this->db->from('table a');
$this->db->join('table b', 'b.id=a.id', 'left');
$this->db->order_by('a.id');
$query = $this->db->get();
$data["view_records"]=$query;
$this->load->view("table_name", $query);

This is the view

<table id="tablestyle" class="table table-bordered table-hover table-condensed">
    <col width="50">
    <col width="150">
    <col width="150">
    <col width="150">
    <thead>
        <tr>
            <th><strong>ID</strong></th>
            <th><strong>NAME</strong></th>
            <th><strong>JOB</strong></th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($view_records->result() as $row) {  ?>
                <tr>
                    <td><?php echo $row->a.id; ?></td>
                    <td><?php echo $row->a.name; ?></td>
                    <td><?php echo $row->b.job; ?></td>
                    <td><input type="submit" value="Modify" id="btnModify" class="btn btn-block btn-success btn-xs" onclick="btnModify('<?php echo $row->a.id; ?>');"/></td>
                </tr>
        <?php } ?>
    </tbody>
</table>
Abdulla Nilam

In Model

public function get_user()
{
    $query = $this->db->query("select a.id, a.name, b.job from table_a a
        left join table_b b on b.id=a.id
        order by a.id");
    $result = $query->result_array();
    return $result;
}

In Controller

$data['get_user'] = $this->model_name->get_user();

$this->load->view("view_name", $data);

In View

<table id="tablestyle" class="table table-bordered table-hover table-condensed">
    <col width="50">
    <col width="150">
    <col width="150">
    <col width="150">
    <thead>
        <tr>
            <th><strong>ID</strong></th>
            <th><strong>NAME</strong></th>
            <th><strong>JOB</strong></th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($get_user as $rowItem) {  ?>
                <tr>
                    <td><?php echo $rowItem['a.id'] ?></td>
                    <td><?php echo $rowItem['a.name'] ?></td>
                    <td><?php echo $rowItem['b.job'] ?></td>
                    <td><input type="submit" value="Modify" id="btnModify" class="btn btn-block btn-success btn-xs" onclick="btnModify('<?php echo $rowItem['a.id'] ?>');"/></td>
                </tr>
        <?php } ?>
    </tbody>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to display records as empty if data not present in table using left join

From Dev

LEFT JOIN not keeping only records that occur in a SELECT query

From Dev

left join query does not return left table records if right table does not have records

From Dev

How to Optimize Left Join Query on Large Table

From Dev

Using GROUP BY and HAVING on a LEFT JOIN table where there is no records stored

From Dev

JPA + How to select records using third table as join in one-to-many relation?

From Dev

How can I select records that match multiple values in a linking table using a MySQL join statement?

From Dev

mysql select with left join query

From Dev

mysql select with left join query

From Dev

Using sub-query to SELECT MAX value along with LEFT JOIN

From Dev

How to JOIN a table to a ranked SELECT query in MySQL?

From Dev

How to use join table and count with select query

From Dev

fetch records from four different table using join query

From Dev

fetch records from four different table using join query

From Dev

Correct result for MySQL query using LEFT JOIN and History table

From Dev

How to display LEFT JOIN results with a condition coming from a third table?

From Dev

How to display LEFT JOIN results with a condition coming from a third table?

From Dev

sql query left join records missing

From Dev

Ignore multiple records in LEFT JOIN query by case

From Dev

PHP How to display mysql select query to table

From Dev

Not getting all records from Table A in left join

From Dev

PySpark: How to select * from the left table during rdd join

From Dev

How to query multiple Left Join table when there exist null

From Dev

using postgresql, how can I join tables , select all from left table and sum and count from right table?

From Dev

Left join or Select in select (SQL - Speed of query)

From Dev

How to optimize a MySQL SELECT query with a LEFT JOIN in main query and an INNER JOIN in subquery?

From Dev

How to optimize a MySQL SELECT query with a LEFT JOIN in main query and an INNER JOIN in subquery?

From Dev

LINQ Query To Join Two Tables and Select Most Recent Records from Table B corresponding to Table A

From Dev

How to optimise this query using table join?

Related Related

  1. 1

    how to display records as empty if data not present in table using left join

  2. 2

    LEFT JOIN not keeping only records that occur in a SELECT query

  3. 3

    left join query does not return left table records if right table does not have records

  4. 4

    How to Optimize Left Join Query on Large Table

  5. 5

    Using GROUP BY and HAVING on a LEFT JOIN table where there is no records stored

  6. 6

    JPA + How to select records using third table as join in one-to-many relation?

  7. 7

    How can I select records that match multiple values in a linking table using a MySQL join statement?

  8. 8

    mysql select with left join query

  9. 9

    mysql select with left join query

  10. 10

    Using sub-query to SELECT MAX value along with LEFT JOIN

  11. 11

    How to JOIN a table to a ranked SELECT query in MySQL?

  12. 12

    How to use join table and count with select query

  13. 13

    fetch records from four different table using join query

  14. 14

    fetch records from four different table using join query

  15. 15

    Correct result for MySQL query using LEFT JOIN and History table

  16. 16

    How to display LEFT JOIN results with a condition coming from a third table?

  17. 17

    How to display LEFT JOIN results with a condition coming from a third table?

  18. 18

    sql query left join records missing

  19. 19

    Ignore multiple records in LEFT JOIN query by case

  20. 20

    PHP How to display mysql select query to table

  21. 21

    Not getting all records from Table A in left join

  22. 22

    PySpark: How to select * from the left table during rdd join

  23. 23

    How to query multiple Left Join table when there exist null

  24. 24

    using postgresql, how can I join tables , select all from left table and sum and count from right table?

  25. 25

    Left join or Select in select (SQL - Speed of query)

  26. 26

    How to optimize a MySQL SELECT query with a LEFT JOIN in main query and an INNER JOIN in subquery?

  27. 27

    How to optimize a MySQL SELECT query with a LEFT JOIN in main query and an INNER JOIN in subquery?

  28. 28

    LINQ Query To Join Two Tables and Select Most Recent Records from Table B corresponding to Table A

  29. 29

    How to optimise this query using table join?

HotTag

Archive