简体   繁体   中英

How do I separate two SQL queries into separate HTML classes?

I have a MySQL query that selects two random rows in a table.

$API = mysql_query("SELECT * FROM APIs ORDER BY RAND() LIMIT 2") or die(mysql_error());

        while ($row = mysql_fetch_assoc($API)) {
         echo  "<div class=\"header\"><h1><a href=" . $row['Link'] . ">" . $row['Name'] . "</a></h1></div>";
}

Is there anything I can put into the while loop that will make the first result have a class of "header apiOne" and the second have a class of "apiTwo"?

How about this?:

$flag = FALSE; 

while ($row = mysql_fetch_assoc($API)) {
    $class = "header";
    if(!$flag) {
        $class .= " apiOne";
        $flag = TRUE;
    } else {
        $class .= " apiTwo";
    }

    echo  "<div class=\"$class\"><h1><a href=" . $row['Link'] . ">" . $row['Name'] . "</a></h1></div>";
}

Add a counter before your for loop. Set it to 0. In the loop do this "if counter == 0 then do A, else do B", then increments the counter (A and B being the two different actions you want to do).

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