Welcome Guest, Not a member yet? Register   Sign In
PHP stdClass object: getting data throught foreah loop
#1

[eluser]developer10[/eluser]
Basically, im pretty frustrated by the fact every time I have to hard-code my pagination whenever (and that is pretty much always) i want to use some filters (category, starting letter, etc) in my results before the final results are presented on the page.

The main problem here is that (IMHO) there is no easy (yet nice way) to get both number of records (that $config['total_rows'] will use) of that resultset (when filters apply) and have limited number of records per page, ready for pagination to render.

So, for a long time I'm struggling to find an acceptable way for me to use every time.
This is as far as I got.

My model:

Code:
function loadArtists($type, $letter, $uriOffset, $limit)
    {
        $query = "SELECT * FROM _tbl_artists WHERE ";
            if($type)
                $query .= "_artist_category = $type AND ";
            
            if($letter)
                $query .= "_ARTIST_NAME LIKE '$letter%' AND ";
        
        $query .= " id > 0 ORDER BY _ARTIST_NAME ASC ";
        
        if ($uriOffset)
            $limited = $query . " LIMIT $uriOffset, $limit";
        else
            $limited = $query;
        
        $noLimit = $this->db->query($query);
        $withLimit = $this->db->query($limited);
        
        $numRows = $noLimit->num_rows();
        $resultStack = array();
        $resultStack = array($numRows);
        
        
        if($withLimit->num_rows() > 0)
        {
            $result = $withLimit->result();
        }
        
        array_push($resultStack, $result);
        
        return $resultStack; //$resultStack;
    }

Now, when i send this resultset to my view (before that, in my controller i pull out the number of records needed by $config['total_rows ']), I can't figure out how to pull a piece of data i need to show on that page.

Here's the output of the above resultset:

Code:
Array
(
    [0] => 148
    [1] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 347
                    [_ARTIST_NAME] => EDIN CARIC
                    [_ARTIST_SEO_NAME] => edin-caric
                    [_artist_category] => 0
                    [_ID_ORIGINAL] => 3325
                )

            [1] => stdClass Object
                (
                    [id] => 348
                    [_ARTIST_NAME] => EDIN DENI OMEROVIC
                    [_ARTIST_SEO_NAME] => edin-deni-omerovic
                    [_artist_category] => 0
                    [_ID_ORIGINAL] => 3326
                )

            [2] => stdClass Object
                (
                    [id] => 349
                    [_ARTIST_NAME] => EDIN PASIC
                    [_ARTIST_SEO_NAME] => edin-pasic
                    [_artist_category] => 0
                    [_ID_ORIGINAL] => 3328
                )

            [3] => stdClass Object
                (
                    [id] => 3069
                    [_ARTIST_NAME] => EDIN UZUN EDY
                    [_ARTIST_SEO_NAME] => edin-uzun-edy
                    [_artist_category] => 0
                    [_ID_ORIGINAL] => 31693
                )

            [4] => stdClass Object
                (
                    [id] => 350
                    [_ARTIST_NAME] => EDIS I HARIS
                    [_ARTIST_SEO_NAME] => edis-i-haris
                    [_artist_category] => 0
                    [_ID_ORIGINAL] => 3330
                )

            [5] => stdClass Object
                (
                    [id] => 351
                    [_ARTIST_NAME] => EDISA
                    [_ARTIST_SEO_NAME] => edisa
                    [_artist_category] => 0
                    [_ID_ORIGINAL] => 3340
                )

        )
)

I'm totally unfamiliar with PHP stdClass and I have tried some options to convert it to array but to no avail.

OK, so what i need from the object is _ARTIST_NAME or other data from that level.

If anyone has any suggestions, please post!
#2

[eluser]adityamenon[/eluser]
Well, I've never encountered something like this, but what happens when you do this?

Code:
//$topLevelArray is the main Array() you posted as output
$object = $topLevelArray[1][0]; //you get those keys by using a foreach loop
$artistName = $object->_ARTIST_NAME;

I know you might have tried that, but I'm just curious about what error you got...
#3

[eluser]developer10[/eluser]
[quote author="adityamenon" date="1309132737"]Well, I've never encountered something like this, but what happens when you do this?

Code:
//$topLevelArray is the main Array() you posted as output
$object = $topLevelArray[1][0]; //you get those keys by using a foreach loop
$artistName = $object->_ARTIST_NAME;

I know you might have tried that, but I'm just curious about what error you got...[/quote]


Well, here's the error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: artists/artistsView.php

Line Number: 26

EDIT: I missed something when applying your code, but when done right, this seems to work!

Now it's really easy to use the number of records that is carried along with the resultset
for displaying number of records viewed.

Thanks a lot for your help!
#4

[eluser]adityamenon[/eluser]
Cheers! By the way, I found another way on StackOverflow today :

Code:
//put your object in place of $object and you might it in array form in $array...
$array = json_decode(json_encode($object), true);

...you might want to check that out too Smile
#5

[eluser]developer10[/eluser]
[quote author="adityamenon" date="1309386230"]Cheers! By the way, I found another way on StackOverflow today :

Code:
//put your object in place of $object and you might it in array form in $array...
$array = json_decode(json_encode($object), true);

...you might want to check that out too Smile[/quote]

Actually, I made this complicated all by my own fault.
This simple code does what i need:

Code:
<?php
    $rSetArtists = $rSetArtists[1];

    foreach ($rSetArtists as $key => $value):
        echo $value->_ARTIST_NAME . '<br />';
    endforeach;

    echo $renderPaginationLinks;
    ?&gt;

At the start, reducing of that multidimensional array is done so the remainder is treated as any other arrayed resultset.

Thanks anyway, that JSON line of code might be useful for me in the future!




Theme © iAndrew 2016 - Forum software by © MyBB