Welcome Guest, Not a member yet? Register   Sign In
how to get data with many parameter passing..?
#1

[eluser]pipiet06[/eluser]
Hi all..

i wanna getData for my search in js.
but,, every I did search there is a bug "<b>Fatal error</b>: Call to a member function num_rows() on a non-object in <b>C:\wamp\www\hid\hidsystem\application\models\ed\MSummary.php</b> on line <b>47</b>"

this is my controller
Code:
function get_data()
        {
            $start = ($this->input->post('start'))? $this->input->post('start'):0;
            $limit = ($this->input->post('limit'))? $this->input->post('limit'):10;
            $query = $this->input->post('query');
            $fields = $this->input->post('fields');
            
            $startDate = $this->input->post('startDate');
            $endDate = $this->input->post('endDate');
            $idsite = $this->input->post('idsite');
            $idfleet = $this->input->post('idfleet');
            $idprefix = $this->input->post('idprefix');
            $eqplan = $this->input->post('eqplan');
            $category = $this->input->post('category');
            $type = $this->input->post('type');
            $idcomponent = $this->input->post('idcomponent');
            $description = $this->input->post('description');
            
            $query = $this->MSummary->getData($start,$limit,$query,$fields,$startDate,$endDate,$idsite,$idfleet,$idprefix,$eqplan,$category,$type,$idcomponent,$description);
            $total = $query['num_rows'] ;
            $rowData = array("total"=>$total);
            if ($total > 0 )
            {
                foreach($query['result'] as $row)
                {
                    $rowData['rows'][] = array(
                        'idvims_summary'=>$row['idvims_summary']
                        ,'startDate'=>$row['event_date']
                        ,'idsite'=>$row['idsite']
                        ,'idfleet'=>$row['idfleet']
                        ,'idprefix'=>$row['idprefix']
                        ,'eqplan'=>$row['eqplan']
                        ,'category'=>$row['category']
                        ,'type'=>$row['type']
                        ,'idcomponent'=>$row['idcomponent']
                        ,'id_event'=>$row['id_event']
                        ,'description'=>$row['description']
                        ,'rating'=>$row['rating']
                    );
                }
            }
            else{
                $rowData['rows'] = "";
            }
            echo json_encode($rowData);
        }


this is my models
Code:
function getData($start,$limit,$query,$fields,$startDate,$endDate,$idsite,$idfleet,$idprefix,$eqplan,$category,$type,$idcomponent,$description)
        {
            if ($query)
            {
                /**
                 * Field Parsing
                 */
                $fields = str_replace("[","",$fields);
                $fields = str_replace("]","",$fields);
                $fields = str_replace("\"","",$fields);
                $fields = explode(",",$fields);
                $search = array();
                foreach  ($fields as $fd)
                {
                    $search[$fd] = $query;
                }
                // $data['num_rows'] =  $this->db->count_all_results('tblvims_summary');
                [color=red]$sql = $this->db->query("SELECT
                                            a.*
                                            , b.description
                                            FROM
                                                tblvims_summary a
                                            LEFT JOIN
                                                tblid_event AS b ON b.id = a.id_event
                                            OR_LIKE
                                                $search
                                            LIMIT
                                                $start,$limit"
                                        );
                $queryn = $this->db->query($sql);
                
                if ($queryn->num_rows() > 0) {
                    $data['num_rows'] = $queryn->num_rows();                    
                    $query = $this->db->query($sql . "LIMIT $start,$limit");
                    $data['result'] = $query->result_array();
                    $this->output->enable_profiler(true);
                exit;
                }
                
                // $data['result'] = $query->result_array();
                return ($query->num_rows() > 0) ?  $data : FALSE;
            }[/color]
            else
            {                                        
                $sql = "SELECT
                        a.*
                        , b.description
                        FROM
                            tblvims_summary a
                        LEFT JOIN
                            tblid_event AS b ON b.id = a.id_event
                        where
                        DATE(event_date) >= STR_TO_DATE('$startDate', '%d-%m-%Y') AND DATE(event_date) <= STR_TO_DATE('$endDate', '%d-%m-%Y')
                        AND idsite='$idsite'
                        AND idfleet='$idfleet'
                        AND idprefix='$idprefix'
                        AND eqplan='$eqplan'
                        AND category='$category'
                        AND a.type='$type'
                        AND a.idcomponent='$idcomponent'";
                $queryn = $this->db->query($sql);
                if ($queryn->num_rows() > 0) {
                    $data['num_rows'] = $queryn->num_rows();                    
                    $query = $this->db->query($sql . "LIMIT $start,$limit");
                    $data['result'] = $query->result_array();
                }
                return ($query->num_rows() > 0) ?  $data : FALSE;
            }

what i'm missing or my code is wrong?

thanks
#2

[eluser]WanWizard[/eluser]
In your model you are using $queryn to store the result of the query. Yet, a few lines further, you use $query to return the result.
#3

[eluser]pipiet06[/eluser]
i use
Code:
$queryn = $this->db->query($sql);
because there is
Code:
if ($queryn->num_rows() > 0) {
   $data['num_rows'] = $queryn->num_rows();                    
   $query = $this->db->query($sql . "LIMIT $start,$limit");
   $data['result'] = $query->result_array();
}
in the bellow

can someone gimme a right code..
#4

[eluser]WanWizard[/eluser]
I understand that, but if $queryn->num_rows() returns zero, $query is never defined.
Restructure your code so that it doesn't contain logic errors.

Also, my I suggest reading up on how codeigniter works? This is not the way to write a good program. No input validation, hardcoded queries without proper variable escaping.

And, running a query and creating a resultset just to get a count is not a smart thing to do. Either run this query, get the count, and then use $query->row($start) and $query->next_row() to get the records for the page you want, or don't do a select but a count(*) to get the count.




Theme © iAndrew 2016 - Forum software by © MyBB