2

I’m currently working on a web site database. The entire site is coded in PHP (procedural style and not object oriented), of course with the obligatory CSS/HTML, etc.

I have everything working properly, but I am having trouble figuring out how to format search results the way I’d like.

Right now, I am displaying my search results in table format, with each cell of the table displaying a different variable from the search. All these little tables display vertically in one column, one after the other, until the end of the results.

What I am trying to do is display two columns of search results, side by side, to fit more results on the screen at one time. I have been able to display two columns of duplicate results, but I’d like two columns of alternating, non-duplicate results.

The current layout of my results is something like:

Result 1
Result 2
Result 3
Result 4

What I have been able to accomplish in my attempts:

Result 1  Result 1
Result 2  Result 2
Result 3  Result 3
Result 4  Result 4

What I would like to have:

Result 1  Result 2
Result 3  Result 4
Result 5  Result 6
Result 7  Result 8

Can anyone shed some light on how to achieve this? I’m not sure if it's a simple CSS/HTML formatting solution, or a PHP/MySQL solution.

Below is the code that specifically relates to the loop that displays my results:

$link = mysqli_connect( ALL MY INFO );

include('strings.php'); //This script sets the SQL search string based on user input.

echo '<br />';

$query = mysqli_query($link, $string) or die ("Error retrieving search results. Error in (main.php) search function.");

$resultrows = mysqli_num_rows($query);

echo '<table>
         <tr>
            <td colspan=2>
               <h3>Showing '.$resultrows.' results. </h3>
            </td>
         </tr>
         <tr>
            <td>';

while ($result = mysqli_fetch_assoc($query)){    
  include('results.php'); //This script formats each search result    
}

echo '     </td>
           <td>';

$query = mysqli_query($link, $string) or die ("Error retrieving search results. Error in (main.php) search function.");

while ($result = mysqli_fetch_assoc($query)){
  include('results.php');    
}

echo '     </td>
       </tr>
  </table>';
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Nathan
  • 21
  • 4
  • Look here http://stackoverflow.com/a/9153969/3200799 – Core972 Dec 13 '15 at 08:00
  • (On a side note, I apologize for the poorly formatted code. I'm working on an ancient laptop, and am at the mercy of Notepad...) – Nathan Dec 13 '15 at 08:01
  • @Nathan Began writing an answer before you posted your code, but I believe my example should be able to cleanly be mixed into your code. Perhaps it should end up the in the `results.php` logic, but the general concept works. Best of luck! – Giacomo1968 Dec 13 '15 at 08:06

1 Answers1

0

I believe the magical solution to your problem would be to use the modulus (remainder) operator in PHP:

Remainder of $a divided by $b.

Might be an odd concept to understand if you’re not into math—and programming is all about logic really—but once you get what a modulus (remainder) operator can do, it is a magical thing.

Basically calculate a modulus based on a counter within a loop. And then at that point the remainder of the modulus value would be 0… Et voilà! You can do something! The examples below use tables and rows, but the same, basic concept could be used for other HTML elements or any formatting logic.

For example, let’s take this simple PHP code to create a table based on a simple alphabet array:

$array = range('a','z');

echo '<table border="1">';
echo '<tr>';

foreach ($array as $key => $value) {
  echo '<td>';
  echo $value;
  echo '</td>';
}

echo '</tr>';
echo '</table>';

The output would be one table with one large ugly row that renders like this:

a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y   z

But if we add some modulus logic to the mix like this:

$row_cell_amount = 2;
$array = range('a','z');

echo '<table border="1">';
echo '<tr>';

$counter = 1;
foreach ($array as $key => $value) {
  echo '<td>';
  echo $value;
  echo '</td>';
  if ($counter % $row_cell_amount == 0 && $counter != 0) {
    echo '</tr>';
    echo '<tr>';
  }
  $counter++;
}

echo '</tr>';
echo '</table>';

The output will cleanly add a new row every time the $counter value evenly matches the modulus operator resulting in 0; aka: no remainder. So the output would look like this with a value of 2 for the $row_cell_amount:

a   b
c   d
e   f
g   h
i   j
k   l
m   n
o   p
q   r
s   t
u   v
w   x
y   z

And if you adjusted the $row_cell_amount to be something like 4, the output would be something like this:

a   b   c   d
e   f   g   h
i   j   k   l
m   n   o   p
q   r   s   t
u   v   w   x
y   z

Small note but in this simple example you can perhaps set $key+1 instead of creating a $counter but just showing the $counter method since you might be dealing with associative arrays that are non-numerical or non-numerically ordered as far as key values go.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103