Hi all,

I am retrieving information from a database and need to have that information put onto a new line (<br>) depending on the location of the comma which seperates the different areas of the information.
Here is what is being retrieved

2.25 L 75 hp (56 kW) I4 petrol (1983–1985), 2.25 L 62 hp (46 kW) I4 diesel (1983), 2.5 L 68 hp (51 kW) I4 diesel (1984–1993), 2.5 L 83 hp (62 kW) I4 petrol (1985–1993), 2.5 L 85 hp (63 kW) I4 Turbodiesel (1986–1990), 3.5 L 113 hp (84 kW) V8 (1983–1986), 3.5 L 134 hp (100 kW) V8 (1986–1993)

Now, whenever a comma appears above, I need that to indicate the beginning of a new line.
So, any suggestions on how I do this? Please don't post code for me without an explanation of what is happening as then I won't be able to learn!!

Here is the code that is getting the information from the database

while($row=mysql_fetch_array($result))
{

echo "<tr>";
echo "</td><td valign=\"top\">";
echo "<span class='vehicle_table_big'>$row[manufacturer] $row[make] $row[model] $row[variant]</span><span class='vehicle_table_normal'>from $row[production_date_from] to $row[production_date_to]</span><br>";
echo "<span class='vehicle_table_cell_header'>Body Style </span>";
echo "<span class='vehicle_table_normal'>$row[body_style]</span><br>";
echo "<span class='vehicle_table_cell_header'>Engine Types </span>";
echo "<span class='vehicle_table_normal'>$row[engine]</span><br>";
echo "<span class='vehicle_table_cell_header'>Transmission </span>";
echo "<span class='vehicle_table_normal'>$row[transmission]</span><br>";
echo "<span class='vehicle_table_cell_header'>Wheelbase </span>";
echo "<span class='vehicle_table_normal'>$row[wheelbase]</span><br>";
echo "<span class='vehicle_table_cell_header'>Length </span>";
echo "<span class='vehicle_table_normal'>$row[length] </span>";
echo "<span class='vehicle_table_cell_header'>Width </span>";
echo "<span class='vehicle_table_normal'>$row[width] </span>";
echo "<span class='vehicle_table_cell_header'>Height </span>";
echo "<span class='vehicle_table_normal'>$row[height]</span><br>";
echo "<span class='vehicle_table_cell_header'>Curb Weight </span>";
echo "<span class='vehicle_table_normal'>$row[curb_weight]</span>";
echo "</td><td align=\"center\" valign=\"middle\">";
echo "<img border=\"1\" src=$row[photo] title=\"$row[manufacturer] $row[make] $row[model] $row[variant]\"></td>";}
echo "</tr>";
echo "</table>";
mysql_close($connect);

Dani AI

Generated

's pointer to PHP string functions was the right direction and, as confirmed, swapping comma separators for line breaks fixes the immediate display problem. The following offers a safer, cleaner approach, explains what each step does, and suggests a couple of improvements for maintainability and security.

function split_on_comma($s) {
    $parts = array_filter(array_map('trim', explode(',', (string)$s)), 'strlen');
    $parts = array_map(function($p){
        return htmlspecialchars($p, ENT_QUOTES, 'UTF-8');
    }, $parts);
    return implode('<br>', $parts);
}

This does four things: (1) casts to string to avoid warnings, (2) splits on commas, (3) trims whitespace and removes empty entries (handles trailing commas), and (4) escapes each piece to prevent XSS before joining with <br>. If the items are really list-like, prefer semantic markup: '<ul><li>' . implode('</li><li>', $parts) . '</li></ul>' so CSS can style the list consistently.

A couple of practical notes tied to the original thread: avoid echoing raw DB values into attributes (quote and run htmlspecialchars on src and alt/title), and move away from deprecated mysql_* calls toward mysqli or PDO with prepared statements so the code is compatible with modern PHP. For long-term maintainability consider normalizing the data: store each engine/type as its own row (or as JSON in a single typed column) instead of a comma-separated string. That makes queries, filtering and updates far easier and removes brittle parsing.

Troubleshooting checklist: trim input on insert, collapse repeated commas, watch for commas inside data (if those exist use a different delimiter or normalized rows), and always escape output. These small changes keep display code simple and the data reliable.

Recommended Answers

All 3 Replies

Hi PhilEaton,

Thank you for pointing me in the right direction! It is now working properly. As an added bonus, it forced me to clean-out the data in the database! So, you have effectively solved two problems I had, one of which I probably would not have realised in time if at all! Now I know why I come to Daniweb for help.

Here is the code that is finally working for me. It is probably not the shortest, most effective way of doing things, but for now it is working and doing the job.

while($row=mysql_fetch_array($result))
{

$bodystyle=$row[body_style];
$enginetype=$row[engine];
$transmission=$row[transmission];
$wheelbase=$row[wheelbase];
$length=$row[length];
$width=$row[width];
$height=$row[height];
$curbweight=$row[curb_weight];

echo "<tr>";
echo "</td><td valign=\"top\">";
echo "<span class='vehicle_table_big'>$row[manufacturer] $row[make] $row[model] $row[variant]</span><span class='vehicle_table_normal'>from $row[production_date_from] to $row[production_date_to]</span><br>";
echo "<span class='vehicle_table_cell_header'>Body Style<br></span>";
echo "<span class='vehicle_table_normal'>";$str_bodystyle=str_replace("," ,"<br>",$bodystyle);echo $str_bodystyle;
echo "<br></span><br>";
echo "<span class='vehicle_table_cell_header'>Engine Types<br></span>";
echo "<span class='vehicle_table_normal'>";$str_enginetype=str_replace("," ,"<br>",$enginetype);echo $str_enginetype;
echo "<br></span><br>";
echo "<span class='vehicle_table_cell_header'>Transmission<br></span>";
echo "<span class='vehicle_table_normal'>";$str_transmission=str_replace("," ,"<br>",$transmission);echo $str_transmission;
echo "<br></span><br>";
echo "<span class='vehicle_table_cell_header'>Wheelbase<br></span>";
echo "<span class='vehicle_table_normal'>";$str_wheelbase=str_replace("," ,"<br>",$wheelbase);echo $str_wheelbase;
echo "<br></span><br>";
echo "<span class='vehicle_table_cell_header'>Length<br></span>";
echo "<span class='vehicle_table_normal'>";$str_length=str_replace("," ,"<br>",$length);echo $str_length;
echo "<br></span><br>";
echo "<span class='vehicle_table_cell_header'>Width<br></span>";
echo "<span class='vehicle_table_normal'>";$str_width=str_replace("," ,"<br>",$width);echo $str_width;
echo "<br></span><br>";
echo "<span class='vehicle_table_cell_header'>Height<br></span>";
echo "<span class='vehicle_table_normal'>";$str_height=str_replace("," ,"<br>",$height);echo $str_height;
echo "<br></span><br>";
echo "<span class='vehicle_table_cell_header'>Curb Weight<br></span>";
echo "<span class='vehicle_table_normal'>";$str_curbweight=str_replace("," ,"<br>",$curbweight);echo $str_curbweight;
echo "</span>";
echo "</td><td align=\"center\" valign=\"middle\">";
echo "<img border=\"1\" src=$row[photo] title=\"$row[manufacturer] $row[make] $row[model] $row[variant]\"></td>";

$bodystyle="";
$enginetype="";
$transmission="";
$wheelbase="";
$length="";
$width="";
$height="";
$curbweight="";

}

echo "</tr>";
echo "</table>";

mysql_close($connect);

Let me know if you all agree with the code above. If you have any suggestions, I would be really interested to hear them, but please also explain why you are making those suggestions so I can learn a bit more.

Carter

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.