php - Combine two mysql results -


this situation..

while($row = mysql_fetch_assoc($sql)){  $name = $row['name']; $class = $row['class']; $result = $row['phone'];  $data .= '<td>'.$name.'</td><td>'.$class.'</td><td>'.$result.'</td>'; }    echo "<table><tr>'.$data.'</tr><table>" 

$name , $class occur once $result occur more once. want show them in single html row..like this

kevin | ix | 5343634,3565656 melvin | x | 54534,3778,54434 

one possible approach: collect results in associative array first, indexed name *as seem unique), whatever want results. example:

$results = array(); while ($row = mysql_fetch_assoc($sql)) {   $name = $row['name'];   $results[$name]['class'] = $row['class'];   $results[$name]['phone'][] = $row['phone']; }  echo '<table>'; foreach ($results $name => $result) {   echo '<tr><td>' . $name . '</td><td>' . $result['class']            . '</td><td>' . implode(',', $result['phone']) . '</td></tr>'; } echo '</table>'; 

another approach: make query aggregate phones group , concat_ws

  select name, class, concat_ws(',', phone) phones     students group name  

then you'll have phones given name served in comma-separated format.


as sidenote, consider switching either mysqli or pdo extensions - instead of mysql. that's php documentation says it:

this extension deprecated of php 5.5.0, , not recommended writing new code it removed in future. instead, either mysqli or pdo_mysql extension should used.

that's not simple may sound, it's worth efforts, prepared statements alone.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -