0

I'm currently working with a database that contains the following table structure:

round | current
===============
P     | 0
1     | 1
2     | 0
3     | 0
4     | 0
A     | 0

I have been trying to write PHP code that will output "a lit up circle" for every row in which current is one. So with the table above, 1 would be lit up because current is 1 - the rest of the other rounds would be represented by grey circles.

I've included an example here: https://i.sstatic.net/uUgkY.png

However the difficulty I'm having right now is that my code is outputting this: https://i.sstatic.net/JGbkO.png when its only meant to be returning a single row as above.

I've included the HTML output here: http://jsfiddle.net/pn8BW/

This is the PHP/MySQL I'm using now. Would appreciate some help on this because I've been working at it for an hour :-(:

<?php
        $sql = "SELECT * from ts_rounds";
        $result = $pdo->query($sql);
$circleSize = array ('circle_small', 'circle');
$rounds = '';
foreach ($result as $row) {

    switch ($row['round']) {
        case 'P':
            $rounds .=   '<div id="r0" class="'.$circleSize[$row['current']].'"><h3>P</h3></div>';
        case '1':
            $rounds .=   '<div id="r1" class="'.$circleSize[$row['current']].'"><h3>1</h3></div>';
        case '2':
            $rounds .=   '<div id="r2" class="'.$circleSize[$row['current']].'"><h3>2</h3></div>';
        case '3':
            $rounds .=   '<div id="r3" class="'.$circleSize[$row['current']].'"><h3>3</h3></div>';
        case '4':
            $rounds .=   '<div id="r4" class="'.$circleSize[$row['current']].'"><h3>4</h3></div>';
        case 'A':
            $rounds .=   '<div id="r5" class="'.$circleSize[$row['current']].'"><h3>A</h3></div>';
    }
}
echo $rounds;
        ?>
0

2 Answers 2

5

You forgot break; in your switch:

case 'P':
    $rounds .=   '<div id="r0" class="'.$circleSize[$row['current']].'"><h3>P</h3></div>';
    break;
Sign up to request clarification or add additional context in comments.

Comments

2

I think you want a break; before each subsequent condition test in your switch, e.g..

    case 'P':
        $rounds .=   ... ;
        break;
    case '1':
        $rounds .=   ... ;
        break;

Without a break;, the logic flow will "fall through" to the next action.

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.