Note that there are some explanatory texts on larger screens.

plurals
  1. PO'Undefined index' error when using foreach loop
    text
    copied!<p>I've 2 tables named preacher &amp; Sermons<br> Fields of Preacher</p> <pre><code> preacher_id first_name last_name preacher_image preacher_logo preacher_bio_brief category </code></pre> <p>fields of sermons</p> <pre><code> sermon_id preacher_id sermon_title sermon_image audio_file sermon_description sort_order </code></pre> <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> <pre><code>A PHP Error was encountered Severity: Notice Message: Undefined index: 1 Filename: home/sermons_view.php Line Number: 27 </code></pre> <p>method in controller</p> <pre><code>function index() { $res = $this-&gt;sermon_model-&gt;viewAllpreachers(); $this-&gt;data['preachers'] = $res; $this-&gt;data['page'] = $this-&gt;config-&gt;item('APP_template_dir') . 'site/home/ sermons_view'; $this-&gt;load-&gt;vars($this-&gt;data); $this-&gt;load-&gt;view($this-&gt;_container); } </code></pre> <p>Method in model</p> <pre><code> function viewAllpreachers() { $preacher = array(); $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('preacher'); $this-&gt;db-&gt;order_by('first_name'); $query = $this-&gt;db-&gt;get(); if ($query-&gt;num_rows() &gt; 0) { foreach ($query-&gt;result() as $row) { $preacher[$row-&gt;preacher_id]['preacher_id'] = $row-&gt;preacher_id; $preacher[$row-&gt;preacher_id]['preacher_name'] = $row-&gt;first_name . ' ' . $row-&gt;last_name; $preacher[$row-&gt;preacher_id]['preacher_image'] = $row-&gt;preacher_image; $preacher[$row-&gt;preacher_id]['preacher_bio_brief'] = $row-&gt;preacher_bio_brief; $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('sermons'); $this-&gt;db-&gt;where('preacher_id',$row-&gt;preacher_id); $query = $this-&gt;db-&gt;get(); if ($query-&gt;num_rows() &gt; 0) { foreach ($query-&gt;result() as $row1) { $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['sermon_id'] = $row1-&gt;sermon_id; $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['preacher_id'] = $row1-&gt;preacher_id; $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['sermon_image'] = $row1-&gt;sermon_image; $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['sermon_title'] = $row1-&gt;sermon_title; $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['audio_file'] = $row1-&gt;audio_file; $preacher[$row1-&gt;preacher_id][$row1-&gt;sermon_id]['sermon_description'] = $row1-&gt;sermon_description; } } } return $preacher; } return false; } </code></pre> <p>code in View</p> <pre><code>&lt;?php if ($preachers) { foreach ($preachers as $val) { ?&gt; &lt;tr&gt; &lt;td&gt;&lt;img src="&lt;?= base_url(); ?&gt;uploads/&lt;?= $val['preacher_image']; ?&gt;"/&gt;&lt;/td&gt; &lt;td colspan="2"&gt;&lt;?= $val['preacher_name']; ?&gt;&lt;/td&gt; &lt;td colspan="2"&gt;&lt;?= $val['preacher_bio_brief']; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;? echo '&lt;pre&gt;'; //print_r($val); echo '&lt;/pre&gt;'; foreach ($val as $val1) { ?&gt; &lt;tr&gt; &lt;td style="padding-left: 50px; "&gt;&lt;img src="&lt;?= base_url(); ?&gt;uploads/&lt;?= $val1['sermon_image']; ?&gt;" style="width: 60px;height: 60px;"/&gt;&lt;/td&gt; &lt;td&gt;&lt;?= $val1['sermon_title']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?= $val1['audio_file']; ?&gt;&lt;/td&gt; &lt;td width="250px"&gt;&lt;?php $res = explode('/', $val1['audio_file']); if ($val1['audio_file']) { ?&gt;&lt;a id="play" href="&lt;?= site_url('sermons/playmp3/' . $val1['sermon_id']); ?&gt;"&gt;&lt;?= $res['1']; ?&gt;&lt;/a&gt;&lt;?php } ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?= $val1['audio_file']; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;? } } } ?&gt; </code></pre> <p>I think its the problem with the resultant array I got that like </p> <pre><code>Array ( [21] =&gt; Array ( [preacher_id] =&gt; 21 [preacher_name] =&gt; Vance Havner [preacher_image] =&gt; profile_image/havner.jpeg [preacher_bio_brief] =&gt; this is for testing [42] =&gt; Array ( [sermon_id] =&gt; 42 [preacher_id] =&gt; 21 [sermon_image] =&gt; sermon_image/image_81322797345.jpg [sermon_title] =&gt; 3 notes of the devil's tales [audio_file] =&gt; audio_file/Niranja_Mizhiyum1.mp3 [sermon_description] =&gt; 3 notes of the devil's tales ) [41] =&gt; Array ( [sermon_id] =&gt; 41 [preacher_id] =&gt; 21 [sermon_image] =&gt; [sermon_title] =&gt; The Lordship of Christs [audio_file] =&gt; audio_file/Naladhamayanthi_Kadhayile.mp3 [sermon_description] =&gt; the lordship of christ ) ) ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload