简体   繁体   中英

PHP: Split results of query into 2 columns

I want to load contacts from a query, Whilst I have found ways to split by the amount of rows I want the result to be split into two columns as below as well as them being a sort of clickable button.Is it possible to display the query results as described below by the use of HTML or by the query itself? :

Contacts
------------------------------
Albert Smith   | Ben Marshall
Benjamin Jones | Chris Jones

I have tried the code below

<? 
$rowcount = mysql_num_rows($records);
echo "<div id='column1'>";
$i = 0;
while ($d = mysql_fetch_object($records)) {
  echo $d->name;
  $i++;

  if ($i == floor($rowcount / 2)) {
      echo "</div><div id='colums2'>";
  }
}
echo "</div>";
?>

But this splits the columns by reaching half of the results in 1 column and the rest in another column

Albert Smith | Benjamin Jones

Ben Marshall | Chris Jones

This is not possible by query itself. You must parse the result with an html <table> , every two results make a new <tr> and double <td>

I'm guessing you are conversant with coding: Check this algorithm

  1. get results
  2. declare an even/odd check variable
  3. loop through your result-set

  4. List item

    increment the counter in each loop

    ii. check counter value in the loop

    • iii. if counter variable value is even give it a different class
    • To make them a clickable button, you can use bootstrap as (ie. give an tag the style of a button)

Let me know on how it works out. Have fun coding.

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