Jump to content


Photo

splitting variable content where a ',' exists


  • Please log in to reply
6 replies to this topic

#1 essjay_d12

essjay_d12
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 25 March 2006 - 03:48 PM

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 = english
subject2 = maths
subject3 = 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.

Thanks

D

#2 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 25 March 2006 - 04:48 PM

<?
$str = 'english, maths, science';
$arr = explode(',', $str);
foreach ($arr as $subject)
{
echo $subject.'<br />';
}
?>

Personal site: NewEnigma  |  Java and JSF Discussion

#3 essjay_d12

essjay_d12
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 25 March 2006 - 05:06 PM

is there anyway that you can make the new variables unique so they can be used at a later time?

so like $subject1 and $subject2

possibly.....
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?

Thanks

D



#4 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 25 March 2006 - 05:17 PM

In a sense they are already unique. Using the above example but altering it slightly...

<?
$str = 'english, maths, science';
$subjects = explode(',', $str);
echo $subject[0] .'<br />';
echo $subject[1] .'<br />';
echo $subject[2] .'<br />';
}
?>


would output

english
maths
science

clear?

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#5 essjay_d12

essjay_d12
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 25 March 2006 - 05:59 PM

yeah cool, thanks

I 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 query

where subject= '$subject'

but with the ',' in it it would not work

#6 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 25 March 2006 - 06:03 PM

$num_in_array = count($arr);

Personal site: NewEnigma  |  Java and JSF Discussion

#7 essjay_d12

essjay_d12
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 25 March 2006 - 06:32 PM

thanks

is there also a way to directly change , for % in the variable $subject?

(leaving it as one variable only)

thanks
d




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users