{"id":609,"date":"2022-08-30T15:04:12","date_gmt":"2022-08-30T15:04:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/undefined-index-error-when-using-foreach-loop-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:12","modified_gmt":"2022-08-30T15:04:12","slug":"undefined-index-error-when-using-foreach-loop-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/undefined-index-error-when-using-foreach-loop-collection-of-common-programming-errors\/","title":{"rendered":"&#39;Undefined index&#39; error when using foreach loop-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve 2 tables named preacher &amp; Sermons<br \/>\nFields of Preacher<\/p>\n<pre><code>    preacher_id \n    first_name \n    last_name \n    preacher_image \n    preacher_logo \n    preacher_bio_brief \n    category\n<\/code><\/pre>\n<p>fields of sermons<\/p>\n<pre><code>    sermon_id\n    preacher_id \n    sermon_title \n    sermon_image \n    audio_file  \n    sermon_description \n    sort_order\n<\/code><\/pre>\n<p>I want to display all the sermons of each preacher by the order of preacher first_name.I got it properly but also got the below error<\/p>\n<pre><code>A PHP Error was encountered\n\nSeverity: Notice\n\nMessage: Undefined index: 1\n\nFilename: home\/sermons_view.php\n\nLine Number: 27\n<\/code><\/pre>\n<p>method in controller<\/p>\n<pre><code>function index() {\n    $res = $this-&gt;sermon_model-&gt;viewAllpreachers();\n    $this-&gt;data['preachers'] = $res;\n    $this-&gt;data['page'] = $this-&gt;config-&gt;item('APP_template_dir') . 'site\/home\/         sermons_view';\n    $this-&gt;load-&gt;vars($this-&gt;data);\n    $this-&gt;load-&gt;view($this-&gt;_container);\n}\n<\/code><\/pre>\n<p>Method in model<\/p>\n<pre><code>   function viewAllpreachers() {\n\n        $preacher = array();\n        $this-&gt;db-&gt;select('*');\n        $this-&gt;db-&gt;from('preacher');\n        $this-&gt;db-&gt;order_by('first_name');\n        $query = $this-&gt;db-&gt;get();\n        if ($query-&gt;num_rows() &gt; 0) {\n            foreach ($query-&gt;result() as $row) {\n                 $preacher[$row-&gt;preacher_id]['preacher_id'] = $row-&gt;preacher_id;\n                $preacher[$row-&gt;preacher_id]['preacher_name'] = $row-&gt;first_name . ' ' . $row-&gt;last_name;\n                $preacher[$row-&gt;preacher_id]['preacher_image'] = $row-&gt;preacher_image;\n                $preacher[$row-&gt;preacher_id]['preacher_bio_brief'] = $row-&gt;preacher_bio_brief;\n\n        $this-&gt;db-&gt;select('*');\n        $this-&gt;db-&gt;from('sermons');\n        $this-&gt;db-&gt;where('preacher_id',$row-&gt;preacher_id);\n        $query = $this-&gt;db-&gt;get();\n        if ($query-&gt;num_rows() &gt; 0) {\n            foreach ($query-&gt;result() as $row1) {\n                $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['sermon_id'] = $row1-&gt;sermon_id;\n                $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['preacher_id'] = $row1-&gt;preacher_id;\n                $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['sermon_image'] = $row1-&gt;sermon_image;\n                $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['sermon_title'] = $row1-&gt;sermon_title;\n                $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['audio_file'] = $row1-&gt;audio_file;\n                $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['sermon_description'] = $row1-&gt;sermon_description;\n            }\n                    }\n            }\n            return $preacher;\n        }\n        return false;\n    }\n<\/code><\/pre>\n<p>code in View<\/p>\n<pre><code>\n                    <\/code><\/pre>\n<tr>\n<td><\/td>\n<td colspan=\"2\"><\/td>\n<\/tr>\n<tr>\n<td style=\"padding-left: 50px;\"><\/td>\n<td><\/td>\n<td width=\"250px\"><\/td>\n<td><\/td>\n<\/tr>\n<p>I think its the problem with the resultant array I got that like<\/p>\n<pre><code>Array\n(\n [21] =&gt; Array\n        (\n            [preacher_id] =&gt; 21\n            [preacher_name] =&gt; Vance Havner\n            [preacher_image] =&gt; profile_image\/havner.jpeg\n            [preacher_bio_brief] =&gt; this is for testing \n            [42] =&gt; Array\n                (\n                    [sermon_id] =&gt; 42\n                    [preacher_id] =&gt; 21\n                    [sermon_image] =&gt; sermon_image\/image_81322797345.jpg\n                    [sermon_title] =&gt; 3 notes of the devil's tales\n                    [audio_file] =&gt; audio_file\/Niranja_Mizhiyum1.mp3\n                    [sermon_description] =&gt; 3 notes of the devil's tales   \n                )\n\n            [41] =&gt; Array\n                (\n                    [sermon_id] =&gt; 41\n                    [preacher_id] =&gt; 21\n                    [sermon_image] =&gt; \n                    [sermon_title] =&gt; The Lordship of Christs\n                    [audio_file] =&gt; audio_file\/Naladhamayanthi_Kadhayile.mp3\n                    [sermon_description] =&gt; the lordship of christ \n                )\n\n        )\n\n)\n<\/code><\/pre>\n<ol>\n<li>\n<p>there is no <code>$res<\/code> in your <code>model<\/code><\/p>\n<p>first debug what is <code>$res<\/code> with <code>print_r($res)<\/code> and<\/p>\n<p>AFAIK there must be <code>$val1['sermon_title']<\/code> instead of <code>$res['1']<\/code><\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:07:27. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve 2 tables named preacher &amp; Sermons Fields of Preacher preacher_id first_name last_name preacher_image preacher_logo preacher_bio_brief category fields of sermons sermon_id preacher_id sermon_title sermon_image audio_file sermon_description sort_order I want to display all the sermons of each preacher by the order of preacher first_name.I got it properly but also got the below error A PHP Error [&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-609","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/609","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=609"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/609\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}