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]