0

I have stored comma separated value in database. Now I want to display list of that value. Currently I am trying to print "bni_member_export_to". It contain string like usa,uk,india. I need as list.

while($row = mysqli_fetch_array($result))  
{  
    $output .= '  
    <br>
    <hr>
    <br>
        <div class="row">
            <div class="col-md-6">
                 <h5>Personal Information</h5>
                      <li>Name : '.$row["bni_member_name"].'</li>
                      <li>Mobile: '.$row["bni_member_mobile"].'</li>
                      <li>Email: '.$row["bni_member_email"].'</li>
            </div>
            <div class="col-md-6">
                 <h5>Business Information</h5>
                      <li>Chapter: '.$row["bni_chapter_id"].'</li>
                      <li>Category: '.$row["bni_category_id"].'</li>
            </div>
       </div>
       <br>
       <hr>

       <br>
        <div class="row">
            <div class="col-md-12">
                 <h6>Description</h6>
                 <h7>'.$row["bni_member_bio"].'</h7>
            </div>
        </div>
        <br>
        <hr>
        <br>
        <div class="row">


            <div class="col-md-4">
            <li>Export to</li>

                 <li>'.$row["bni_member_export_to"].'</li>     
            </div>
            <div class="col-md-4">
                  <li>Import From</li>
                  <li>'.$row["bni_member_import_from"].'</li>     
            </div>
            <div class="col-md-4">
                  <li>Want to Connect To</li>
                  <li>'.$row["bni_member_want_to_connect_to"].'</li>     
            </div>
        </div>

I have php code for list all string. But I doesn't work if I put it into

    <li>Export to</li>
    <li>'.$row["bni_member_export_to"].'</li>     
</div>

for print list

$exp = $row["bni_member_export_to"];
$expl = explode(',', $exp);
foreach($expl as $my_Array){
   echo $my_Array.'<br>';  
}
4
  • You can not put a foreach loop “inside” of a text literal. You need to end your string assignment first, then do the loop and append to $output inside of it, and then append the rest of the HTML code to $output after the loop. Commented Jul 26, 2019 at 12:04
  • 2
    Seems like a database design error. Store tht data properly and processing it will be easier Commented Jul 26, 2019 at 12:11
  • You need like usa<br>india<br> OR like <li>usa</li><li>india</li> Commented Jul 26, 2019 at 12:18
  • You could also try $exp = str_replace(",","<br>",$row["bni_member_export_to"]); echo $exp; Commented Jul 26, 2019 at 12:52

3 Answers 3

2

You will have to stop the string concatenation, and start the PHP processor to generate the explode() and a foreach loop to generate the <li> items

$output .= '  

    . . .

    <div class="col-md-4">
        <ul>                                <!-- added the missing <ul> tag -->
            <li>Want to Connect To</li>';    // not sure this <li> belongs here
<?php 
    $items = explode(',', $row["bni_member_want_to_connect_to"])
    foreach ($items as $item) {
        $output .= "<li>$item</li>";
    }
?>

$output .= '</ul></div>';
Sign up to request clarification or add additional context in comments.

Comments

0

You have some messed html (missing ul, ...)

But if you need html list from comma separated string ("usa,india"), you can explode your string into the array and then implode it back separated by </li><li>

'<ul><li>'.implode('</li><li>', explode(',', $row["bni_member_export_to"])).'<li></ul>'

or you can just replace comma with </li><li>

'<ul><li>'.str_replace(',', '</li><li>', $row["bni_member_export_to"])).'<li></ul>'

But I think your database is badly designed.

Comments

0

This should work

$exp = $row["bni_member_export_to"];
$expl = explode(',', $exp);
$output .= '<ul>';
foreach($expl as $my_Array){
   $output .= '<li>'.$my_Array.'</li>';  
}
$output .= '</ul>';

echo $output;

Comments

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.