CodeIgniter Forums
How to select all or get all the data in a table? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How to select all or get all the data in a table? (/thread-53213.html)



How to select all or get all the data in a table? - El Forum - 07-15-2012

[eluser]towki[/eluser]
I couldnt figure out waht wrong with my code, what i wanted is to display all the rows in a table but instead it displays only one row, only the latest data added to the database was being displayed.

This if my function in my controller(criminal.php)
Code:
public function viewRecords(){
$this->load->model('dbOperations');
$data['results']=$this->dbOperations->view_records();

$this->load->view('view_data', $data);

}
This i for my model(dbOperations.php):
Code:
public function view_records()
{
$query = $this->db->query("SELECT * FROM inputtest");
if ($query->num_rows() > 0){
return $query->result_array();
}
else{

echo "EMPTY!";
}

}

And for my view(view_data.php):
Code:
<h2>Criminal Records System</h2>

&lt;?php foreach ($results as $row) ?&gt;
<h2>&lt;?php echo $row['id'] ?&gt;</h2>
<h3>&lt;?php echo $row['Name'] ?&gt;</h3>

Pls anyone help me to clear this out. Im basically new to php and codeIgniter :-)



How to select all or get all the data in a table? - El Forum - 07-15-2012

[eluser]mr_prasanna[/eluser]
Replace
Code:
&lt;?php foreach ($results as $row) ?&gt;
<h2>&lt;?php echo $row['id'] ?&gt;</h2>
<h3>&lt;?php echo $row['Name'] ?&gt;</h3>

with this

Code:
&lt;?php foreach ($results as $row) ?&gt;
<h2>&lt;?php echo $row['id'] ?&gt;</h2>
<h3>&lt;?php echo $row['Name'] ?&gt;</h3>
&lt;?php endforeach; ?&gt;

Basically, endforeach is missing.


How to select all or get all the data in a table? - El Forum - 07-15-2012

[eluser]skunkbad[/eluser]
In the view, the way you wrote the foreach is probably wrong. mr_prasanna was right. I was going to suggest you try:

Code:
&lt;?php
foreach( $results as $row )
{
     echo '
          <h2>' . $row['id'] . '</h2>
          <h3>' . $row['Name'] . '</h3>
     ';
}

Even though you are using alternative syntax for your foreach, you have left out the colon(Smile, so I'm not sure it's going to work. Personally, I don't like the alternative syntax, and think you should use standard syntax because alternative syntax is cluttered.


How to select all or get all the data in a table? - El Forum - 07-15-2012

[eluser]towki[/eluser]
[quote author="mr_prasanna" date="1342365113"]Replace
Code:
&lt;?php foreach ($results as $row) ?&gt;
<h2>&lt;?php echo $row['id'] ?&gt;</h2>
<h3>&lt;?php echo $row['Name'] ?&gt;</h3>

with this

Code:
&lt;?php foreach ($results as $row) ?&gt;
<h2>&lt;?php echo $row['id'] ?&gt;</h2>
<h3>&lt;?php echo $row['Name'] ?&gt;</h3>
&lt;?php endforeach; ?&gt;

Basically, endforeach is missing.[/quote]

Thanks mr_passana for your reply. But I didnt get it to work when i tried your code.
It show me this error
Quote:Parse error: syntax error, unexpected T_ENDFOREACH in C:\xampp\htdocs\basicwebsite\application\views\view_data.php on line 6



How to select all or get all the data in a table? - El Forum - 07-15-2012

[eluser]towki[/eluser]
[quote author="skunkbad" date="1342365392"]In the view, the way you wrote the foreach is probably wrong. mr_prasanna was right. I was going to suggest you try:

Code:
&lt;?php
foreach( $results as $row )
{
     echo '
          <h2>' . $row['id'] . '</h2>
          <h3>' . $row['Name'] . '</h3>
     ';
}

Even though you are using alternative syntax for your foreach, you have left out the colon(Smile, so I'm not sure it's going to work. Personally, I don't like the alternative syntax, and think you should use standard syntax because alternative syntax is cluttered.[/quote]

I have tried your code skunkbad, and my problem was solved! But I dont understand why mr_passana was suggesting a endforeach while your code had no endforeach at all. Was my problem was caused by using alternative syntax??
Thanks to all of you for fast reply!




How to select all or get all the data in a table? - El Forum - 07-15-2012

[eluser]towki[/eluser]
Never mind. I knew it now. It was just the colon im missing and a endforeach in my syntax