Display multiple foreach db table selects on same page?

I’m working on a php project that involves displaying rows from a database into a view. I was successful at completing this task but now I see that I’ll have to display results in more sections than one from several db tables into the same view.

I’ve done quite a bit of coding with regular old PHP but now I’m working in the Codeigniter (PHP framework) for this project. I’m having trouble with my displaying multiple foreach statements on the same page.

Here is my code:

(php view info)
[php]




Recipes



Found <?php echo $num_results; ?> films
		<?php foreach($recipes as $recipe): ?>
			<?php echo "<div class='recipeclone jmStyleDivs tans'>"; ?>
				<?php foreach($fields as $field_name => $field_display): ?>
				<td>
					<?php echo $recipe->$field_name; ?>
				</td>
				<?php endforeach; ?>
			<?php echo "</div>"; ?>
		<?php endforeach; ?>
				
		<?php if (strlen($pagination)): ?>
		<div>
			Pages: <?php echo $pagination; ?>
		</div>
		<?php endif; ?>
		
	</div>
	<!--_____________-->
	<div id="healthtipside" class="span3">
		<p class="specialpara2 bold">Health Tips</p>
		
		<?php foreach($tips as $tip): ?>
			<?php echo "<div class='healthclone jmStyleDivs tans'>"; ?>
				<?php foreach($fields as $field_name => $field_display): ?>
				<td>
					<?php echo $recipe->$field_name; ?>
				</td>
				<?php endforeach; ?>
			<?php echo "</div>"; ?>
		<?php endforeach; ?>
				
		<?php if (strlen($pagination)): ?>
		<div>
			Pages: <?php echo $pagination; ?>
		</div>
		<?php endif; ?>
		
	</div>
	<!--_____________-->
</div>[/php]

(php model)
[php]<?php

class Model_getStuff extends CI_Model {

	public function recipes_show($limit, $offset, $sort_by, $sort_order) {
		
		$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
		$sort_columns = array('id, name, video');
		$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'id';
		
		// results query
		$q = $this->db->select('id, name, video')
			->from('recipes')
			->limit($limit, $offset)
			->order_by($sort_by, $sort_order);
		
		$ret['rows'] = $q->get()->result();
		
		// count query
		$q = $this->db->select('COUNT(*) as count', FALSE)
			->from('recipes');
		
		$tmp = $q->get()->result();
		
		$ret['num_rows'] = $tmp[0]->count;
		
		return $ret;
	}

	public function tips_show($limit, $offset, $sort_by, $sort_order) {
		
		$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
		$sort_columns = array('id, name, video');
		$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'id';
		
		// results query
		$q = $this->db->select('id, name, video')
			->from('tips')
			->limit($limit, $offset)
			->order_by($sort_by, $sort_order);
		
		$ret['rows'] = $q->get()->result();
		
		// count query
		$q = $this->db->select('COUNT(*) as count', FALSE)
			->from('tips');
		
		$tmp = $q->get()->result();
		
		$ret['num_rows'] = $tmp[0]->count;
		
		return $ret;
	}

}[/php]

(php controller)
[php]

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Site extends CI_Controller { public function menu($sort_by = 'id', $sort_order = 'asc', $offset = 0) { $this->load->view('site_header'); $this->load->view('site_nav'); $limit = 4; $data['fields'] = array( 'id' => 'ID', 'name' => 'Name', 'video' => 'Video' ); $this->load->model('Model_getStuff'); //Recipes query side $results = $this->Model_getStuff->recipes_show($limit, $offset, $sort_by, $sort_order); $data['recipes'] = $results['rows']; $data['num_results'] = $results['num_rows']; // pagination $this->load->library('pagination'); $config = array(); $config['base_url'] = site_url("site/menu/$sort_by/$sort_order"); $config['total_rows'] = $data['num_results']; $config['per_page'] = $limit; $config['uri_segment'] = 5; $this->pagination->initialize($config); $data['pagination'] = $this->pagination->create_links(); $data['sort_by'] = $sort_by; $data['sort_order'] = $sort_order; if ($this->session->userdata('is_logged_in')) { $this->load->view('view_menu', $data); } else { redirect('site/login'); } $this->load->view('site_footer'); } public function tipsect($sort_by = 'id', $sort_order = 'asc', $offset = 0) { $limit = 4; $data2['fields'] = array( 'id' => 'ID', 'name' => 'Name', 'video' => 'Video' ); $this->load->model('Model_getStuff'); //Recipes query side $results = $this->Model_getStuff->tips_show($limit, $offset, $sort_by, $sort_order); $data2['tips'] = $results['rows']; $data2['num_results'] = $results['num_rows']; // pagination /* $this->load->library('pagination'); $config = array(); $config['base_url'] = site_url("site/tipsect/$sort_by/$sort_order"); $config['total_rows'] = $data['num_results']; $config['per_page'] = $limit; $config['uri_segment'] = 5; $this->pagination->initialize($config); $data['pagination'] = $this->pagination->create_links(); $data['sort_by'] = $sort_by; $data['sort_order'] = $sort_order; */ $this->load->view('tipsect', $data2); } }[/php] I hope this is not too much clutter. Any suggestions would be great. Thanks
Sponsor our Newsletter | Privacy Policy | Terms of Service