0

I have a query which concat the row value.

 GROUP_CONCAT(DISTINCT CONCAT(id, ' - ', name, ' - ', status) SEPARATOR ', ') AS id_name_status

I want to split it as id, name and status when in HTML. Sample code is as following

 @foreach($results as $r)
 <?php
 $id_name_status = explode(',',  $r->id_name_status);
 foreach ($id_name_status as $id) {
 // how can i split the string into 3 value and echo it?
 }
 ?>
 @endforeach
2
  • 4
    Why are you combining them if you're just going to split them? Commented Dec 13, 2017 at 15:00
  • 1
    So DONT concat them in the query, or leave the concat in the query BUT ADD the 3 seperate fields as well to your SELECT Commented Dec 13, 2017 at 15:03

1 Answer 1

2

You are concatenating the strings with - so you have to explode them with that character as well.

$id_name_status = explode(' - ',  $r->id_name_status);

Update:
You can also use the php list function to store the values directly into variables.

list($id, $name, $status) = explode(' - ',  $r->id_name_status);
Sign up to request clarification or add additional context in comments.

3 Comments

Probably should be explode(' - ', $r->id_name_status); if you look closely
i want store in list() instead of using explode to loop through the value?
You can do that, it's just a small update. I added an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.