0

Hi,

This has been troubling me for the last hour and I cant think of a way around it.

My SQL table looks like this:

Name, Cost
Matt, $5.00
Carl, $4.50
Tom, $6.00
Tom, $7.50

I need to make a query so that it puts anything with the same Name on 1 line like this:
<td>Matt, $5.00</td>
<td>Carl, $4.50</td>
<td>Tom, $6.00, $7.50</td>

My current SQL is below and it just displays 4 individual rows as expected:

$sql="name, price from table1";
echo "<tr><td>Name</td><td>>Price</td></tr>";
if ($result=mysql_query($sql)) {
while ($row=mysql_fetch_assoc($result)) {
echo "<td>".$row['name']."</td>";
echo "<td>".$row['price']."</td></tr>";}}
4
Contributors
5
Replies
8
Views
4 Years
Discussion Span
Last Post by mathieu89
0

remove line 6

and place

 echo "<td colspan=2>".$row['name'].",".$row['price']."</td>"

instead of what you have written at line 5

check it once by making those changes

let me know the status and make this thread solved if you get the exact answer

Edited by radhakrishna.p: giving clear view on his problem

0

You mean change this

$sql="name, price from table1";
echo "<tr><td>Name</td><td>>Price</td></tr>";
if ($result=mysql_query($sql)) {
while ($row=mysql_fetch_assoc($result)) {
echo "<td>".$row['name']."</td>";
echo "<td>".$row['price']."</td></tr>";}}

to this, right ?

$sql="select name, price from table1";
echo "<tr><td>Name</td><td>>Price</td></tr>";
if ($result=mysql_query($sql)) {

while ($row=mysql_fetch_assoc($result)) {
echo "<tr><td colspan=2>".$row['name'].",".$row['price']."</td></tr>";
}

}

Edited by veedeoo: info added

1
$sql="name, price from table1";
echo "<tr><td>Name</td><td>>Price</td></tr>";
if ($result=mysql_query($sql)) {
while ($row=mysql_fetch_assoc($result)) {
echo "<td>".$row['name']."</td>";
echo "<td>".$row['price']."</td></tr>";}}

Try this:

Dunno the if condition what it does but anyway:

while ($row=mysql_fetch_assoc($result)) {
     if($name == $row['name']){
         echo ", ".$row['price'];
     }else{
        echo "<br/>";
        echo $row['name'] . ", ";
        echo $row['price'];
    }
    $name = $row['name'];
}

Good Luck

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.