sushibrain
sushibrain

Reputation: 2780

change first and last value of PHP row

So I'm busy with a page where all the members of my website, with a certain rank, are shown. It's quite easy, this is the method I used:

<?php
        $rank = '10';
        $query = $db->conn->prepare('SELECT * FROM ht_users WHERE rank = ? OR rank2 = ?');
        $query->bind_param('ss', $rank, $rank);
        $query->execute();
        $result = $query->get_result();
        while($row = $result->fetch_assoc())
        {
            $content = '<div class="med">
                <b>' . $row['naam'] . '</b>
                <img src="http://127.0.0.1/imaging/avatarimage?user=' . $row['naam'] . '&direction=left" />
                <div style="margin-left:275px;"></div>
                </div>
            ';
            echo $content;
        }   
    ?>

It shows 2 people. I also used this on other ranks, but when I place them in a div, they all face left, since I'm using &direction=left in my image link. I can set the following properties: &direction=left, &direction=right, &direction=frontal

The thing I want to do is make the first character face left (&direction=left), the last one face right (&direction=right) and the ones in the middle face the front (&direction=frontal). Is it possible to make that happen?

Thanks.

Upvotes: 0

Views: 37

Answers (1)

Ali
Ali

Reputation: 3461

$rank = '10';
$query = $db->conn->prepare('SELECT * FROM ht_users WHERE rank = ? OR rank2 = ?');
$query->bind_param('ss', $rank, $rank);
$query->execute();
$result = $query->get_result();
$count = $result->num_rows;
$base = 1;
while($row = $result->fetch_assoc())
{
    $direction = 'frontal';
    if($base == 1) $direction = 'left';
    if($base == $count) $direction = 'right';
    $content = '<div class="med">
        <b>' . $row['naam'] . '</b>
        <img src="http://127.0.0.1/imaging/avatarimage?user=' . $row['naam'] . '&direction='. $direction .'" />
        <div style="margin-left:275px;"></div>
        </div>
    ';
    echo $content;
    $base++;
}   

This works by setting a base count of 1, meaning the first row, in each loop it checks if the base is 1, then it shows left (meaning first row), if the base is the last row, it shows right, else it shows frontal

Upvotes: 1

Related Questions