Solved

php code

Posted on 2012-04-11
4
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

705 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