0

I have a CodeIgniter query that brings back an array of objects. My query is querying multiple tables that all have the same field name. So I am using the select to give them aliases. Like this:

SELECT t1.platform_brief b1, t2.platform_brief b2

where t1 and t2 are my two tables. My array when print_r'd returns objects like this:

Array
(
    [0] => stdClass Object
        (
            [b1] => Lorem ipsum
            [b2] => 
        )

    [1] => stdClass Object
        (
            [b1] => 
            [b2] => Sic dolor sit
        )

)

In my foreach, when I echo them, how do I do that? I tried something like this but it didn't work:

<?php foreach ($lasers as $laser) {
echo $laser->?
?>

What do I put in place of the question mark?

EDIT:

Here is my CI query:

$this->db->select('ils1.platform_brief b1, ils2.platform_brief b2');
$this->db->where('ils1.language', $lang);
$this->db->or_where('ils2.language', $lang);
$this->db->join('all_platform_ils975 ils2', 'ils2.laser_id = c.laser_id', 'left');
$this->db->join('all_platform_ils1275 ils1', 'ils1.laser_id = c.laser_id', 'left');
$this->db->join('all_lasers l', 'l.laser_id = c.laser_id', 'inner');
return $this->db->get($lang . '_configure_lasers c')->result();
5
  • 2
    This question is worded poorly. Why is b1 blank in the first record, and b2 blank in the second? How do these two records relate, why the two tables, why the empty fields? Commented Sep 21, 2011 at 23:40
  • This is the way my join combined them. How do I make my join put them in the same field? When I do it that way, I only get the last result? Commented Sep 21, 2011 at 23:43
  • The two tables are tables for two lasers with lots of information in each one. I need to pull data from all of the laser tables and return it as one result. Commented Sep 21, 2011 at 23:44
  • Maybe my join statement is the issue. Commented Sep 21, 2011 at 23:44
  • $lasers is the value returned from my query. Commented Sep 21, 2011 at 23:47

2 Answers 2

2

Judging by your follow-up comment, it sounds like you want to output all the b1 fields for each result, then all the b2 fields. One way:

foreach( array('b1', 'b2') as $fields ) {
    foreach( $lasers as $laser ) {
        echo $laser->$field, '<br>';
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I can't use an array because I have a lot of data to return other than this. But thanks anyways.
0

You could iterate over each object:

foreach ($lasers as $laser) {
    foreach($laser as $field) {
        if(!empty($field)) echo $field;
    }
}

However, instead you should change the database design / query design to better meet your needs.

4 Comments

I think I need to change my query, but I'm not sure what to differently. It's posted above. Do you have any suggestions. It's too late to redesign the tables.
If you can't change the tables, create views that "replace" your tables: dev.mysql.com/doc/refman/5.0/en/views.html
Can I query a view? It sounds promising.
Yes, it's sort of a virtual table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.