1

I would like to get, for each type, the number of occurrences. You can see in the image that they are positioned at difference places based on a COUNT query I'm doing to get the data.

enter image description here

I am using CodeIgniter, and this is my current code, but it doesn't work.

foreach ($query->result() as $row) {
    $num = $row->type;
    $returndata[] = array(
        'type' => $row->type,
        'num' => $row->TYPE.$num
    );
} 

I was thinking that I could get the data with a simple foreach loop but apparently I can't. Is there some secret voodoo magic, apart from using a switch statement, to get the data?

6
  • From a distance this looks like it should work, what do you get in $returndata after your loop is over? Commented Oct 11, 2013 at 5:35
  • you can simply cast the object to an array- like $data = (array)$query->result(), but i don't think that'll give you what you're looking for with the extra array element 'num' Commented Oct 11, 2013 at 5:41
  • @Hanky웃Panky I was getting the same as type. BTW your edit is inaccurate (switch -> foreach). I meant that first thing that came to my mind was to create a switch and check the type of $row->num and based on that get a different TYPE1.2.3.... Commented Oct 11, 2013 at 5:41
  • apart from using a switch loop: switch in the first place is not a loop at all, it is a conditional structure. Secondly the code that you presented as your attempt has a foreach loop and no switch statement anywhere. Besides, if you consider this edit changes the meaning of what you stated, please revert it back with an apology from my side Commented Oct 11, 2013 at 5:45
  • @Hanky웃Panky The switch idea was in reference to what he could do. I've made a small edit to the question's formatting in order to make that more clear. Commented Oct 11, 2013 at 5:49

3 Answers 3

2

'num' => $row->TYPE.$num isn't quite doing what you think it is, as that is not the correct way to access a field with a dynamic name. In reality, you're setting num to the value of $num appended to $row->TYPE.

To handle dynamic object property names, you need to enclose the name in brackets:

'num' => $row->{'TYPE'.$num}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Vulcan! That's what I was looking for. Smart.
0
foreach($query->result_array() as $row){

$num = $row["type"];
                    $returndata[] = array(
                        'type' => $row["type"],
                        'num' => $row["TYPE"].$num
                    );

                } 

Comments

0
$returndata = [];
foreach($query->result() as $row){
    for ($i=1; $i <= 11; $i++)
    {
        if($value = $row->{'TYPE'.$i})
        {
            $returndata[$row->type] = $value;
        }
    }
}

3 Comments

Just as a side note. This will fail when there is type>11, no?
Yes. If you don't know how many columns there are in advance then the script will need to be changed to accustom for that. It is also assuming that only one column will have values for each row. If that is not the case, then you need to add them together (if that is what you want).
If you mean the amount of rows, then no it won't fail. For each row it goes through 11 columns in my example.

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.