Solved

codeigniter loop result inside loop

Posted on 2014-12-08
9
1,284 Views
Last Modified: 2014-12-10
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
0
Comment
Question by:avir
[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
  • 5
  • 4
9 Comments
 

Author Comment

by:avir
ID: 40486819
Here is how the db is set up.
db.gif
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40487391
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

0
 

Author Comment

by:avir
ID: 40487552
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.
0
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40487584
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.
0
 

Author Comment

by:avir
ID: 40488321
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

0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 40488506
OK. You just need to add the verses to the $data variable and then loop them in the view. I tend to use Objects instead of Arrays as I think it makes life a little easier. You'll need to change the result_array() to result() in your 2 models methods. Then something like this:

function homepage() 
{
     $data = new stdClass();
     $data->chapters = $this->book_model->get_chapters();
     foreach ($data->chapters as $chapter) {
          $chapter->verses = $this->book_model->get_verses($chapter->chapter);
     }

     $this->load->view('book/homepage', $data);
}

Open in new window


Then in your view:

<?php foreach ($chapters as $chapter): ?>
	<h2><?php echo $chapter->chapter ?></h2>
	
	<?php foreach ($chapter->verses as $verse): ?>
		<p><?php echo $verse->verseName ?><br><?php echo $verse->text ?></p> 		
	<?php endforeach; ?>

<?php endforeach; ?>

Open in new window

0
 

Author Closing Comment

by:avir
ID: 40488621
Great! Thanks for your help. Is there a difference between using Objects or Arrays other than what one might be used to?
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40489104
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

0
 

Author Comment

by:avir
ID: 40491516
Thanks for the explanation. It's a bit clearer now.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Session time out quickly. 6 55
Export Data from MySql Using PHP 16 67
Best hashing method? 1 22
Ajax success not firing alert 6 44
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn how to dynamically set the form action using jQuery.
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 …

739 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