Welcome Guest, Not a member yet? Register   Sign In
Assistance needed for MVC newbie
#1

Hi there, I'm looking for some guidance with counting the # of rows from a query, please see below (I had this working fine when posting the variable).

Model
Code:
   // Get Proxy Rules
   function getAllRule($ip_address,$client_id){
       $result = $this->query("SELECT * FROM reverse_proxy_rules WHERE ipaddress='$ip_address' AND cust_id=$client_id");

       $num_rows = mysql_num_rows($result);

       return $result;

   }


Controller
Code:
// Add Rule Method
       function addRule(){
           $proxy_id = $_POST['proxy_id'];
           $this->loadLibrary('formvalidator.class');

           if($user = $this->logged_in())
               {
                       if($user['group_id']==1)
                       {
                               /**
                                * If $_POST data isn't set, form has
                                * not been submitted, load with no
                                * errors
                                */
                               if(!isset($_POST['bindaddress']) && !isset($_POST['bindport']) && !isset($_POST['connectaddress']) && !isset($_POST['connectport']))
                               {
                                       $this->redirect('navigator/view2/'.$id);
                               }
                               else
                               {
                                       $bindaddress = $_POST['bindaddress'];
                                       $bindport = $_POST['bindport'];
                                       $connectaddress = $_POST['connectaddress'];
                                       $connectport = $_POST['connectport'];
                                       $maxrule = $num_rows;

                                       /**
                                        * Validate user input for the new
                                        * project
                                        */
                                        $validator             = new FormValidator();
                                        $bindaddress_rule      = $validator->run($bindaddress, 'SRC IP Address', 'required|clean');
                                        $bindport_rule         = $validator->run($bindport, 'SRC Port', 'required|clean');
                                        $connectaddress_rule   = $validator->run($connectaddress, 'Destination IP address', 'required|clean');
                                        $connectport_rule      = $validator->run($connectport, 'Destination Port', 'required|clean');
                                        $maxrule_rule      = $validator->run($maxrule, 'Maximum Rules', 'clean');

                                                        /**
                                                        * Reload the form if there are errors
                                                        */
if ($validator->has_errors == true)
                                                       {
                                                         //echo "test21";

                                                               $this->set('bindaddress', $bindaddress_rule);
                                                               $this->set('bindport', $bindport_rule);
                                                               $this->set('connectaddress', $connectaddress);
                                                               $this->set('connectport', $connectport_rule);
                                                               $this->set('error_list', $validator->error_list);
                                                               //echo $validator->error_list;
                                                               $this->loadModel('Project');
                                                               $proxy = $this->Project->proxy_details($proxy_id);
                                                               $this->set('proxy', $proxy);

                                                               $this->loadView('view-proxy');
                                                               //$this->redirect('navigator/view2/'.$proxy_id);;
                                                               }
                                                               else if($bindport<1 || $bindport>65535){
                                           $this->set('error_list', '<div class="error">Incorrect SRC Port</div>');
                                           $this->loadModel('Project');
                                                            $proxy = $this->Project->proxy_details($proxy_id);
                                                        $this->set('proxy', $proxy);

                                         $this->loadView('view-proxy');
                                       }
                                       else if($connectport<1 || $connectport>65535){
                                           $this->set('error_list', '<div class="error">Incorrect Destination Port</div>');
                                           $this->loadModel('Project');
                                                            $proxy = $this->Project->proxy_details($proxy_id);
                                                        $this->set('proxy', $proxy);

                                         $this->loadView('view-proxy');
                                       }else if(filter_var($connectaddress, FILTER_VALIDATE_IP) == false){
                                           $this->set('error_list', '<div class="error">Incorrect Destination IP</div>');
                                           $this->loadModel('Project');
                                                            $proxy = $this->Project->proxy_details($proxy_id);
                                                        $this->set('proxy', $proxy);

                                         $this->loadView('view-proxy');
                                       }
                                         else if($maxrule>=5){
                                           $this->set('error_list', '<div class="error">You have exceeded your amount of allocated rules, please contact support.</div>');
                                           $this->loadModel('Project');
                                                            $proxy = $this->Project->proxy_details($proxy_id);
                                                        $this->set('proxy', $proxy);

                                         $this->loadView('view-proxy');
                                       }
                                       else
                                       {


Anyone know where I'm going wrong?
Reply
#2

I don't see where your model returns the value of $num_rows
CI 3.1 Kubuntu 19.04 Apache 5.x&nbsp; Mysql 5.x PHP 5.x PHP 7.x
Remember: Obfuscation is a bad thing.
Clarity is desirable over Brevity every time.
Reply
#3

Additionally, CI's db->query() method usually returns a result object, so you can use $result->num_rows() to get the number of rows, assuming your model's query() method is returning the result of the db->query() method.

You shouldn't call MySQL functions directly from your model, especially since the PHP mysql_* functions are deprecated (use the methods provided by CI's database library and configure it to use mysqli or pdo).
Reply




Theme © iAndrew 2016 - Forum software by © MyBB