0

I have a PHP while loop that pulls from an SQL database and displays the contents in a table with two columns.

// Check Connection
    if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

// Select Data Table
    $result = mysqli_query($con,"SELECT * FROM Recommendations") or die(mysqli_error);

// Split Data
    $mid = ceil(mysqli_num_rows($result)/2);

// Display Content
    while ($rows = mysqli_fetch_array($result)) {
        $Name = $rows['Name'];
        $Author = $rows['Author'];
        $Cover = $rows['Link to Cover'];
        $Link = $rows['Link to Profile'];
    echo "<table><tr><td>
    <a href='" . $Link . "' >".$Name."</a>
    <br />".
    $Author.
    "<br />
    <a href='" . $Link . "' ><img src='" . $Cover . "' /></a>
    </td>
    <td>
    <a href='" . $Link . "' >".$Name."</a>
    <br />".
    $Author.
    "<br />
    <a href='" . $Link . "' ><img src='" . $Cover . "' /></a>
    </td></tr></table>";
    }
?>

I want to be able to display the looped results side by side in columns of two.

ex. 1 2
    3 4
    5 6

I've tried using pseudo classes to display only the even and odd results in the different table columns, but honestly have no idea how to do this. I'm new to PHP, so my apologies if the results are really obvious. Thanks in advance!

1
  • Hello Jake, I edited your post just to remove the unnecessary tags like HTML, CSS and modified your tag from mysql to mysqli because that is what you use. :) Welcome to stackoverflow buddy! Commented Aug 7, 2014 at 0:30

2 Answers 2

1

I'll show you a simple option to split the even and odd numbers.

foreach($numbers as $n) {
    ($n % 2 == 0) ? array_push($e, $n): array_push($o, $n);
}

The above is a short-hand ternary operator (?:)

The above function will check if the the number is even or odd. It stores the even/odd values in their own seperate arrays, which in this case is $e for even and $o for odd.

If you want to see how it works, Check this Example Out.


Using the above example, you'd do something like this (AS PSEUDO CODE)

WHILE(ROW = MYSQL.FETCH(DATA)) {
    MYDATA[] = ROW;
}

// now we do presentation
FOREACH(MYDATA AS ITEM) {
    IF(ITEM.NUMBER % 2 == 0) {
        PRINT EVEN.INFORMATION;       
    } ELSE {
        PRINT ODD.INFORMATION;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Without CSS: take advantage of the modulus function % which returns the remainder of a division. Odd #s return 1, even return 0. So for odd numbers start a row, even numbers end a row.

    echo "<table>";
    $i = 1;
    // Display Content
while ($rows = mysqli_fetch_array($result)) {
    $Name = $rows['Name'];
    $Author = $rows['Author'];
    $Cover = $rows['Link to Cover'];
    $Link = $rows['Link to Profile'];
    if ($i % 2 == 1) {
        echo "<tr>";
    }
    echo "<td>
    <a href='" . $Link . "' >$Name</a>
    <br />
    $Author
    <br />
    <a href='" . $Link . "' ><img src='" . $Cover . "' /></a>
    </td>
    <td>
    <a href='" . $Link . "' >$Name</a>
    <br />
    $Author
    <br />
    <a href='" . $Link . "' ><img src='" . $Cover . "' /></a>
    </td>";
    if ($i % 2 == 0) {
        echo "</tr>";
        $i ++;
    }
}

2 Comments

This didn't seem to work. I tried to replace the code and there seemed to be a parse error in it.
Sorry about that. I built the previous code on the fly without checking for completeness. Code has been edited so it may be worth another try.

Your Answer

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