• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

php code

This code below has php code which has a foreach loop. This loop iterates through the dataset sql query but am actually wondering about this statement below, is $row an actual row and is reading the first item returned from column 'pk_wish_id'


"$wish['id'] = $row->pk_wish_id;"


 public function getWishes($page = 1,$limit = 10) {
        $offset = ($page-1)*$limit;
        $query = $this->db->select('pk_wish_id, child_name, child_age, child_state, child_disease,
            child_poverty, book_name, book_image, book_price')
                ->where('is_active',1)
                ->where('is_granted',0)
                ->get('wishes',$limit,$offset);
        $wishes = array();
        foreach($query->result() as $row){
            $wish = array();
            $wish['id'] = $row->pk_wish_id;
            $wish['childName'] = $row->child_name;
            $wish['age'] = $row->child_age;
            $wish['state'] = $row->child_state;
            $wish['disease'] = $row->child_disease;
            $wish['poverty'] = $row->child_poverty;
            $wish['bookName'] = $row->book_name;
            $wish['image'] = $row->book_image;
            $wish['price'] = $row->book_price;
            $inCart = $this->cart->hasWish($wish['id']);

            if(!$inCart)
                $wishes[] = $wish;
        }
        shuffle($wishes);
        return $wishes;
    }

Open in new window

0
Moizsaif123
Asked:
Moizsaif123
  • 2
3 Solutions
 
gnemiCommented:
$query->result() is a row returned from the query.

So $row->pk_wish_id is just value from the pk_wish_id field in the row returned from that query.
0
 
Olaf DoschkeSoftware DeveloperCommented:
foreach($query->result() as $row)

This means each element of result() is put into a variable $row and then processed. What $query->result() actually is, can only be guessed, not knowing what $this->db->select() returns. But most probably it's an array, because foreach() does process arrays.

So $query->result() is the result array of the query containing all rows. And $row is set to one of the rows, in a loop every row is therefore processed.

$wish['id'] = $row->pk_wish_id;
And all the other lines set an array element of $wish[], to a field of the row.

So to answer your direct question: Yes, $row is a record, to be more precise, $row is a record object here, having a property pk_wish_id, which was read from the database table, and all the other fields are also properties of $row.

And it is not "reading the first item returned from column 'pk_wish_id'", the foreach loop makes it any such id, all retreived records are processed to a $wish array each.

Then the final lines determine if that $wish is added to another array of $wishes, depending on the id being found in $this->cart, which is checked via $this->cart->hasWish(). Finally all wishes also found in the cart are shuffled and returned.

Do you have any problem with this code, is the $wishes array finally returned not what you expect?

Bye, Olaf.
0
 
Ray PaseurCommented:
This is just a guess, but it looks like you've got a script that needs to select information from a data base and rearrange the data in random order.  I think you might be able to save a number of steps if you ORDER BY RAND.
0
 
Olaf DoschkeSoftware DeveloperCommented:
You could make this code much leaner, if $this->db->select would create $query->result() as an array of record arrays instead of an array of record objects, then you would just do the foreach($query->result() as $wish) and not need any line to convert from $row property to $wish array element.

Bye, Olaf.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now