Solved

php code

Posted on 2012-04-11
4
289 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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from 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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 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