Welcome Guest, Not a member yet? Register   Sign In
How can I pass multiple queries from different tables to a single view in CodeIgniter
#1

Hi everyone,
I'm very new to CodeIgniter. I'm trying to send queries from the controller to a single view. I have two different functions, each with a different query, and I want to style their results differently in the same view.
Here’s my controller code:
Code:
public function publications()
{
    $query = $this->db->query('SELECT P_ID, TITLE FROM publications WHERE SSN = 1');
    $data['name'] = 'publications';
    $data['section'] = 'Publication';
    $data['query'] = $query;
    $this->load->view('adel', $data);
}
public function teaching()
{
    $query = $this->db->query('SELECT C_ID, DEP_CODE, C_NAME, C_DESC FROM TEACHING WHERE SSN = 1');
    $data['name'] = 'teaching';
    $data['section'] = 'Teaching';
    $data['query'] = $query;
    $this->load->view('adel', $data);
}
And here’s the corrected view code:
Code:
<div class="col-lg-12">
    <h1><?= $section ?></h1>
    <?php if ($name == 'publications'): ?>
        <?php foreach ($query->result() as $row): ?>
            <?= $row->P_ID ?>: <?= $row->TITLE ?><br><br>
        <?php endforeach; ?>
       
    <?php elseif ($name == 'teaching'): ?>
        <?php foreach ($query->result() as $row): ?>
            <?= $row->DEP_CODE ?><?= $row->C_ID ?> <?= $row->C_NAME ?><br>
            Course Description:<br>
            <?= $row->C_DESC ?><br><br>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

Main fixes:
  • Use == for comparison, not = .
  • Use cleaner PHP short syntax inside HTML.
  • Close PHP properly for loops and conditions.
Reply


Messages In This Thread
How can I pass multiple queries from different tables to a single view in CodeIgniter - by Butted - 04-18-2025, 08:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB