Login    Forum    Search    FAQ   Blog

Board index » Server Side Development » PHP




 Page 1 of 1 [ 2 posts ] 



Author Message
 Post subject: array rebuildin and the assignment in to the another array
 Post Posted: Thu Nov 19, 2009 7:07 am 
Offline

Joined: Sat Feb 21, 2009 1:04 pm
Posts: 47
The below code that doesn't seem 2 work. If some of you could
point out where I was goin wrong I would much appreciate that.
My guess is I m goin all wrong in rebuildin da array. Also, da last
bit $data['comment_row'] = $amended_comment;
could I assign an array in 2 an indexed array just like this?

Here comes ma 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;


Top 
 Post subject: Re: array rebuildin and the assignment in to the another array
 Post Posted: Thu Nov 19, 2009 7:07 am 
Offline

Joined: Sat Feb 21, 2009 11:06 am
Posts: 1794
Here was ma problem:

Code:
foreach($comments[$i] as $key)


This would actually just traverse da vals in da $comments array, not it
keys.

Try dis instead, then it must work:

Code:
foreach($comments[$i] as $key => $value)


Top 
Display posts from previous:  Sort by  
 
 Page 1 of 1 [ 2 posts ] 




Board index » Server Side Development » PHP


Who is online

Users browsing this forum: No registered users and 1 guest

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron