1

I have some PHP code which executes and selects 10 rows from a SQL table. One column called result can hold the value of won, or loss.

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "<div class = 'logrow'> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ".  $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";
    }

How could I do something which would echo the values from each row as above but to echo a different statement for each row where the value of result is loss. For example to add an inline styling for the background colour.

So for example, say I have 10 rows found - 9 of these have $row["result"]as won, so they should be echoed as above. But 1 row has the value of $row["result"] as loss, a different echo should be applied. Perhaps with an inline style, or maybe with a variable inserted which hold this style.

I know this is very specific and may not be clear so thanks in advance.

4
  • just use an if... if($row["result"] == 'won'). Then you echo something different in the if than in the else... Commented Jul 9, 2015 at 12:54
  • Will this work for every row, if there are more than one? I always though while() had to be used here. If not, can you make this an answer so i can mark the question as answered. TIA Commented Jul 9, 2015 at 12:55
  • @Aba's answer is simple and efficient :) You can put the css in your actual css file... Commented Jul 9, 2015 at 12:59
  • Thanks random :) Aba's answer is definately awesome Commented Jul 9, 2015 at 13:00

2 Answers 2

3

Based on the value in $row["result"] (won/loss) create a class and use it in the echo

css:
    .won{background-color::blue}
    .loss{background-color::red}

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "<div class = 'logrow ". $row["result"] ."'> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ".  $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Try this by changing class names.

<?php 
 $status = $row['result'];
 $classname = 'won';
 if($status == 0){
   $classname = 'fail';
 }
?>
<div class = 'logrow <?php echo $classname ?> '> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ".  $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";

Now define classes for won and fail under style sheet.

<style type="text/css">
.won{ color: green; }
.fail{ color: red; }
</style>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.