How to allow duplicate mysql row id in array?

309
December 24, 2017, at 3:58 PM

My data in table t1 as below (only 2 record),

+-----------+-----------------+----------+
| shid      | lvlmin          | lvlmax   |
+-----------+-----------------+----------+
| 1         | 1               | 10       |
| 2         | 5               | 10       |
+----------------------------------------+

My php code is:

$userinfo[0] = '9';
$ghunt = DB::fetch_all("SELECT shid FROM t1 
     WHERE lvlmin <= ".$userinfo[0]." AND lvlmax >= ".$userinfo[0].
    "ORDER BY rand() LIMIT 5");
print_r($ghunt);

Result got 2 array:

Array ( [0] => Array ( [shid] => 2 ) [1] => Array ( [shid] => 1 ) )

How do I do when the array result is less than the LIMIT 5 in mysql query, auto use the array result in $ghunt to fill up the array?

What I mean is:

Array ( 
  [0] => Array ( [shid] => 2 )
  [1] => Array ( [shid] => 1 ) 
  [2] => Array ( [shid] => 2 ) 
  [3] => Array ( [shid] => 1 ) 
  [4] => Array ( [shid] => 1 )
)

The shid can be random place in array.

Answer 1

Why don't you do something like this?

If (count($ghunt) < 5){
    $realResultCount = count($ghunt);
    for ($i = realResultCount; $i <= 5; $i++){
        $ghunt[$i] = $ghunt[rand(0,realResultCount-1)];
    }
}

Basically, what above code does is, if ghunt has less than 5 records in it, it tops it up to 5, by randomly selecting records out of initially returned records.

Answer 2

I don't code PHP, but I can describe one way you can achieve your goal simply. Most languages have a MOD operator, usually % - the php manual page for mod is here

It gives us the remainder of a division operation, so 10 mod 3 is 1, because 10 divided by 3 is 9 remainder 1

A useful property of MOD then, is that it always cycles between 0 and 1 less than what you're modding by. If you mod an incrementing number by 5, the result will always be 0,1,2,3,4,0,1,2,3,4 in a cycle. This means you can have a for loop with some incrementing number, mod by an array length and the result will be an integer that is certainly an array index. If the loop variable goes higher than the end of the array, the mod operator will make it wrap round to the start of the array again

MyArray[ 1746262848 mod MyArray.length ] 

Will certainly not crash, even if the array only has 2 items

So for your case, just have a loop.. make he following pseudo code into PHP

// run the loop 5 times
For I as integer = 0 to 4 do
  Print MyArray[ i mod MyArray.length ]

If you have 2 items in your array, A and B, it will simply print ABABA

If you have 3 items A B C it will print ABCAB

Hopefully this info will be helpful to you for implementing a solution in php for this, and many future problems. Mod can be really useful for implementing various things when working with arrays

Rent Charter Buses Company
READ ALSO
Specify variable in array - PHP

Specify variable in array - PHP

I don't know if it's because i've been staring at the screen for too long or what, but I cannot seem to get this right

222
How to create wordpress category and subcategory for a custom post type?

How to create wordpress category and subcategory for a custom post type?

I created custom post type with a custom category, each category may or may not have subcategory and contain postsI am confused with

291