Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/5774-splitting-variable-content-where-a-exists/
Share on other sites

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

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 output

english
maths
science

clear?

Lite...
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
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.

Guest
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.