0

I have the following: The result I am getting is the image attached. I would like the username to be listed only once and then the win, loose across the page. not a row for each result.

enter image description here

$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY 'username' asc ")
or die (mysql_error()); 
        while ($row = mysql_fetch_array($sql_events)) {
    $username = $row["username"];
    $week = $row["win_loose"] ;

   $row_color = ($row_count++ % 2 == 0 ? $color1 : $color2);
    echo '<tr style="background-color: '.$row_color.';">';
    echo '<td style="width: 100" align="center"><font size="2">'.$username.'</td>';
    echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
    echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
    echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
    echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';

2 Answers 2

2

you have to update your code. You are querying correctly but taking the data in a wrong way. You have to try associative array to solve your problem. Please try following codes-

$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY 'username' asc ") or die (mysql_error()); 

while ($row = mysql_fetch_array($sql_events)) {
    $username[$row["username"]][] = $row["win_loose"];
}

foreach($username as $key=>$val){
    echo '<td style="width: 100" align="center"><font size="2">'.$username.'</td>';
    foreach($val as $value){
        echo '<td style="width: 50" align="center"><font size="2">'.$value.'</td>';
    }
}

One more thing to mention, it is not a good practice to use inline css on every table column. You could use a class and get the css to the css file attached to that class.

Hope it helps...:)

Sign up to request clarification or add additional context in comments.

1 Comment

I think you need to get the 'username' out of the quotes too, otherwise you're ordering by the same string on everything.
1

You're pre-echoing weeks for which you don't have data yet. The idea is, print a cell for each week as long as it's referring to the same user:

$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY username asc ")
        or die(mysql_error());

$current_username = null;
while ($row = mysql_fetch_array($sql_events)) {
    $username = $row["username"];    
    //User changed
    if ($current_username == null || $current_username != $username) {        
        if ($current_username != null) {
            echo '</tr>'; //Had another user before so end the row
        }
        $row_color = ($row_count++ % 2 == 0 ? $color1 : $color2);       
        echo '<tr style="background-color: ' . $row_color . ';">';
        echo '<td style="width: 100" align="center"><font size="2">' . $username . '</td>';
        $current_username = $username;
    }    
    $week = $row["win_loose"];
    echo '<td style="width: 50" align="center"><font size="2">' . $week . '</td>';
}
echo '</tr>';

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.