Welcome Guest, Not a member yet? Register   Sign In
Blank row is being inserted instead of values from sql table.
#1

[eluser]reghan[/eluser]
Hello I am having problems and I am new to codeignitor.

Here is the code from my controller:
Code:
<?php

class Bia extends CI_Controller

{
function Bia()
{
  parent::__construct();
  $this->load->model('department_model');

  $this->load->helper('form');
  $this->load->helper('url');
  $this->load->helper('form_helper');
  $this->load->library('form_validation');
}



function index()
{


  $this->db->select('DepartmentName');
  $this->db->order_by('DepartmentName', 'asc')
  $query['depart_list'] = $this->db->get('department');


  $this->load->view('bia_home', $query);
}

}

?>

here is the code from my view:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
<h3>Business Impact Analysis(BIA) Questionnaire</h3>

  <p>The Business Impact Analysis (BIA) is a process
  to determine the mission critical business functions and
  associated critical resources. This will be accomplished for
  Manitoba Lotteries by completing the following six components:
  </p>

  &lt;?php if($depart_list->num_rows() == 0): ?&gt;

  <p>There are no Departments to List</p>

  &lt;?php else: ?&gt;
    <ul>
   &lt;?php foreach($depart_list->result() as $row): ?&gt;

      <li>&lt;?=  $row['DepartmentName'] ?&gt;</li>


    &lt;?php endforeach; ?&gt;

   </ul>
  &lt;?php endif; ?&gt;

&lt;/body&gt;
&lt;/html&gt;

The output prints three bullets for the unordered list but does not print the values. There are three rows of data in the table so it knows that there is the correct amount of rows its is just not giving me the values. The table name is department and the colums are departmentId, departmentName, departmentDescription. I just want to list all the Department Names in the table.

Anyone know what I am doing wrong?

Thanks in Advance.
#2

[eluser]gRoberts[/eluser]
On first thoughts:

a) You are loading a model but using the Database class directly in your controller...
b) $depart_list->result() returns an object, not an array, so your code would be:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
<h3>Business Impact Analysis(BIA) Questionnaire</h3>

  <p>The Business Impact Analysis (BIA) is a process
  to determine the mission critical business functions and
  associated critical resources. This will be accomplished for
  Manitoba Lotteries by completing the following six components:
  </p>

  &lt;?php if($depart_list->num_rows() == 0): ?&gt;

  <p>There are no Departments to List</p>

  &lt;?php else: ?&gt;
    <ul>
   &lt;?php foreach($depart_list->result() as $row): ?&gt;

      <li>&lt;?=  $row->DepartmentName; ?&gt;</li>


    &lt;?php endforeach; ?&gt;

   </ul>
  &lt;?php endif; ?&gt;

&lt;/body&gt;
&lt;/html&gt;

You should really generate an array of departments from your model and then pass this array to your view. Using the empty function (http://php.net/manual/en/function.empty.php) you can then test to see if any departments are in the array, or even use the count function (http://php.net/manual/en/function.count.php) to test how many departments are in the array before showing.

HTH
#3

[eluser]reghan[/eluser]
Thanks for your reply. I have coded the following:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
<h3>Business Impact Analysis(BIA) Questionnaire</h3>

  <p>The Business Impact Analysis (BIA) is a process
  to determine the mission critical business functions and
  associated critical resources. This will be accomplished for
  Manitoba Lotteries by completing the following six components:
  </p>

  &lt;?php if($depart_list->num_rows() == 0): ?&gt;

  <p>There are no Departments to List</p>

  &lt;?php else: ?&gt;
    <ul>
   &lt;?php foreach($depart_list->result() as $row): ?&gt;

      <li>&lt;?=  $row->DepartmentName; ?&gt;</li>



    &lt;?php endforeach; ?&gt;

    <p>&lt;?= $depart_list->num_rows(); ?&gt;</p>

   </ul>
  &lt;?php endif; ?&gt;




&lt;/body&gt;
&lt;/html&gt;
However it is still not working. Maybe I dont understand. but the view returns the following:

The Business Impact Analysis (BIA) is a process to determine the mission critical business functions and associated critical resources. This will be accomplished for Manitoba Lotteries by completing the following six components:
•DepartmentName; ?&gt;
•DepartmentName; ?&gt;
•DepartmentName; ?&gt;

num_rows(); ?&gt;


I am still stumped.

Thanks Again!
#4

[eluser]gRoberts[/eluser]
Change lines that start with

Code:
&lt;?=

to start with

Code:
&lt;?php echo

and see if that fixes it?
#5

[eluser]reghan[/eluser]
Yes that fixes it! Thanks! Do you know why that was the problem? I thought I could use the alternative syntax? Unless I was doing it wrong I am new to php and codeignitor.
#6

[eluser]gRoberts[/eluser]
Short tags are enabled within your PHP's INI file: http://www.php.net/manual/en/ini.core.ph...t-open-tag




Theme © iAndrew 2016 - Forum software by © MyBB