essjay_d12 Posted March 25, 2006 Share Posted March 25, 2006 I'm extracting data from a mySQL database table, school. There is a VARCHAR field called subject. Within this field it could be like the following......english, maths, science(all seperated by a comma)I need to seperate this extracted data into variables for each subject so .....subject1 = englishsubject2 = mathssubject3 = science Different rows may have different numbers of subjects within them (meaning it is not necessarily 3) and they are always seperated by a comma.ThanksD Link to comment https://forums.phpfreaks.com/topic/5774-splitting-variable-content-where-a-exists/ Share on other sites More sharing options...
annihilate Posted March 25, 2006 Share Posted March 25, 2006 [code]<?$str = 'english, maths, science';$arr = explode(',', $str);foreach ($arr as $subject){echo $subject.'<br />';}?>[/code] Link to comment https://forums.phpfreaks.com/topic/5774-splitting-variable-content-where-a-exists/#findComment-20582 Share on other sites More sharing options...
essjay_d12 Posted March 25, 2006 Author Share Posted March 25, 2006 is there anyway that you can make the new variables unique so they can be used at a later time?so like $subject1 and $subject2possibly.....how would i create a while loop to go with the code that increments 'a' by 1 each time then perhaps something like $subject + a...would this work?ThanksD Link to comment https://forums.phpfreaks.com/topic/5774-splitting-variable-content-where-a-exists/#findComment-20595 Share on other sites More sharing options...
litebearer Posted March 25, 2006 Share Posted March 25, 2006 In a sense they are already unique. Using the above example but altering it slightly...[code]<?$str = 'english, maths, science';$subjects = explode(',', $str);echo $subject[0] .'<br />';echo $subject[1] .'<br />';echo $subject[2] .'<br />';}?>[/code]would outputenglishmathsscienceclear?Lite... Link to comment https://forums.phpfreaks.com/topic/5774-splitting-variable-content-where-a-exists/#findComment-20601 Share on other sites More sharing options...
essjay_d12 Posted March 25, 2006 Author Share Posted March 25, 2006 yeah cool, thanksI didnt realise that the new variable was an array.Cheers:-)Actually how would i know how many values are in the array so if i did want to use them in a for loop later i could set the for loop to the correct amount.Or is it possible to simply replace the ',' in the field to a %its that the fied needs to be put into a sql search querywhere subject= '$subject' but with the ',' in it it would not work Link to comment https://forums.phpfreaks.com/topic/5774-splitting-variable-content-where-a-exists/#findComment-20620 Share on other sites More sharing options...
annihilate Posted March 25, 2006 Share Posted March 25, 2006 [code]$num_in_array = count($arr);[/code] Link to comment https://forums.phpfreaks.com/topic/5774-splitting-variable-content-where-a-exists/#findComment-20627 Share on other sites More sharing options...
essjay_d12 Posted March 25, 2006 Author Share Posted March 25, 2006 thanksis there also a way to directly change , for % in the variable $subject?(leaving it as one variable only)thanksd Link to comment https://forums.phpfreaks.com/topic/5774-splitting-variable-content-where-a-exists/#findComment-20649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.