{"id":762,"date":"2022-08-30T15:06:45","date_gmt":"2022-08-30T15:06:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-make-this-php-code-more-object-oriented-and-less-scripted-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:06:45","modified_gmt":"2022-08-30T15:06:45","slug":"how-to-make-this-php-code-more-object-oriented-and-less-scripted-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-make-this-php-code-more-object-oriented-and-less-scripted-collection-of-common-programming-errors\/","title":{"rendered":"How to make this PHP code more object oriented and less scripted-Collection of common programming errors"},"content":{"rendered":"<p>I am utilizing CodeIgniter&#8217;s Pagination Class to implement pagination in the project. In this project, there are 3 modules: Event,Business &amp; Parts. Each Module has same &#8220;view&#8221; for pagination. I have written pagination code for the event module, which has 3 types of pagination depending on the condition (received from end user).<\/p>\n<p>Pagination ScreenShot: http:\/\/i.stack.imgur.com\/twM3e.png<\/p>\n<p>Following is the code for the event module. The controller:<\/p>\n<pre><code>class Event extends CI_Controller{\n      public function event_list($sort_by = 'Description', $sort_order = 'asc', $limit =         3,$offset = 0,$search = false,$category = false){\n    $config = array();\n        if($category){\n\n                $third_term = $this-&gt;uri-&gt;segment(3);\n                if(is_numeric($sort_by)){\n                    if(is_numeric($third_term)){\n                        $sort_by = 'Description';\n                    }else{\n                        $sort_by = 'Price';\n                    }\n                }\n                 $config['base_url'] = site_url(\"event\/categories\/$sort_by\/$sort_order\/$limit\");\n\n        }\n\n        elseif($search || $_POST){\n            if($_POST){\n                $search_data = array(\n                    'search_term' =&gt; '',\n                    'search_category' =&gt; '',\n                );\n                $this-&gt;session-&gt;unset_userdata($search_data);\n                $search_term = $this-&gt;input-&gt;post('search',TRUE);\n                $search_category = $this-&gt;input-&gt;post('select-search',TRUE);\n                $this-&gt;session-&gt;set_flashdata('search_term',$search_term);\n                $this-&gt;session-&gt;set_flashdata('search_category',$this-&gt;input-&gt;post('select-search',TRUE));\n                $config['base_url'] = site_url(\"event\/search_list\/$sort_by\/$sort_order\/$limit\");\n            }\n            else{\n                $this-&gt;session-&gt;keep_flashdata('search_term');\n                $this-&gt;session-&gt;keep_flashdata('search_category');\n                $search_term = $this-&gt;session-&gt;flashdata('search_term');\n                $search_category = $this-&gt;session-&gt;flashdata('search_category');\n\n                $config['base_url'] = site_url(\"event\/search_list\/$sort_by\/$sort_order\/$limit\");\n\n\n            }\n        }else{\n            $config['base_url'] = site_url(\"event\/event_list\/$sort_by\/$sort_order\/$limit\");\n        }\n\n        $data['fields'] = array(\n            'Description' =&gt; 'Description',\n            'Price' =&gt; 'Price'\n\n        );\n\n        $config['per_page'] = $limit;\n\n        $this-&gt;load-&gt;model('event_model');\n        $results = $this-&gt;event_model-&gt;search($limit, $offset, $sort_by, $sort_order,$search,$category);\n        $data['num_results'] = $results['num_rows'];\n        $data['events'] = $results['rows'];\n        $this-&gt;load-&gt;library('pagination');\n        $config['total_rows'] = $data['num_results'];\n        $config['uri_segment'] = 6;\n        $this-&gt;config-&gt;load('pagination');          \n        \/\/$this-&gt;pagination-&gt;initialize($config);\n        $data['pagination'] = $this-&gt;pagination-&gt;create_links();\n        $data['sort_by'] = $sort_by;\n        $data['sort_order'] = $sort_order;\n        $data['page_no'] = ceil($this-&gt;uri-&gt;segment(6)\/ $limit) + 1;\n        $total_pages = ceil($config['total_rows'] \/ $limit);\n        if($total_pages &gt; 1){\n            $data['total_pages'] = $total_pages;\n        }\n        $config['use_page_numbers'] = TRUE;\n\n        $data['limit'] = $limit;\/\/echo '<\/code><\/pre>\n<pre>'.print_r($config);exit();\n        $this-&gt;load-&gt;view('templates\/event\/event-list',$data);      \n    }\n\n       public function search_list($sort_by = 'Description', $sort_order = 'asc', $limit = 3,$offset = 0,$search = true,$category = false){\n         $this-&gt;event_list($sort_by,$sort_order,$limit,$offset,$search);\n    }\n\n       public function categories($sort_by = 'Description', $sort_order = 'asc', $limit = 3,$offset = 0,$search = false,$category = true){\n\n        $search_category = $this-&gt;uri-&gt;segment(3);\n\n        $in_flash =  $this-&gt;session-&gt;flashdata('search_category');\n        if(($in_flash == $search_category) &amp;&amp; (!is_numeric($search_category))){\n            $this-&gt;session-&gt;keep_flashdata('search_category');\n        }else{\n            $search_data = array(\n                    'search_term' =&gt; '',\n                    'search_category' =&gt; '',\n            );\n            $this-&gt;session-&gt;unset_userdata($search_data);\n            $this-&gt;session-&gt;set_flashdata('search_category',$search_category);\n        }\n\n        $this-&gt;event_list($sort_by,$sort_order,$limit,$offset,$search=false,$category = true);\n    }\n}\n<\/pre>\n<p><code>The Model:<\/code><\/p>\n<pre><code><code>class event_model extends CI_Model{\n\n\nfunction search($limit, $offset, $sort_by, $sort_order,$search,$category) {\n\n    $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';\n    $sort_columns = array('Description', 'Price');\n    $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'Description';\n\n    if($search){\n\n        $search_term = \"'%\".$this-&gt;input-&gt;post('search',TRUE).\"%'\";\n        $search_category = $this-&gt;input-&gt;post('select-search',TRUE);\n\n        $query =  sprintf('SELECT *,a.id AS eid,(SELECT Url from oc_storage where Object_Id = a.id and Object = \"event\" and type= \"thumb.normal\" limit 0,1 ) as thumb FROM oc_event a,oc_event_type b,oc_address c WHERE  b.id = %s AND a.Event_Type = b.id AND c.Object_type = \"event\" AND c.Object_Id = a.id AND a.Description like \"%s\" ORDER BY a.%s %s LIMIT %s ,%s',$search_category,$search_term,$sort_by, $sort_order,$offset,$limit);\n\n        $count = \"Select Count(*)FROM oc_event a,oc_event_type b,oc_address c WHERE  a.Event_Type = b.id AND c.Object_type = 'event' AND c.Object_Id = a.id AND a.Description like $search_term\";\n\n    }\n\n    if($category){\n\n         $search_category = $this-&gt;session-&gt;flashdata('search_category');       \n\n        $query =  sprintf('SELECT *,a.id AS eid,(SELECT Url from oc_storage where Object_Id = a.id and Object = \"event\" and type= \"thumb.normal\" limit 0,1 ) as thumb FROM oc_event a,oc_event_type b,oc_address c WHERE  b.id = %s AND c.Object_type = \"event\" AND c.Object_Id = a.id ORDER BY a.%s %s LIMIT %s ,%s',$search_category,$sort_by, $sort_order,$offset,$limit);\n\n         $count = \"Select Count(*)FROM oc_event a,oc_event_type b,oc_address c WHERE  a.Event_Type = $search_category AND c.Object_type = 'event' AND c.Object_Id = a.id\";\n    }\n    else{\n\n        $query = sprintf('SELECT *,a.id AS eid,(SELECT Url from oc_storage where Object_Id = a.id and Object = \"event\" and type= \"thumb.normal\" limit 0,1 ) as thumb FROM oc_event a,oc_event_type b,oc_address c WHERE  a.Event_Type = b.id AND c.Object_type = \"event\" AND c.Object_Id = a.id ORDER BY a.%s %s LIMIT %s ,%s',$sort_by, $sort_order,$offset,$limit);\n\n        $count = \"Select Count(*)FROM oc_event a,oc_event_type b,oc_address c WHERE  a.Event_Type = b.id AND c.Object_type = 'event' AND c.Object_Id = a.id \";\n    }\n\n    $stmt = $this-&gt;db-&gt;conn_id-&gt;prepare($query); \n    $stmt-&gt;execute();                   \n    $ret['rows']  = $stmt-&gt;fetchAll( PDO::FETCH_ASSOC ) ;\n\n    $ret['num_rows'] = $this-&gt;db-&gt;conn_id-&gt;query($count)-&gt;fetchColumn();\n\n    return $ret;\n\n}\n<\/code><\/code><\/pre>\n<p>The view : &#8220;Including only whats required&#8221;;<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:40:30. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am utilizing CodeIgniter&#8217;s Pagination Class to implement pagination in the project. In this project, there are 3 modules: Event,Business &amp; Parts. Each Module has same &#8220;view&#8221; for pagination. I have written pagination code for the event module, which has 3 types of pagination depending on the condition (received from end user). Pagination ScreenShot: http:\/\/i.stack.imgur.com\/twM3e.png [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-762","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=762"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/762\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}