• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[GESIOR] Spliting SQL result into 2 columns.

raf

Active Member
Joined
Jan 10, 2011
Messages
261
Reaction score
38
Location
Warsaw, PL
Hey there,
i've been trying to split SQL result into 2 rows, to save some space on a page. I've seen alot of solutions on stackoverflow, but none of them worked for me (my guess would be something with `mysql_query` function).

This is my piece of code, including query and outputing data.
PHP:
$perStaffStatsQueryQuery = $SQL->query('SELECT * FROM channel_message WHERE `player_id`>0 AND `channel_id`=9 ORDER BY `count` DESC');


foreach ($perStaffStatsQueryQuery as $staffStatistic) {

    $player = new Player();
    $player->find($staffStatistic['player_name']);

    $main_content .= '<br>
    <div class="ui six wide">
        <table class="ui table-striped celled table" style="width:447px;margin: 10 auto;">
          <thead>
            <tr>
                <th class="text-center" colspan="3">
                    <a href="?subtopic=characters&name='.htmlspecialchars($staffStatistic['player_name']).'">
                        <h3 class="ui inverted header">'.$staffStatistic['player_name'].'</h3>
                    </a>
                    <p>'.htmlspecialchars(Website::getGroupName($player->getGroupID())).'</p>
                </th>
            </tr>
          </thead>
          <tbody >
            <tr>
              <td>Help channel replies</td>
              <td class="center aligned">'.$staffStatistic['count'].'</td>
            </tr>
          </tbody>
        </table>
    </div>';
}

and i'm trying to put it into this:
PHP:
$main_content .= '
<div class="ui equal width grid">
  <div class="equal width row">
    <div class="column">
        <div class="row">
            1st member
        </div>
        <div class="row">
            3rd member
        </div>
        <div class="row">
            5th member
        </div>
    </div>
    <div class="column">
        <div class="row">
            2nd member
        </div>
        <div class="row">
            4th member
        </div>
        <div class="row">
            6th member
        </div>
    </div>
  </div>
</div>
';

Those are questions i've found:
  1. http://stackoverflow.com/questions/2032758/split-records-into-two-columns
  2. http://stackoverflow.com/questions/...rds-in-half-to-display-on-each-side-of-a-page
  3. http://stackoverflow.com/questions/20442291/pdo-split-records-in-half
 
You could use the modulus operator, http://codepad.viper-7.com/1qGIV6 or you could split the results array into chunks (using array_chunk), and loop them accordingly, http://codepad.viper-7.com/Ydy4Zj
Thanks, that helped me alot :)

Managed to finish up with this:
PHP:
$test123 = $SQL->prepare('
        SELECT
            *
        FROM
            channel_message
        WHERE
            player_id>0
        AND
            channel_id=9
        ORDER BY
            count DESC
    ');
$test123->execute();

// Do we have any results?
    if ($test123->rowCount() > 0) {
        // Define how we want to fetch the results
        $test123->setFetchMode(PDO::FETCH_ASSOC);
        $it123 = new IteratorIterator($test123);

        $c = 0; // Our counter
        $n = 2; // Each Nth iteration would be a new row


        echo '
        <div class="ui equal width grid">
            <div class="equal width row">
                <div class="column">';
        foreach ($it123 as $test1) {



            if($c % $n == 0 && $c != 0) // If $c is divisible by $n...
            {
                // New row
                echo '</div><div class="column">';
            }
            $c++;
            echo $c;
            echo '<p>'.$test1['player_name'].'</p>';
        }
        echo '
                </div>
            </div>
        </div>';

    }
And it works (almost as i wanted)
 
Back
Top