Avatar of avir
avir

asked on 

codeigniter loop result inside loop

Hi
I'm getting very confused and would appreciate some help.
I am returning book data that has chapter and verse fields (the Bible) and I would like to loop through each chapter and return all the verses from each particular chapter sorted by chapter.  What I have so far returns only the first verse from each chapter. You can see the results at:
http://www.imj.org.il/isaiah/book/homepage
Thanks
isaiah-mvc.txt
PHPMySQL ServerWeb Frameworks

Avatar of undefined
Last Comment
avir
Avatar of avir
avir

ASKER

Here is how the db is set up.
db.gif
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

You're grouping on the Chapter, so you'll only get one record per chapter.

You'll need to run several loops on the database to get the records you need. First, select the Chapter records (grouped by Chapter), and then for each Chapter in the result set, query the database for the Verses. Add a new method to your model to get the verses based on Chapter:

public function get_verses($chapter)
{
    $this->db->select('*');
    $this->db->from('isaiah');
    $this->db->where('chapter', $chapter);
    $query = $this->db->get();

    if($query->num_rows() == 0) return false;
    return $query->result_array();
}

Open in new window


Then something along these lines:

$chapters = $this->book_model->get_chapters();
foreach ($chapters as $chapter) {
    $this->book_model->get_verses($chapter['chapter']);
}

Open in new window

Avatar of avir
avir

ASKER

Okay thanks for your help.  I'll try this tomorrow. How does the get_verses function get the value for $chapter? This something that I don't quite get.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

The function is defined with an argument called $chapter:

public function get_verses($chapter)

Open in new window


The function is then called with the chapter being passed in as an argument, which you get from inside your controller loop.

foreach ($chapters as $chapter) {
    $chapter->verses = $this->book_model->get_verses($chapter['chapter']);
}

 Also, one bit I'd missed was assigning the verses to the chapter so you can loop through them in the views.

Let me know if you get stuck - I've not given a copy / paste answer and you'll need to update your model, controller and view.
Avatar of avir
avir

ASKER

I almost got it. I am echoing the results in the controller, but I can't figure out how to move them over to the view.
This is my controller now.
    function homepage() 
	{
		$chapters = $this->book_model->get_chapters();
		foreach ($chapters as $chapter) {
			$verses= $this->book_model->get_verses($chapter['chapter']);
			echo $chapter['chapter']."<br>";
			foreach ($verses as $verse) {
			echo $verse['verseName']."<br>";
			echo $verse['text']."<br>";	
			echo "<br>";				
			}
		}			
		$this->load->view('book/homepage', $data);
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of avir
avir

ASKER

Great! Thanks for your help. Is there a difference between using Objects or Arrays other than what one might be used to?
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

For simple data collections, you can use arrays and objects in a similar way:

$data = new array();
$data['firstname'] = "chris";
echo $data['firstname'];

$data = new stdClass();
$data->firstname = "chris";
echo $data->firstname;

Open in new window


Codeigniter is heavily Object Oriented, so you may find yourself using objects more than you realise, and you'll soon see the benefit of using them.

For example, calling $query->result() in your model will return an array of Objects. These objects can be instantiated against a class, so as well as the column names being available, you can also add methods to your class and call them directly:

// Query the database and instantiate a User for each record
foreach ($query->result('User') as $row)
{
   // output a column name
   echo $row->firstname;
   // call a method
   echo $row->getFullName();
}

Open in new window

Avatar of avir
avir

ASKER

Thanks for the explanation. It's a bit clearer now.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo