-2

Please I am having an issue, which is running a foreach in a while loop, I am only getting sometimes the first or last result in that column.

while ($girl = mysqli_fetch_array($obo)) {
    $ch_desc = $girl['desc'];
    $ch_tags = $girl['tags'];  //eg boy,ope,not,good

    $laye = explode (",", $ch_tags);
        foreach ($laye as $fb){
            $yepa = "<span> #$fb </span>";  
        }

    <div class="panel-heading">
        <?php echo $yepa ?> 
    </div>
}
chiz
  • 71
  • 1
  • 1
  • 8
  • turn on error reporting and use COMMENT tags for comments either // or /*... */ like ` // eg boy,ope,not,good ` .. you are not echo'ing $yepa inside the loop, so you only get that last value – Duane Lortie Mar 26 '17 at 22:48
  • also please indent, I cant be bothered to even help if you wont present your question nicely – Halfpint Mar 26 '17 at 22:49
  • 1
    btw; you really should normalize your db. Have a read [Is storing a delimited list in a database column really that bad?](http://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) – Funk Forty Niner Mar 26 '17 at 23:20
  • @Fred-ii- Thanks read it but where i am using bootstrap input tags prevent that duplicate entry automatically would look into it more still learning data loop please could you help me with this issue – chiz Mar 26 '17 at 23:27

1 Answers1

0

You are overwriting the value of the variable in each loop. You need to initialize it as an empty string and concatenate the tag in each loop.

$tags = explode (",", $ch_tags);
$tagList = "";
foreach ($tags as $tag){
    $tagList .= "<span> #$tag </span>";
}
  • Thanks for the answer but now i am getting the same result two times – chiz Mar 26 '17 at 23:12
  • @chiz Made a mistake. I edited my answer. I was using the same name for the tag list and for the foreach variable. – Benjamin Diaz Mar 26 '17 at 23:18
  • Thank you getting closer it sometimes creates two or three records of the same thing but it ECHO everything out but this time two or three times likes this is one column bum bum,tell,oshe,na and the result is #bum bum #bum bum #tell #bum bum #tell #oshe #bum bum #tell #oshe #na – chiz Mar 26 '17 at 23:23