简体   繁体   中英

How can you split sql results into two with php?

Lets say I get the names of costumers from a table named costumers. Now, I want to display the first three inside a special div, and then the rest in another. For example:

SELECT costumers FROM costumers LIMIT 7;

php code will go here

<div class="special">
    <em>Robert</em>
    <em>Mark</em>
    <em>Kevin</em>
</div>
<div class="therest">
    <em>Mary</em>
    <em>John</em>
    <em>Orange</em>
    <em>Tanya</em>
</div>

I have always done "while loops" and this displays a list of results. Currently I am doing two separate calls. I could leave as is, but I am trying to find out if it is possible to split a single call, and what the name of it is.

The function array_slice can help you

for example, your result are the following array:

<?php $results = array("Robert", "Mark", "Kevin", "Mary", "Jonn", "Orange", "Tanya"); ?>

and you can use the following scripts to split the array based on your question:

<?php
$array_1 = array_slice($results, 0, 3);
$array_2 = array_slice($results, 3);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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