简体   繁体   English

将数据库查询结果插入控制器中的变量-CODEIGNITER

[英]Insert results of DB query into a variable in controller - CODEIGNITER

I have two tables. 我有两张桌子。

The finalists table which lists the finalists with fields such as ID, Name. 决赛入围者表格列出了决赛入围者,其中包含ID,姓名等字段。 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. 投票表中“已确认”字段为“是”的一行表示一票。

To get the number of votes for a particular finalist I run 为了获得特定决赛入围者的票数,我进行了投票

$this->db->where('finalist_id', $finalist_id);
$this->db->where('confirmed', 'yes');
$query = $this->db->get('votes');
return $query->num_rows();

And that gets me the number of rows.(votes) 这样就可以得到行数。

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. 现在,我要做的是遍历决赛选手并列出他们的姓名,ID和投票数,但是我正努力在控制器中编写代码。

My controller : 我的控制器:

$this->load->database();

            $query = $this->db->get('finalists');
            $data['finalists'] = $query->result_array();


                $this->db->where('finalist_id', $finalist->id);
                $this->db->where('confirmed', 'yes');
                $query = $this->db->get('votes')
                $data['votes'] = $query->result_array();


    $this->template
            ->build('admin/listing', $data);

My view: 我的观点:

<?php foreach ($finalists as $finalist): ?>



<li>ID:<?php echo $finalist['id'] ?>, Name:<?php echo $finalist['name'] ?>, votes:<?php echo $finalist['votes'] ?></li>


<?php endforeach ?>

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. 因此,我要实现的目标是将关键投票及其结果添加到$ finalists数组中-显然是在正确的一个上,而我无法理解该怎么做。

I suppose I should loop through the finalists and somehow insert the result of the query for that particular finalist. 我想我应该遍历决赛选手,并以某种方式插入该特定决赛选手的查询结果。

Any help would be appreciated. 任何帮助,将不胜感激。

Try this into your controller 试试这个到你的控制器

   $this->load->database();
   $this->db->select('*');
   $this->db->from('finalists');
   $this->db->join('votes',finalists.id = votes.finalist_id);
   $this->db->where('votes.confirmed', 'yes');

   $query = $this->db->get();
   $data['finalists'] = $query;


   $this->template->build('admin/listing', $data);

and this will your view 这将你的看法

<?php foreach ($finalists->result() as $finalist){ ?>



<li>ID:<?php echo $finalist->id; ?>, Name:<?php echo $finalist->name; ?>, votes:<?php echo $finalist->votes; ?></li>


<?php } ?>

UPDATE 更新

Work on the raw SQL query first, then you can figure out how to implement in codeigniter second. 首先处理原始SQL查询,然后再找出如何在codeigniter中实现。 You have to have an understanding of the join first. 您必须先了解联接。 Something like this should get you started: 这样的事情应该让您入门:

SELECT f.id, f.name, COUNT(v.confirmed) AS votes
FROM finalists AS f
INNER JOIN votes AS v
  ON f.id = v.finalist_id
WHERE v.confirmed = 'yes'
GROUP BY f.id

This link should give you a better idea as to how to form more complex queries using codeigniter's ActiveRecord class. 该链接应该使您更好地了解如何使用codeigniter的ActiveRecord类形成更复杂的查询。 ellislab.com/codeigniter/user-guide/database/active_record.html ellislab.com/codeigniter/用户指南/database/active_record.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM