{"id":1590,"date":"2013-10-20T14:28:24","date_gmt":"2013-10-20T12:28:24","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=1590"},"modified":"2013-10-20T14:42:45","modified_gmt":"2013-10-20T12:42:45","slug":"codeigniter-one-record-treatment","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=1590","title":{"rendered":"CodeIgniter : One record and count treatment"},"content":{"rendered":"<p>This post will treat the case of one record treatment and counting in active record through the CodeIgniter&#8217; core. Suppose you are displaying a user&#8217;s profile. It&#8217;s good practice to store all database queries in the MODEL. I use this:<\/p>\n<p><strong>CONTROLLER:<\/strong><\/p>\n<pre>$this-&gt;load-&gt;model('Profile');\r\n$data['row'] = $this-&gt;Profile_model-&gt;profile_read(); \/\/get profile data\r\n$this-&gt;load-&gt;view('profile_view', $data); \/\/load data to view\r\n<\/pre>\n<p><strong>MODEL:<\/strong><\/p>\n<pre>\r\nfunction profile_read()\r\n{\r\n$this-&gt;db-&gt;where('user_id', $user_id);\r\n$query = $this-&gt;db-&gt;get('user_profiles'); \/\/get all data from user_profiles table that belong to the respective user\r\nreturn $query-&gt;row(); \/\/return the data\r\n}\r\n<\/pre>\n<p>In the model you can have all your other database functions (create, delete, update)<\/p>\n<p><strong>VIEW:<\/strong><\/p>\n<pre>\r\n&lt;?php echo $row-&gt;name; ?&gt;\r\n&lt;?php echo $row-&gt;email; ?&gt;\r\n&lt;?php echo $row-&gt;about; ?&gt;\r\n<\/pre>\n<p>Finally in the view you can echo any rows from the table that belong to the user.<\/p>\n<hr\/>\n<p>$query-&gt;row()<strong> just fetches the first row.<\/strong><\/p>\n<p>So instead of looping through the result, you could just do:<\/p>\n<pre>$student_id = $query-&gt;row()-&gt;student_id;<\/pre>\n<p>OR<\/p>\n<pre>\r\n$row = $query-&gt;row();\r\n$student_id = $row-&gt;student_id;\r\n<\/pre>\n<p>Assuming the query will always return a row.<br \/>\nIn the other case, the normal result parsing is :<\/p>\n<pre>$query = $this->db->query(\"YOUR QUERY\");\r\n\r\nforeach ($query->result() as $row)\r\n{\r\n   echo $row->title;\r\n   echo $row->name;\r\n   echo $row->body;\r\n}<\/pre>\n<hr\/>\n<h2>Difference between $query>num_rows() and $this->db->count_all_results()<\/h2>\n<p>There are two ways to count total number of records that the query will return. First this<\/p>\n<pre><code>$query = $this-&gt;db-&gt;query('select * from users');  \r\nreturn $query-&gt;num_rows();<\/code><\/pre>\n<p>This will return number of rows the query brought.<\/p>\n<p>Second<\/p>\n<pre><code>return $this-&gt;db-&gt;count_all_results('select * from users');<\/code><\/pre>\n<p>Simply count_all_results will require to run the query again.<\/p>\n<p>Cheers,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post will treat the case of one record treatment and counting in active record through the CodeIgniter&#8217; core. Suppose you are displaying a user&#8217;s profile. It&#8217;s good practice to store all database queries&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2841,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[348,250],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1590"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1590"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1590\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2841"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}