Welcome Guest, Not a member yet? Register   Sign In
how to script the for loop in codeigniter using foreach
#1

[eluser]nOsCiRe[/eluser]
<?Php $num_data = $this->db->count_all('ad_users');


for ( $counter = 1; $counter <= $num_data; $counter += 1) {

echo "<tr><td>";
echo $counter;
echo "</td></tr>";
}
echo "</table>";

?&gt;
#2

[eluser]Akinzekeel[/eluser]
I don't think there's anything CI-specific about a foreach loop so I assume you'd like to know how to iterate through an active record resultset. This is how I usually do it:

Controller (you probably already have something similar to that):
Code:
$this->db->select("something");
$this->db->from("somewhere");
$data["result"] = $this->db->get()->result();

$this->load->view("some_view", $data);

View:
Code:
<table>
&lt;?php foreach( $result as $counter => $row ): ?&gt;
<tr>
  <td>
    &lt;?=$counter?&gt; &lt;?=$row->something?&gt;
  </td>
</tr>
&lt;?php endforeach; ?&gt;
</table>

Note that I'm using alternative PHP syntax in the view (just for convenience). If you're new to it, have a look at http://ellislab.com/codeigniter/user-gui...e_php.html
#3

[eluser]CroNiX[/eluser]
I believe count_all_results() returns a single integer telling how many rows a table has, not an array or any other data. Effectively, COUNT(*). So there's nothing to loop over in the first place.
http://ellislab.com/codeigniter/user-gui...lpers.html
#4

[eluser]Matalina[/eluser]
[quote author="CroNiX" date="1331742994"]I believe count_all_results() returns a single integer telling how many rows a table has, not an array or any other data. Effectively, COUNT(*). So there's nothing to loop over in the first place.
http://ellislab.com/codeigniter/user-gui...lpers.html[/quote]

he's not looping through the returned value he's using it as as top case in a for loop.
#5

[eluser]skunkbad[/eluser]
For OP, here is your foreach documentation:

http://php.net/manual/en/control-structures.foreach.php




Theme © iAndrew 2016 - Forum software by © MyBB