Solved

php code

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
phpmyadmin 3 33
Animated .jpg? 13 60
Website Interactive tool that will do calculations 3 29
Scope of $_SESSION 17 30
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

920 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

12 Experts available now in Live!

Get 1:1 Help Now