Welcome Guest, Not a member yet? Register   Sign In
for loops in model that return an array
#1

[eluser]face1m[/eluser]
Greetings forum:
I am trying to convert some php code to the mvc.
Here is the code:
<?php
for( $i =1; $i <= 39 ; $i++ ) {
$result = mysql_query( "select count(*) from pball where pball =" . $i );
$count = mysql_result($result, 0);
$pball[$i] = $count;
}

?&gt;
This makes a repeated query to the db for the total count of number $i. Afterward the for loop completes I would like to return an array.

I figure the above code goes in the model and called by the controller.

I am stumped. I am new to codeigniter and mvc. I'm thinking there is an easy solution, but its hard hitting for me. thanks in advance.

rick mcelderry
#2

[eluser]theprodigy[/eluser]
Quote:I figure the above code goes in the model and called by the controller.
I am stumped. I am new to codeigniter and mvc. I’m thinking there is an easy solution, but its hard hitting for me. thanks in advance.

You are right in the fact that this code should belong in the model.
All you would need to do is create a function, put this code in the function, have the function return the $pball array to the controller.

Your model function might look something like:
Code:
function get_pball_counts()
{
    for(  $i =1; $i <= 39 ; $i++ )
    {
        $this->db->select('count(*)',false);
        $this->db->where('pball', $i);
        $result = $this->db->get('pball');

        $pball[$i] = $result->num_rows();
    }

    return $pball;
}
#3

[eluser]face1m[/eluser]
Thank you very much. You know i read about those db functions and building queries. However, I didn't put 2 and 2 together. Thanks for your help. Additionally, I'm now trying to loop through that array inside another loop.
&lt;?php

echo "<table id=\"table\" class=\"sortable\"><thead><tr><th ><h3>Powerball Number</h3></th><th ><h3>Times Drawn</h3></th></tr></thead><tbody>";
for ( $i = 1; $i <= 39; $i++ ) {

echo "<tr ><td width=\"50%\">" . $i . "</td>" . "<td>" . $pball[$i] . "</td></tr>";
}
echo "</tbody></table>";
?&gt;
The above prints out the powerball numbers col ok. However, for the times drawn col. it prints out 1 for all pball. Hmm. Any ideas. thanks in advance.

rick mcelderry
#4

[eluser]theprodigy[/eluser]
Change:
Code:
function get_pball_counts()
{
    for(  $i =1; $i <= 39 ; $i++ )
    {
        $this->db->select('count(*)',false);
        $this->db->where('pball', $i);
        $result = $this->db->get('pball');

        $pball[$i] = $result->num_rows();
    }

    return $pball;
}
to:
Code:
function get_pball_counts()
{
    for(  $i =1; $i <= 39 ; $i++ )
    {
        $this->db->select('count(*) as `drawn`',false);
        $this->db->where('pball', $i);
        $result = $this->db->get('pball');
        $row = $result->row();
        $pball[$i] = $row->drawn;
    }

    return $pball;
}
#5

[eluser]face1m[/eluser]
Looking at the code I think num_rows() would return 1 since there is one row return. How can i get the value of the count()?
Everthing is working besides that.

Thanks

rick mcelderry
#6

[eluser]face1m[/eluser]
thanks!!! Its working and i learned a lot from you.

rick




Theme © iAndrew 2016 - Forum software by © MyBB