CodeIgniter Forums
Returning Number of Rows - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Returning Number of Rows (/showthread.php?tid=24340)

Pages: 1 2


Returning Number of Rows - El Forum - 11-06-2009

[eluser]Stenson[/eluser]
I have been trying to return the number of rows from my database using the function get_num_rows in my model to the controller but I cant seem to get it to work. I have been trying to figure it out for days now but still dont seem to be getting anywhere. If anyone could possibly point me in the right direction then that would be a great help.

This is my controller which uploads a file(mp3 in this case). I need to return the id(podID) from my podcasts table to be used as part of the mp3 file name.

Controller
Code:
function do_upload() {
        $dir = './uploads/podcast/';
        $segment = $this->input->post('lectureID');
        $config['upload_path'] = $dir;
        $config['allowed_types'] = 'mp3';
        $config['remove_spaces']  = TRUE;
        $config['max_filename'] = '64';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        
        if ( ! $this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            //redirect('lecture/editLecture/' . $segment . '/', $error);
            echo 'failed';
            //echo $segment;
        } else {
            echo 'works';
            //echo $segment;
            $data = array('upload_data' => $this->upload->data());
            $file = $data['upload_data']['file_name'];
            $filesize = $data['upload_data']['file_size'];
            $this->load->library('form_validation');
            $podTitle = $this->form_validation->set_rules('podTitle', 'podTitle', 'trim|required|max_length[64]|xss_clean');
            $this->load->model('podcast_model');
            $addPodcast = $this->podcast_model->add_podcast($filesize);
            
            $getNumRows = $this->podcast_model->get_num_rows();
            
            if($addPodcast == TRUE) {
                rename($dir . $file, $dir . $getNumRows . $segment . '.mp3');
                //echo 'Works';
                //redirect('lecture/editLecture/' . $segment . '/', $data);
                
            } else {
                // Fail
                //echo 'fail';
                //redirect('lecture/editLecture/' . $segment . '/', $error);
            }
        }
    }

Model
Code:
function get_num_rows();
        $query = $this->db->select('podID');
        $query = $this->db->where('lectureID', $this->uri->segment(3));
        $query = $this->db->get('podcasts');
        
        $numrows = $query->num_rows();
        return $numrows;
    }

Much Appreciated,

Jack


Returning Number of Rows - El Forum - 11-06-2009

[eluser]mah0001[/eluser]
In your get_num_rows() function check the value for the $this->uri->segment(3), may be it is empty. Just to test, try:
Code:
print $this->db->get("podcasts")->num_rows();exit;

If you see a number printed on the page, there is a problem with your where clause.

or better, enable the profiler to view the actual query being run. Add the following line at the beginning of the do_upload function:

Code:
$this->output->enable_profiler(TRUE);

With the profiler, you can see all the SQL queries list at the bottom of the page.


Returning Number of Rows - El Forum - 11-06-2009

[eluser]Thorpe Obazee[/eluser]
[quote author="Stenson" date="1257571219"]

Model
Code:
function get_num_rows();
        $query = $this->db->select('podID');
        $query = $this->db->where('lectureID', $this->uri->segment(3));
        $query = $this->db->get('podcasts');
        
        $numrows = $query->num_rows();
        return $numrows;
    }
[/quote]

Try this:

Model
Code:
function get_num_rows();
        $this->db->select('podID');
        $this->db->where('lectureID', $this->uri->segment(3));
        $query = $this->db->get('podcasts');
        
        $numrows = $query->num_rows();
        return $numrows;
    }

Do not declare $query on every line.


Returning Number of Rows - El Forum - 11-06-2009

[eluser]Thorpe Obazee[/eluser]
And it's much better to do this:

Model
Code:
function get_num_rows();
        $this->db->where('lectureID', $this->uri->segment(3));
        return $this->db->count_all_results('podcasts');
    }



Returning Number of Rows - El Forum - 11-06-2009

[eluser]Stenson[/eluser]
When i add the line
Code:
print $this->db->get("podcasts")->num_rows();exit;
it receives the number of rows in the table fine but when I try to add the line
Code:
echo $this->podcast_model->get_num_rows();
the code doesnt run beyond it.
I still cannot see any problems with the get_num_rows function as i've set the lectureID value to 35 which is stored in the lectureID column in my podcasts table.

Code:
function get_num_rows(;
        $this->db->where('lectureID', 35);
        return $this->db->count_all_results('podcasts');
    }

Any thoughts?


Returning Number of Rows - El Forum - 11-06-2009

[eluser]mah0001[/eluser]
can you re-post your code for the controller? the Suggestions from bargainph should have solved your problem.


Returning Number of Rows - El Forum - 11-06-2009

[eluser]Stenson[/eluser]
Code:
function do_upload() {
    //$this->output->enable_profiler(TRUE);
        
        $dir = './uploads/podcast/';
        $segment = $this->input->post('lectureID');
        $config['upload_path'] = $dir;
        $config['allowed_types'] = 'mp3';
        $config['remove_spaces']  = TRUE;
        $config['max_filename'] = '64';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        
        if ( ! $this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            //redirect('lecture/editLecture/' . $segment . '/', $error);
            echo 'failed';
            //echo $segment;
        } else {
            echo 'works';
            //echo $segment;
            $data = array('upload_data' => $this->upload->data());
            $file = $data['upload_data']['file_name'];
            $filesize = $data['upload_data']['file_size'];
            $this->load->library('form_validation');
            $podTitle = $this->form_validation->set_rules('podTitle', 'podTitle', 'trim|required|max_length[64]|xss_clean');
            $this->load->model('podcast_model');
            $addPodcast = $this->podcast_model->add_podcast($filesize);
            
            $getNumRows = $this->podcast_model->get_num_rows();
            print $this->db->get("podcasts")->num_rows();exit;
            
            if($addPodcast == TRUE) {
                rename($dir . $file, $dir . $getNumRows . $segment . '.mp3');
                echo 'Works';
                //redirect('lecture/editLecture/' . $segment . '/', $data);
                
            } else {
                // Fail
                echo 'fail';
                //redirect('lecture/editLecture/' . $segment . '/', $error);
            }
        }
    }
:?


Returning Number of Rows - El Forum - 11-06-2009

[eluser]mah0001[/eluser]
Please remove the line
Code:
print $this->db->get("podcasts")->num_rows();exit;
from your controller. and make sure in your function get_num_rows is exactly as below:

Code:
function get_num_rows(){
        $this->db->where('lectureID', $this->uri->segment(3));
        return $this->db->count_all_results('podcasts');
}



Returning Number of Rows - El Forum - 11-07-2009

[eluser]Stenson[/eluser]
It returns the number of podcasts but not the number of podcasts with the specified lectureID. It also does not run beyond the
Code:
print $this->db->get("podcasts")->num_rows();exit;

Should i not see the string 'Works' from within the if statement below it?


Returning Number of Rows - El Forum - 11-07-2009

[eluser]mah0001[/eluser]
The exit; terminates the script and nothing after that line gets executed. You need to remove it to line to execute rest of your code.