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>";}}

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

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>";
}

}
$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

yes veedeoo try with that

Thanks for your help everyone! Its works great

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.