Jump to content

How to list all the items dynamically in an explode array

Go to solution Solved by justin7410,

Recommended Posts

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)) { 


$user_traits  // data from field `user_traits` FROM `database` 
$traits = explode( ',' , $user_traits ); 
echo $user_traits; 

Which results in; ( for 1 profile example)

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.



Link to comment
Share on other sites

  • Solution




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)) { 

$user_traits // data from field `user_traits` FROM `database` 
$traits = explode( ',' , $user_traits );
echo $user_traits;

foreach ($traits as $trait){
     echo '<a href="#">' .$trait .'</a>';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.