Codeigniter帮助

编程入门 行业动态 更新时间:2024-10-28 20:31:41
Codeigniter帮助 - 控制器中的活动记录和循环(Codeigniter help - active record and looping in controller)

我目前正在使用一个codeigniter应用程序,它可以让你根据各种标准来过滤作业,但它不能正常工作,基本上,应该发生的是,过滤后的用户应该以这种格式显示结果,

<section style="display: block;" class="employer"> <div class="job_holder"> <img src="http://lcl.doctrine.com/media/uploads/users/large_moovjob_large4.gif" alt="Test Company" height="148" width="198"> <dl> <dt>2</dt> <dd>Matches</dd> </dl> <!--<a href="" class="fave">Fave</a>--> <ul class="job_listings"> <li> + <a href="http://lcl.doctrine.com/jobwall/getjob/4" class="openjob"> PHP Backend Web Developer </a> </li> <li> + <a href="http://lcl.doctrine.com/jobwall/getjob/5" class="openjob"> Website Designer </a> </li> </ul> </div> </section>

但是我得到的是标记重复两次,我循环遍历一个看起来像这样的数组,

Array ( [0] => Array ( [job_id] => 4 [job_title] => PHP Backend Web Developer [salary] => 23000 [job_tags] => IT,Media [retrain] => no [job_extras] => We offer a very generous bonus scheme, including a company car, profit bonuses and a pension package. [job_summary] => The job consists of a 60/40 spilt of PHP and front end skills, you will be expected to be proficient in, PHP, OO, MySQL and JAVASCRIPT [job_description] => The job is a 60/40 split between the front and the backend of coding. You will be working within a team of 4 using an inhouse framework and where applicable an in house CMS. For working hours of 9-5.30 (we try not to stay too late but sometimes this is unnavoidable) you will be paid ?23,000, you will also be given a company car, and 1% of all yearly profits, we will also match any pension contributions that you make. [company_name] => Test Company [company_summary] => Test company is excatly what it says it is a test company, we have created this test company so that we can see that moovjob is functioning as it should be and that everything is upload, saving, applying and generally saving as we would expect. Hello [logo_small] => small_moovjob_small12.png [logo_large] => large_moovjob_large4.gif [employer_id] => 1 ) [1] => Array ( [job_id] => 5 [job_title] => Website Designer [salary] => 28000 [job_tags] => Media,Marketing [retrain] => no [job_extras] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim nisl, sodales dignissim tempus in, placerat id enim. Aenean eget diam velit. Morbi quis erat mi. Donec metus sem, consectetur at malesuada ac, iaculis nec arcu. Morbi libero leo, rhoncus ac vestibulum quis, vehicula nec metus. Vestibulum quis luctus erat. In varius accumsan ornare. Ut ullamcorper bibendum lorem ut elementum. [job_summary] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim nisl, sodales dignissim tempus in, placerat id enim. Aenean eget diam velit. Morbi quis erat mi. Donec metus sem, consectetur at malesuada ac, iaculis nec arcu. Morbi libero leo, rhoncus ac vestibulum quis, vehicula nec metus. Vestibulum quis luctus erat.us erat. In varius accumsan ornare. Ut ullamcorper bibendum lorem ut elementum. [job_description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim nisl, sodales dignissim tempus in, placerat id enim. Aenean eget diam velit. Morbi quis erat mi. Donec metus sem, consectetur at malesuada ac, iaculis nec arcu. Morbi libero leo, rhoncus ac vestibulum quis, vehicula nec metus. Vestibulum quis luctus erat. In varius accumsan ornare. Ut ullamcorper bibendum lorem ut elementum.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim nisl, sodales dignissim tempus in, placerat id enim. Aenean eget diam velit. Morbi quis erat mi. Donec metus sem, consectetur at malesuada ac, iaculis nec arcu. Morbi libero leo, rhoncus ac vestibulum quis, vehicula nec metus. Vestibulum quis luctus erat. In varius accumsan ornare. Ut ullamcorper bibendum lorem ut elementum. [company_name] => Test Company [company_summary] => Test company is excatly what it says it is a test company, we have created this test company so that we can see that moovjob is functioning as it should be and that everything is upload, saving, applying and generally saving as we would expect. Hello [logo_small] => small_moovjob_small12.png [logo_large] => large_moovjob_large4.gif [employer_id] => 1 ) )

我通过执行以下代码循环它,

<?php if(is_array($jobs)) : ?> <pre><?php print_r($jobs); ?></pre> <?php $count = count($jobs); ?> <?php foreach ($jobs as $j) : ?> <section class="employer"> <div class="job_holder"> <img src="<?php echo base_url(); ?>media/uploads/users/<?php echo $j['logo_large']; ?>" width="198" height="148" alt="<?php echo $j['company_name']; ?>"/> <dl> <dt><?php echo $count; ?></dt> <dd>Matches</dd> </dl> <!--<a href="" class="fave">Fave</a>--> <ul class="job_listings"> <?php foreach ($jobs as $j) : ?> <?php $job = $j['job_title']; ?> <li> + <a href="<?php echo base_url(); ?>jobwall/getjob/<?php echo $j['job_id']; ?>" class="openjob"> <?php echo $job; ?> </a> </li> <?php endforeach; ?> </ul> </div> </section> <?php endforeach; ?>

基本上我需要做的是回应公司名称,然后是公司的工作,然后继续前进,现在正在做什么,回应comapny名称然后它的工作,然后重复自己作为$jobs has 2结果,但他们都是同一家公司。

我模型中的代码如下,

public function search($salary = null, $location = null, $tags = null, $quick_tags = null) { $this->db->select('job_id, job_title, salary, job_tags, retrain, job_extras, job_summary, job_description, company_name, company_summary, logo_small, logo_large, employers.employer_id'); $this->db->from('jobs'); $this->db->join('employers', 'employers.employer_id = jobs.employer_id', 'left'); if(isset($salary)) { $this->db->where('jobs.salary >=', $salary); } if(isset($location)) { $this->db->where('jobs.city', $location); } if(isset($tags)) { $this->db->like('jobs.job_tags', $tags); } if(isset($quick_tags) && is_array($quick_tags)) { foreach ($quick_tags as $index => $value) { if ($index == 0) $this->db->like('jobs.job_tags', $value); else $this->db->or_like('jobs.job_tags', $value); } } $this->db->group_by('jobs.job_title'); $query = $this->db->get(); return $query->result_array(); }

I am working with a codeigniter application at the moment, and it lets you filter jobs, based on various critieria, however it is not quite working right, basically, what should happen is that after filtering a user should be shown a result in this format,

<section style="display: block;" class="employer"> <div class="job_holder"> <img src="http://lcl.doctrine.com/media/uploads/users/large_moovjob_large4.gif" alt="Test Company" height="148" width="198"> <dl> <dt>2</dt> <dd>Matches</dd> </dl> <!--<a href="" class="fave">Fave</a>--> <ul class="job_listings"> <li> + <a href="http://lcl.doctrine.com/jobwall/getjob/4" class="openjob"> PHP Backend Web Developer </a> </li> <li> + <a href="http://lcl.doctrine.com/jobwall/getjob/5" class="openjob"> Website Designer </a> </li> </ul> </div> </section>

However what I get is that markup repeated twice, I looping over an array that looks like this,

Array ( [0] => Array ( [job_id] => 4 [job_title] => PHP Backend Web Developer [salary] => 23000 [job_tags] => IT,Media [retrain] => no [job_extras] => We offer a very generous bonus scheme, including a company car, profit bonuses and a pension package. [job_summary] => The job consists of a 60/40 spilt of PHP and front end skills, you will be expected to be proficient in, PHP, OO, MySQL and JAVASCRIPT [job_description] => The job is a 60/40 split between the front and the backend of coding. You will be working within a team of 4 using an inhouse framework and where applicable an in house CMS. For working hours of 9-5.30 (we try not to stay too late but sometimes this is unnavoidable) you will be paid ?23,000, you will also be given a company car, and 1% of all yearly profits, we will also match any pension contributions that you make. [company_name] => Test Company [company_summary] => Test company is excatly what it says it is a test company, we have created this test company so that we can see that moovjob is functioning as it should be and that everything is upload, saving, applying and generally saving as we would expect. Hello [logo_small] => small_moovjob_small12.png [logo_large] => large_moovjob_large4.gif [employer_id] => 1 ) [1] => Array ( [job_id] => 5 [job_title] => Website Designer [salary] => 28000 [job_tags] => Media,Marketing [retrain] => no [job_extras] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim nisl, sodales dignissim tempus in, placerat id enim. Aenean eget diam velit. Morbi quis erat mi. Donec metus sem, consectetur at malesuada ac, iaculis nec arcu. Morbi libero leo, rhoncus ac vestibulum quis, vehicula nec metus. Vestibulum quis luctus erat. In varius accumsan ornare. Ut ullamcorper bibendum lorem ut elementum. [job_summary] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim nisl, sodales dignissim tempus in, placerat id enim. Aenean eget diam velit. Morbi quis erat mi. Donec metus sem, consectetur at malesuada ac, iaculis nec arcu. Morbi libero leo, rhoncus ac vestibulum quis, vehicula nec metus. Vestibulum quis luctus erat.us erat. In varius accumsan ornare. Ut ullamcorper bibendum lorem ut elementum. [job_description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim nisl, sodales dignissim tempus in, placerat id enim. Aenean eget diam velit. Morbi quis erat mi. Donec metus sem, consectetur at malesuada ac, iaculis nec arcu. Morbi libero leo, rhoncus ac vestibulum quis, vehicula nec metus. Vestibulum quis luctus erat. In varius accumsan ornare. Ut ullamcorper bibendum lorem ut elementum.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam enim nisl, sodales dignissim tempus in, placerat id enim. Aenean eget diam velit. Morbi quis erat mi. Donec metus sem, consectetur at malesuada ac, iaculis nec arcu. Morbi libero leo, rhoncus ac vestibulum quis, vehicula nec metus. Vestibulum quis luctus erat. In varius accumsan ornare. Ut ullamcorper bibendum lorem ut elementum. [company_name] => Test Company [company_summary] => Test company is excatly what it says it is a test company, we have created this test company so that we can see that moovjob is functioning as it should be and that everything is upload, saving, applying and generally saving as we would expect. Hello [logo_small] => small_moovjob_small12.png [logo_large] => large_moovjob_large4.gif [employer_id] => 1 ) )

I loop over it by doing the following code,

<?php if(is_array($jobs)) : ?> <pre><?php print_r($jobs); ?></pre> <?php $count = count($jobs); ?> <?php foreach ($jobs as $j) : ?> <section class="employer"> <div class="job_holder"> <img src="<?php echo base_url(); ?>media/uploads/users/<?php echo $j['logo_large']; ?>" width="198" height="148" alt="<?php echo $j['company_name']; ?>"/> <dl> <dt><?php echo $count; ?></dt> <dd>Matches</dd> </dl> <!--<a href="" class="fave">Fave</a>--> <ul class="job_listings"> <?php foreach ($jobs as $j) : ?> <?php $job = $j['job_title']; ?> <li> + <a href="<?php echo base_url(); ?>jobwall/getjob/<?php echo $j['job_id']; ?>" class="openjob"> <?php echo $job; ?> </a> </li> <?php endforeach; ?> </ul> </div> </section> <?php endforeach; ?>

Basically what I need to do, is echo out the company name and then the companies jobs, and then move on, what it is doing at the moment, is echoing out the comapny name and then its jobs and then repeating itself as $jobs has 2 results in it, but they are both for the same company.

The code in my model is below,

public function search($salary = null, $location = null, $tags = null, $quick_tags = null) { $this->db->select('job_id, job_title, salary, job_tags, retrain, job_extras, job_summary, job_description, company_name, company_summary, logo_small, logo_large, employers.employer_id'); $this->db->from('jobs'); $this->db->join('employers', 'employers.employer_id = jobs.employer_id', 'left'); if(isset($salary)) { $this->db->where('jobs.salary >=', $salary); } if(isset($location)) { $this->db->where('jobs.city', $location); } if(isset($tags)) { $this->db->like('jobs.job_tags', $tags); } if(isset($quick_tags) && is_array($quick_tags)) { foreach ($quick_tags as $index => $value) { if ($index == 0) $this->db->like('jobs.job_tags', $value); else $this->db->or_like('jobs.job_tags', $value); } } $this->db->group_by('jobs.job_title'); $query = $this->db->get(); return $query->result_array(); }

最满意答案

为了达到一致的结果

Employer job posting 1 job posting 2 Other employer job posting 1

您可能必须首先更改您的查询以按雇主对作业进行排序,以确保将作业组合在一起。

然后循环遍历您的数据(未经测试的伪代码)

employerCache = ''; for ($jobs as $job) { if ($job['company_name'] != employerCache) { // display company/employer header $employerCache = $job['company_name']; } // display job posting }

这将在同一数据集上为您提供1个循环而不是两个嵌套循环

To achieve a consistent result like

Employer job posting 1 job posting 2 Other employer job posting 1

You might have to alter you query to sort your jobs by employer first to be sure that the jobs are grouped together.

Then by looping through your data (untested pseudo-code)

employerCache = ''; for ($jobs as $job) { if ($job['company_name'] != employerCache) { // display company/employer header $employerCache = $job['company_name']; } // display job posting }

That will give you 1 loop instead of two nested loops on the same dataset

更多推荐

本文发布于:2023-08-04 12:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Codeigniter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!