Welcome Guest, Not a member yet? Register   Sign In
Array rebuilding and assigning it to be used in view
#1

[eluser]oliur[/eluser]
The following code doesn't seem to work. If someone can point out where I am going wrong I'd much appreciate that.
My guess is I am going all wrong in rebuilding the array. Also, the last bit $data['comment_row'] = $amended_comment;
Can I assign an array into an indexed array like this?

Here is my code.

Code:
// take a string and look for expressions, replace expression with corresponding emoticons
function addSmiley($des){
  $des = (string)$des;
  $expression = array(":P" => "tongue_smile.gif", ":lol:" => "lol.gif");
  for($i=0;$i<count($expression);$i++){
        foreach($expression as $key=>$value)
        $des = str_replace($key,$value,$des);
  }
  return $des;  
}

// this could be either an array or database results set
function getComment(){
    $comments_row[] = array("id" => 0, "user" => "Oliur", "comment" => "This is a comment :P");
    $comments_row[] = array("id" =>1, "user"=>"James","comment" =>"This is another comment :lol:");
    
    return $comments_row;
}

$comments = getComment();

// replace smiley expression with emoticons
// rebuild the array
for($i=0;$i<count($comments);$i++){
  foreach($comments[$i] as $key){
      if($key == "comment")
          $value = addSmiley($value);
      $amended_comment[] = array($key=>$value);
}
}
// assign the rebuilt array to another array index so it can be used in Codeigniter. In the following example, $comment_row will now contain the rebuilt array and we can loop through this in our view and display the comments with emoticons.

$data['comment_row'] = $amended_comment;




Theme © iAndrew 2016 - Forum software by © MyBB