简体   繁体   中英

PHP MYSQL - Search and Sort Query Results

hoping someone can help here.

I'm in the process of creating a site which will list horse racing tips, which are pulled from a MySQL database using PHP scripts. I've managed to create the database, which has five columns (ID, RaceDate, Location, RaceTime and Horse) and I've created some scripts that query the RaceDate column and only display the rows which match the current date. See below:

<?php
    $db_host = 'xxxxx'; // Server Name
    $db_user = 'xxxxx'; // Username
    $db_pass = 'xxxxx'; // Password
    $db_name = 'xxxxx'; // Database Name

    $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
        if (!$conn) {
            die ('Failed to connect to MySQL: ' . mysqli_connect_error());  
        }

    $sql = 'SELECT * FROM Horses WHERE Date(RaceDate) = Date(NOW())';
    $result = $conn->query($sql);

?>

I then have another bit of php further down my page which echo's the data to screen, formatted in a nice table.

<?php
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo '<tr>
                <td>'.$row['Location'].'</td>
                <td>'.$row['RaceTime'].'</td>
                <td>'.$row['Horse'].'</td>
            </tr>';
        }
    } else {
        echo '0 results';
    }
        $conn->close();
?>

That's all works great, but spits out everything in a big list. What I'd really like to be able to do is split the results up depending on the race location.

So, for example, there may be 5 races @ location1, 5 @ location2 and 5 @ location3. Rather than have 15 races listed one after the other, I'd like it split into 3 groups/tables. Is there a way to search through the Location column of my query data and determine what racecourses are listed, then echo the data to screen?

e.g IF Location = Wolverhampton
        THEN echo to table 1
    ELSE IF Location = Ayr
        THEN echo to table 2
    ELSE IF Location = Llos Fas
        THEN echo to table 3
    (etc, if more Locations exist)

My comment just complicated things, this is simpler way to do it.

while($row = $result->fetch_assoc()) {
     if($row['Location'] === 'location1')
     {
        echo '<tr>
            <td>'.$row['Location'].'</td>
            <td>'.$row['RaceTime'].'</td>
            <td>'.$row['Horse'].'</td>
        </tr>';
     }else if($row['Location'] === 'location2')
     {
        echo '<tr>
            <td>'.$row['Location'].'</td>
            <td>'.$row['RaceTime'].'</td>
            <td>'.$row['Horse'].'</td>
        </tr>';
     }else if($row['Location'] === 'location3')
     {
        echo '<tr>
            <td>'.$row['Location'].'</td>
            <td>'.$row['RaceTime'].'</td>
            <td>'.$row['Horse'].'</td>
        </tr>';
     }
     // And so on....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM