{"id":798,"date":"2022-08-30T15:07:21","date_gmt":"2022-08-30T15:07:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/codeigniter-undefined-variable-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:21","modified_gmt":"2022-08-30T15:07:21","slug":"codeigniter-undefined-variable-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/codeigniter-undefined-variable-collection-of-common-programming-errors\/","title":{"rendered":"Codeigniter &#8211; undefined variable-Collection of common programming errors"},"content":{"rendered":"<p>I am getting this error:<\/p>\n<p>A PHP Error was encountered<\/p>\n<p>Severity: Notice<\/p>\n<p>Message: Undefined variable: results<\/p>\n<p>Filename: views\/body.php<\/p>\n<p>Line Number: 75<\/p>\n<p>My Model<\/p>\n<pre><code>function get_results($search_term='default')\n{\n    $this-&gt;db-&gt;select('*');\n    $this-&gt;db-&gt;from('books');\n    $this-&gt;db-&gt;like('title', $search_term);\n\n    $query = $this-&gt;db-&gt;get();\n\n    return $query-&gt;result_array();\n}\n<\/code><\/pre>\n<p>My Controller<\/p>\n<pre><code>function execute_search(){\n\n\n    $this-&gt;load-&gt;model('books_model');\n    $search_term = $this-&gt;input-&gt;post('search');\n\n    $data['results'] = $this-&gt;books_model-&gt;get_results($search_term);\n\n   $this-&gt;load-&gt;view('body', $data);\n\n\n}\n<\/code><\/pre>\n<p>My View<\/p>\n<h1><code>Search Results<\/code><\/h1>\n<pre>\n\n\n\n    <\/pre>\n<pre>\n\n\n\n\n<\/pre>\n<h2><code>No books found<\/code><\/h2>\n<pre> \n\n\n\n<\/pre>\n<p><code>I do not understand because I have called the variable 'results' in my controller yet it is not working in the view. Thanks in advance for any help.<\/code><\/p>\n<ol>\n<li>\n<p><code>Try not returning the query result as an array until the for loop.<\/code><\/p>\n<p><code>Your Model:<\/code><\/p>\n<pre><code><code>function get_results($search_term='default')\n{\n    $this-&gt;db-&gt;select('*');\n    $this-&gt;db-&gt;from('books');\n    $this-&gt;db-&gt;like('title', $search_term);\n\n    $query = $this-&gt;db-&gt;get();\n\n    return $query;\n}\n<\/code><\/code><\/pre>\n<p>No changes in your Controller<\/p>\n<p>Your View<\/p>\n<h1><code>Search Results<\/code><\/h1>\n<pre>\n\n\n\n    <\/pre>\n<pre>\n\n\n\n\n<\/pre>\n<h2><code>No books found<\/code><\/h2>\n<pre> \n\n\n\n<\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:46:05. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am getting this error: A PHP Error was encountered Severity: Notice Message: Undefined variable: results Filename: views\/body.php Line Number: 75 My Model function get_results($search_term=&#8217;default&#8217;) { $this-&gt;db-&gt;select(&#8216;*&#8217;); $this-&gt;db-&gt;from(&#8216;books&#8217;); $this-&gt;db-&gt;like(&#8216;title&#8217;, $search_term); $query = $this-&gt;db-&gt;get(); return $query-&gt;result_array(); } My Controller function execute_search(){ $this-&gt;load-&gt;model(&#8216;books_model&#8217;); $search_term = $this-&gt;input-&gt;post(&#8216;search&#8217;); $data[&#8216;results&#8217;] = $this-&gt;books_model-&gt;get_results($search_term); $this-&gt;load-&gt;view(&#8216;body&#8217;, $data); } My View Search Results No [&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-798","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/798","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=798"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/798\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}