I have a select query whch eturns 55 results, how do I change it so that is returns groups of 5 so I can bvreak them up to be on the left/right so i dont just get 1 long line of reslts
Heres my query
$query = "SELECT beginning_slot, ending_slot, device FROM devices WHERE rack_id = ".$_GET['id']." AND display = 1";
Yes. You could loop through them and split them into separate arrays to then output to each div, for example, or if the sequence that the query returns them allows it, you could just detect when to switch to the âotherâ div.
Iâm not sure exactly how you want to display them - you say youâll get 55 back but want to split them into groups of five to display on the âleft/rightâ, but do you mean that you also want to paginate - only show ten then have a ânextâ button? Or do you want 23 on the left and 22 on the right?
Sorry John_Betong, Iâm trying to learn the most efficient way to do this so im going to do this from scratch using m_hutley methodâŚ
I used array_chunk() to split the results into groups of 5
Well donât be too surprised that the âefficientâ method is doing pretty much exactly what John did, in the background, just with command names.
array_map takes two* parameters. A function, and array(s) to send to that function. It applies the Function to each member of the array(s). (Lambda functions may be useful here.)
implode takes a string and an array, and reduces it to the concatenated string made by inserting the input string between each element of the array.
Take a look at the following array values that are valid integers that would pass the filter test. Obviously, these would not normally be valid idâs.
$values = array(-23, 0b11111111, 0x1A, 0b11111111);
foreach ($values as $value) {
if (filter_var($value, FILTER_VALIDATE_INT)) {
echo 'Variable is an integer<br>';
} else {
echo 'Variable is not integer<br>';
}
}
So what is the way to check for valid ID numbers only? is_numeric? No. ctype_digit? Closer, but no since an âidâ of zero will validate. So what then? The sure fire way to valid IDâs consisting only of numbers starting with a 1 or greater is with a regex.
Try to use the automatically generated integers in the for next loop I gave in my example and there is no reason to introduce another function to test for integer validation.
Also can you print_r($results); from the following and it will reduce incorrect assumptions. Only a couple of results are required.
What I used to do way back before I discovered prepared statements was to do a preg_replace on the ID.
$id = preg_replace('#[^0-9]#', '', $_GET[id]) ;
Which would just strip out anythingthing that was non-numeric.
Canât say I ever had any problems while using that, but I always use prepared statements now for user variables.
The difference from what I posted is your regex is sanitizing bad data while the regex I posted is validating good data. Two different things. You really shouldnât be changing user supplied data to make it âgood dataâ. If it is bad, it should just fail validation.
Even after passing through the sanitizing you could still end up with an invalid ID format requiring even more code to validate the changed data. Your regex would allow for an ID starting with one or more leading zeroâs which is not the norm for IDâs.
Take a look at your regex with bad data. It does strip everything but numbers but you could still be left with an id with one or more leading zeros.