简体   繁体   中英

table output php and mysql not getting result

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.

在此处输入图片说明

$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>';

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...:)

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>';

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