2

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

3
  • So, what is your question? Commented Aug 18, 2017 at 9:35
  • Put here some test datas and your query or what have you tried till now Commented Aug 18, 2017 at 9:36
  • Have you tried using subqueries and filtering by odd and even? Commented Aug 18, 2017 at 9:38

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

3 Comments

It is possible to achieve those results by SQL but I'm agree with you, using a table is more efficient.
So using php I parse the result and whenever the number is divisble by 2 a new table is created for the following sets?
if ($i % 2 == 0 && $i != count($records)) { // do ur thing }
1

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.

3 Comments

Altough I am close to what I need, Im having a bit of trouble, the columns are not being placed as they should for some reason the even column is being put in a new row and in the 2 columns of that row
Could you please upload a screenshot of the same. It will be helpful
Image isn't loading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.