0

I was trying something but I need to explode it twice because I store 2 variables in a string. Anyways, I used a while loop but I don't understand, I use cid++, but it does not appear to increase. ANyways, here's the code.

              $cid = 0;
              while($row = mysql_fetch_array($result)){
                $comment = explode("-", $row['comments']);
                $madeby = explode("///", $comment[$cid]);
                $cid++;
                echo $madeby[1];
              }
James L.
  • 131
  • 2
  • 3
  • 7
  • 4
    See [Is storing a delimited list in a database column really that bad?](http://stackoverflow.com/a/3653574) – eggyal Aug 14 '13 at 20:01
  • 1
    ...and [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – brbcoding Aug 14 '13 at 20:03
  • 1
    Both comments above are right. But to answer the question, you're checking `$comment[0]` on the first row, then `$comment[1]` in the second row, and so on. I don't think that's really the intention. – bfavaretto Aug 14 '13 at 20:04
  • I think you need `[]` for `$madeby`. – emco Aug 14 '13 at 20:07

3 Answers3

2

Since you're setting a var and incrementing you can use it as a key for both arrays.

$cid = 0;
while($row = mysql_fetch_array($result)){
   $comment[$cid] = explode("-", $row['comments']);
   $madeby[$cid] = explode("///", $comment[$cid]);
   echo $madeby[$cid][1];
   $cid++;
}

Then you can do this:

foreach($madeby as $key=>$tempArr){
   echo '"'.$madeby[$key][0].'" by "'.$madeby[$key][1].'"<br>'; 
}

To see the whole array:

print_r($madeby);
AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
0

try this:

while($row = mysql_fetch_array($result)){

    $comment = explode("-", $row['comments']);
    $cnt = count($comment);

    $madeby = array();
    for($cid=0; $cid<$cnt; $cid++){
        $madeby[] = explode("///", $comment[$cid]);
    }    
    print_r($madeby);
}
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
0

try this

while($row = mysql_fetch_array($result))
{
 $comment = explode("-", $row['comments']);
 foreach($comment as $each_comment)
 {
  $madeby = explode("///", $each_comment);
  echo $madeby[1];
 }
}

or if you really want to use cid then

while($row = mysql_fetch_array($result))
    {
     $comment = explode("-", $row['comments']);
     for($cid=0;$cid<count($comment);$cid++)
     {
      $madeby = explode("///", $comment[$cid]);
      echo $madeby[1];
     }
    }
coderkane
  • 488
  • 4
  • 14