[SOLVED] What function is..
Moderator: General Moderators
-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
[SOLVED] What function is..
Hey, what function takes a list of contents from a frield.. seperated by commas and then displays them by the charecter?
Like if i have hi,you,there
I want to pull that and seperate the words by ,.
Thanks
Anthony
Like if i have hi,you,there
I want to pull that and seperate the words by ,.
Thanks
Anthony
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Example:
This would return
Code: Select all
$a = "What is your name?"
$seperator = ' ';
$result = explode($seperator,$a);
$i = 0;
while($result[$i]) {
echo $result[$i] . "<br>";
$i++;
}Code: Select all
What
is
your
name?-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
Thank you, one more question for ya.. What function would i use to add something to that then put it all back into the field in the table. I am sure it is an array, am i right?
Example of what i want to do.
Pull the contents from the db (or add it for the first time) so if 1,2,3 are in i want to pull it out and add 4, so then the contents is 1,2,3,4.
Thank you for your time and help
Anthony
Example of what i want to do.
Pull the contents from the db (or add it for the first time) so if 1,2,3 are in i want to pull it out and add 4, so then the contents is 1,2,3,4.
Thank you for your time and help
Anthony
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
$field = "1, 2, 3"; //Original data
$somearray = explode(', ', $field); //Break up into Array ('1', '2', '3')
array_push($somearray, '4'); //Add 4 to give Array ('1', '2', '3', '4')
$newfield = implode(', ', $somearray); //Turn back to string '1, 2, 3, 4'-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
Thank you all for your help, and i've done this before long time go so it is frusterating me i can't figure it out.
When i explode the contents i want to list them out.
so i have..
$list_friends = explode(',', $friends);
The question is how do i well basically give each content inside of the array its own var?
Thanks
Anthony
When i explode the contents i want to list them out.
so i have..
$list_friends = explode(',', $friends);
The question is how do i well basically give each content inside of the array its own var?
Thanks
Anthony
-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
Code: Select all
$sql = "SELECT fFROM users WHERE user='$name'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$f = $row['f'];
$list_f = explode(',', $f);
$i = 1;
while ($i <= 7) {
echo $list_f[];
$i++;
endwhile;Thank you
anthony
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I don't really see what you're lokking to do. Each elemt in an array is it's own var, it's more manageable than exporting them as new variables.
Or if you want your loop method (but why):
Have I misunderstood you?
Code: Select all
print_r($array);Code: Select all
$list_f = explode($f);
for ($i=0; $i<count($list_f); $i++) {
echo $list_f[$i] . '<br />';
}-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
???? Not sure what you mean. You mean if "pinehead" is part of the string, then ignore the rest of the string? There are some string functions that will help (for a more complex search than this you'll need a regexp, in which case ask me).pinehead18 wrote:Sorry to bother you one last time..
How would i say...
if the word pinehead is in the db frield. LIke if one,two,pinehead and if pinehead is in the field then..
i don't know how to make it read just that one part..
Code: Select all
$string = "one,two,pinehead";
$somearray = array():
if (strstr('pinehead', $string)) {
array_push('pinehead', $somearray);
} else {
$somearray = explode(',' $string);
}- SystemWisdom
- Forum Commoner
- Posts: 69
- Joined: Sat Mar 26, 2005 5:54 pm
- Location: A Canadian South of the 49th Parallel
I think he just wants to search the array you exploded.. something like:
Reference for in_array() function at http://php.net/in_array
Code: Select all
if( in_array( 'pinhead', $list_f ) )
{
// do stuff..
}-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
Code: Select all
$list_friends = explode(',', $f);
if (in_array('$name', $list_f)) { }- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
You're using single quotes... variables wont work inside single quotes.pinehead18 wrote:that does not work.. any ideas?Code: Select all
$list_friends = explode(',', $f); if (in_array('$name', $list_f)) { }
-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm