Solved

php code

Posted on 2012-04-11
4
287 Views
Last Modified: 2012-04-11
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
Comment
Question by:Moizsaif123
  • 2
4 Comments
 
LVL 3

Assisted Solution

by:gnemi
gnemi earned 83 total points
ID: 37832029
$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
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 84 total points
ID: 37832046
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 83 total points
ID: 37833948
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
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 37836163
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now