0

i am using form to insert multiple passenger list using implode
form passenger list
view passenger list

In controller

$passname=''.implode("*",$this->input->post("passangernm")).''; 
$passage=''.implode("*",$this->input->post("psgage")).''; 
$passgender=''.implode("*",$this->input->post("psggender")).'';
$passpref=''.implode("*",$this->input->post("psgpref")).'';

In View

<td>".implode(explode('*',$rec->passangerName)," | ")." ".implode(explode('*',$rec->passangerAge)," | ")." ".implode(explode('*',$rec->passangerGender)," | ")."</td>

Result show like

Roshan | Gyan | manish 23 | 23 | 23 male | male | male lower | middle | upper

But I need like this format

Roshan 23 male lower | Gyan 23 male middle | manish 23 male upper|
sauhardnc
  • 1,961
  • 2
  • 6
  • 16
Roshan
  • 1
  • 1
  • _Small Point_ Not everything need to be **EMBOLDENED** It is like shouting at us all – RiggsFolly Jul 02 '20 at 17:06
  • read about [Database Normalization](https://en.wikipedia.org/wiki/Database_normalization), it's considered bad practice to store data as CSV or any other delimited string – Vickel Jul 02 '20 at 19:11

3 Answers3

0

You can first count the total number of values in any one of them then use for loop to show all the data as you want, like so -

$passangerNameArr   = explode('*', $rec->passangerName);
$passangerAgeArr    = explode('*', $rec->passangerAge);
$passangerGenderArr = explode('*', $rec->passangerGender);
$passangerBerthArr  = explode('*', $rec->your_varialbe); // Name not provided in your code(change here)

// All the other value count should be equal to this one ↓↓ ie if 2 passengers then 2 ages and gender
$total = count($passangerNameArr); 

for($i = 0; $i < $total; $i++){

    echo "<td>";
    
    // notice the spaces and double quotes ↓↓ (You can also use &nbsp; for space)
    echo "{$passangerNameArr[$i]} {$passangerAgeArr[$i]} {$passangerGenderArr[$i]} {$passangerBerthArr[$i]}";
    
    echo " | ";
    echo "</td>

}

Also, keep in mind that this is assuming you always have more than one data in your column, so you should first check if the returned variable is an array only then you should apply this code.

Hope this helps you.

sauhardnc
  • 1,961
  • 2
  • 6
  • 16
0

I would definitely recommend not storing passenger details in a delimited format. Each value should be it's own column (ideally on another table that you can join).

You can store it as JSON which is easier to work with, but either way it makes that data difficult to search on.

Based on this scenario and assuming there are values for every field that match up correctly, I think it might make more sense to create an array of strings in the Controller and use that to display the data in the View.

Get the POST data in the Controller:

list($passnames) = $this->input->post("passangernm");
list($passages) = $this->input->post("psgage");
list($passgenders) = $this->input->post("psggender");
list($passprefs) = $this->input->post("psgpref");

Loop through the data in the Controller to build strings:

$passengersArray = [];
foreach($passnames as $key => $name) {
    $passengersArray[] = $name . " " . $passages[$key] . " " . $passgenders[$key] . " " . $passprefs[$key];
}

$passengersArray should now be an array of strings where each array item is a different passenger, so in the View you can implode them with | delimiter:

"<td>" . implode("|", $passengersArray) . "</td>"
Spudly
  • 310
  • 1
  • 2
  • 10
0

You can use array_column like so. This will still work even if the names/ages/genders/preferences don't have the same number of items in them:

$names = ["Roshan", "Gyan", "manish"];
$ages = ["23", "23", "23"];
$genders = ["male", "male", "male"];
$preferences = ["lower", "middle", "upper"];

$data = [$names, $ages, $genders, $preferences];
$max_len = count(max($data));

$combined = [];
for($i=0; $i<$max_len; $i++){
    $combined[] = implode(" ", array_column( $data, $i ) );
}
echo '<td>' . implode(" | ", $combined) . '</td>';

If you can change the way the passenger details are stored in the database, I agree with Spudly that are better ways of storing the data.

Alternative answer

The following also works:

$combined = [];
array_map(function(...$params) use (&$combined){
    $combined[] = implode(" ", $params);
}, $names, $ages, $genders, $preferences);

echo '<td>' . implode(" | ", $combined) . '</td>';
hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
  • Looks rather odd to abuse/misuse `array_map()` as if it was `array_walk()`. I would not code like this in a professional script. – mickmackusa Jul 26 '22 at 04:51