Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert results of DB query into a variable in controller - CODEIGNITER
    primarykey
    data
    text
    <p>I have two tables.</p> <p>The finalists table which lists the finalists with fields such as ID, Name. The Votes table which stores the votes for those finalists. One row in the vote tables where the field "confirmed" is yes means one vote.</p> <p>To get the number of votes for a particular finalist I run</p> <pre><code>$this-&gt;db-&gt;where('finalist_id', $finalist_id); $this-&gt;db-&gt;where('confirmed', 'yes'); $query = $this-&gt;db-&gt;get('votes'); return $query-&gt;num_rows(); </code></pre> <p>And that gets me the number of rows.(votes)</p> <p>Now what I want to do is loop through my finalists and list their name, their ID and their number of votes but I am struggling to write the code in my controller.</p> <p>My controller :</p> <pre><code>$this-&gt;load-&gt;database(); $query = $this-&gt;db-&gt;get('finalists'); $data['finalists'] = $query-&gt;result_array(); $this-&gt;db-&gt;where('finalist_id', $finalist-&gt;id); $this-&gt;db-&gt;where('confirmed', 'yes'); $query = $this-&gt;db-&gt;get('votes') $data['votes'] = $query-&gt;result_array(); $this-&gt;template -&gt;build('admin/listing', $data); </code></pre> <p>My view:</p> <pre><code>&lt;?php foreach ($finalists as $finalist): ?&gt; &lt;li&gt;ID:&lt;?php echo $finalist['id'] ?&gt;, Name:&lt;?php echo $finalist['name'] ?&gt;, votes:&lt;?php echo $finalist['votes'] ?&gt;&lt;/li&gt; &lt;?php endforeach ?&gt; </code></pre> <p>SO what I am trying to achieve is to add the key vote and its result to the array $finalists - obviously to the right one, and I can't get my head around how to do that.</p> <p>I suppose I should loop through the finalists and somehow insert the result of the query for that particular finalist.</p> <p>Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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