justin7410 Posted May 5, 2013 Share Posted May 5, 2013 Hey guys, so i am retrieving data from my database and have called on a certain variable.. this variable is called $personality_traits and is containing a string with all the tags (honest,go getter,lazy,humble,etc..) each persons profile is showing these traits as to what is being stored per their own ID. so one individual profile would show profile 1 Traits : Happy, Organized, Honest. Brave profile 2 Traits : Quiet, Conservative, Shy, Smart profile 3 Traits : Honest, Smart, Go getter, Now my issue: i am now trying to break this variables string into parts , so that i can echo the tags individually , and create hrefs from them accordingly. When i use the explode() this works for me and separates each tag into an index inside an array while ($row = mysql_fetch_assoc($links)) { extract($row); $user_traits // data from field `user_traits` FROM `database` $traits = explode( ',' , $user_traits ); echo $user_traits; print_r($traits); Which results in; ( for 1 profile example) positive,warm,fun Array ( [0] => positive [1] => warm [2] => fun ) My Question; How can i now list all the indices in this array for each user , as if i were inputting $user_traits ? i cant just type echo $traits[0]; echo $traits[1]; echo $traits[2]; since each profile has a different amount of variables to be defined, some have only 2 traits, while others might have 4,5 or even 6. I am assuming there is a way to do this using a mysql command , but is there anyway PHP wise to solve this problem ? any ideas or suggestions would be much appreciated, this is a feature i definitely want to have showing for my users. Quote Link to comment https://forums.phpfreaks.com/topic/277683-how-to-list-all-the-items-dynamically-in-an-explode-array/ Share on other sites More sharing options...
Jessica Posted May 5, 2013 Share Posted May 5, 2013 (edited) http://php.net/foreach Edited May 5, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/277683-how-to-list-all-the-items-dynamically-in-an-explode-array/#findComment-1428502 Share on other sites More sharing options...
Solution justin7410 Posted May 5, 2013 Author Solution Share Posted May 5, 2013 http://php.net/foreach Such a simple yet helpful answer . Thank you Jessica, i just did not think this quite through enough for the answer was simply: while ($row = mysql_fetch_assoc($links)) { extract($row); $user_traits // data from field `user_traits` FROM `database` $traits = explode( ',' , $user_traits ); echo $user_traits; print_r($traits); foreach ($traits as $trait){ echo '<a href="#">' .$trait .'</a>'; } Quote Link to comment https://forums.phpfreaks.com/topic/277683-how-to-list-all-the-items-dynamically-in-an-explode-array/#findComment-1428503 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.