robbo5899
robbo5899

Reputation: 99

Adding commas to database requests

I have this as part of a line of code in my php file:

($install["eco_plus"] ? "Eco+ " : "") . ($install["satnav"] ? str_replace('_', ' ', $install["satnav"]) . " " : "") . ($install["ttConnect"] ? "TTConnect " : "") . ($install["fms"] ? "FMS " : "") . ($install["remote_link"] ? "Remote Link " : "") . ($install["remote_logbook"] ? "Remote Logbook " : "")

It outputs this into my table:

Eco+ Go 530 TTConnect FMS Remote Link Remote Logbook

I want it to output something like the following:

Eco+, Go 530, TTConnect, FMS, Remote Link, Remote Logbook

I'm unsure of how to do this without having it output something like this:

Eco+, Go 530, TTConnect, FMS, Remote Link, Remote Logbook,

or When its not displaying all of the data and only part like this:

Eco+, Go 530,

I would appreciate any help you can offer or link me. Thanks

Upvotes: 0

Views: 41

Answers (2)

Barmar
Barmar

Reputation: 782105

Put each item into an array, and use implode():

$array = array();
if ($install["eco_plus"]) {
    $array[] = "Eco+";
}
if ($install['satnav']) {
    $array[] = str_replace('_', ' ', $install["satnav"]);
}
if ($install['ttConnect']) {
    $array[] = 'TTConnect';
}
if ($install['fms']) {
    $array[] = "FMS";
}
...
$features = implode(', ', $array);

Upvotes: 2

kiks73
kiks73

Reputation: 3768

You can add a comma to all your substrings and remove the last one with rtrim function :

rtrim(($install["eco_plus"] ? "Eco+, " : "") . ($install["satnav"] ? str_replace('_', ' ', $install["satnav"]) . ", " : "") . ($install["ttConnect"] ? "TTConnect, " : "") . ($install["fms"] ? "FMS, " : "") . ($install["remote_link"] ? "Remote Link, " : "") . ($install["remote_logbook"] ? "Remote Logbook " : ""),",")

Upvotes: 0

Related Questions