简体   繁体   English

3个条件+随机顺序+分页(Code Igniter + MySQL)

[英]3 Conditions + Random Order + Pagination (Code Igniter + MySQL)

I have a tricky issue to solve, let me see if you can help me... 我有一个棘手的问题要解决,请让我看看您是否可以帮助我...

Im developing a services directory using CodeIgniter + MySQL and, due to my client's business rules, theres 3 plan types. 我使用CodeIgniter + MySQL开发了一个服务目录,并且由于我客户的业务规则,这里有3种计划类型。 (Platinum, Gold and Silver - in this order from best to worse) (白金,金和银-从好到坏按此顺序排列)

There is a table with the companies tha advertise in the directory, and each one of them may be paying one of this 3 plans. 目录中有一张桌子,上面有做广告的公司,每个公司可能都在付这三个计划中的一个。

The problem is: 问题是:

I have to present the results following this rules: 1. Platinum comes first, than gold, than silver 2. Each range of results MUST BE random itself 我必须遵循以下规则来陈述结果:1.铂金比黄金高,银比银高。2.每个结果范围本身必须是随机的

Platinum 2
Platinum 1 
Platinum 3 
Gold 3 
Gold 1
GOld 2
Silver 2 
Silver 3
Silver 1

refresh and i get: 刷新,我得到:

Platinum 1
Platinum 3 
Platinum 2 
Gold 2 
Gold 1
GOld 3
Silver 2 
Silver 1
Silver 3

The query in php is something like this: php中的查询是这样的:

// after a Select and several joins to filter city, provinces, service types
// i do this to randomize , order and group

        $query = $this->db->order_by('plan.level', 'desc');
        $query = $this->db->order_by('busines.id', 'random');

        $query = $this->db->group_by('business.id'); 
                return $query->result_array();

Until now, so far so good. 到现在为止,到目前为止还算不错。 The problem is, with so many results, i have to paginate them :) How can i do this keeping in mind that: 问题是,要获得如此多的结果,我必须对它们进行分页:)在记住这一点的情况下,我该如何做到这一点:

  • The plan priority must be the same through the pages 各个页面的计划优先级必须相同
  • the results must be random within the plan "blocks" 结果必须在计划“块”内是随机的
  • BUT the pagination CANT repeat records through pages or omiss some results 但分页无法通过页面重复记录或省略某些结果

i have to achieve something like this (eg with a 4 per page pagination) 我必须实现这样的目标(例如,每页分页为4)

Page I:
Platinum 1
Platinum 4
Platinum 5
Platinum 3

Page II:
Platinum 2
Gold 5
Gold 3
Gold 1

Page III:
Gold 4
Gold 2
Silver 2
Silver 5

Page IV:
Silver 1
Silver 3
Silver 4

and refreshing, randomly changing the position of records INSIDE THE PLAN BLOCKS, within the pages, without repeat or omiss results in the 4 pages, in this example. 在此示例中,刷新,随机更改页面内计划块内部的记录的位置,而没有重复或遗漏会导致4页。

Somebody can help me, please. 请有人可以帮助我。 Its a challenge one ;) Thanks, Adriano. 这是一个挑战;)谢谢,阿德里亚诺。

Since the randomization of results has to be preserved across sessions (for pagination), why don't you store in PHP instead of MySQL? 由于必须在会话之间保留结果的随机性(用于分页),因此为什么不存储在PHP中而不是MySQL中呢?

An initial query to get the randomized queries in MySQL: 初始查询以获取MySQL中的随机查询:

$this->db->select('id');
// The rest of your ActiveRecord query here, randomized

And perhaps storing the IDs in PHP: 也许将ID存储在PHP中:

$this->session->set_userdata('random_query', $array_of_random_ids);

Then querying it per page 然后每页查询一次

$array_of_random_ids = $this->session->userdata('random_query');
$this->db->from('business');
$this->db->where_in('id', array_slice($array_of_random_ids, $page_number * $length, $views_per_page);
$this->db->limit($views_per_page, $page_number * $length);

And of course resetting the session later, if that's how you choose to do it. 当然,如果您选择这样做的话,稍后再重置会话。

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

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