Welcome Guest, Not a member yet? Register   Sign In
Help needed: how to pass multiple queries with different table names to the view ?
#1

[eluser]adelregh[/eluser]
Hi guys,

I am very new to codeigniter. I am trying to send a query from the controller to the view. I have to different functions with two different queries. How can I style the two different results in the single view I have? Sad

Does that make sense?

here is my code in the controller:

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);

}



here is my code in the view:

Code:
<div class="col-lg-12">
<h1>  &lt;?=$section ?&gt;  </h1>
                        
&lt;?php

if ($name = 'publications')
{
foreach ($query->result() as $row)
{
echo $row->P_ID;
echo ": ";
echo $row->TITLE;
echo "<br>" . "<br>";
}
                              
}

elseif ( $name = 'teaching')
{
foreach ($query->result() as $row)
{
echo $row->DEP_CODE;
echo $row->C_ID;
echo " ";
echo $row->C_NAME;
echo "<br>" . "Course Description: <br>";
echo $row->C_DESC;

}

}

?&gt;
</div>

any help would greatly appreciated Smile
#2

[eluser]InsiteFX[/eluser]
CodeIgniter Users Guide - HTML Table Class
#3

[eluser]adelregh[/eluser]
Thank you very much InsiteFX for your response. My question is not how to alter a table!

My question is, when you pass different queries from different functions from the controller (like "publications" and "teaching" in my code stated above), how do I manage to display the related one on the page??

I am really sorry if this does not make sense because I am a beginner at this Smile
#4

[eluser]adelregh[/eluser]
Never mind

I solved it with "switch", though I am curious why "if" did not work??

If someone could tell me how to set a font-style for each echo without having to play with css I would be grateful . here is the code:

Code:
foreach ($query->result() as $row)
                            {
                               echo $row->P_ID;
                               echo $row->TITLE;
                               echo "<br>" . "<br>";
                           }
#5

[eluser]Tim Brownlaw[/eluser]
Your IF never worked as you were setting $name = 'publications' which just so happens to evaluate to TRUE
Code:
if ($name = 'publications')
If you want to TEST if a var equals something you need == ( in the case of boolean ie testing against TRUE/FALSE you use === )
So you should of had...
Code:
if ($name == 'publications')
and
Code:
elseif ( $name == 'teaching')

The switch way of doing things works well...

And welcome to CI Land Smile
#6

[eluser]adelregh[/eluser]
I knew these rules but I was soooo blind. That is why it worked with the switch statement in witch I could not miss type Smile
Anyway, thank you very much
#7

[eluser]InsiteFX[/eluser]
If you use the HTML Table Class you can create a Table template and set the styles in it see the Users Guide.

In your Controllers __construct() add this:
Code:
// Needs to be updated to CI ver 2.2.0

  $tmpl = array (
   'table_open'   => '<table border="0" cellpadding="4" cellspacing="0">',

   'thead_open'   => '<thead>',
   'thead_close'   => '</thead>',

   'heading_row_start'  => '<tr>',
   'heading_row_end'  => '</tr>',
   'heading_cell_start' => '<th>',
   'heading_cell_end'  => '</th>',

   'tbody_open'   => '<tbody>',
   'tbody_close'   => '</tbody>',

   'row_start'    => '<tr>',
   'row_end'    => '</tr>',
   'cell_start'   => '<td>',
   'cell_end'    => '</td>',

   'row_alt_start'   => '<tr class="alt">',
   'row_alt_end'   => '</tr>',
   'cell_alt_start'  => '<td>',
   'cell_alt_end'   => '</td>',

   'table_close'   => '</table>'
  );

  $this->table->set_template($tmpl);
#8

[eluser]adelregh[/eluser]
Got it, thanks Smile




Theme © iAndrew 2016 - Forum software by © MyBB