Page 1 of 1
[SOLVED] What function is..
Posted: Fri Apr 08, 2005 7:15 pm
by pinehead18
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
Posted: Fri Apr 08, 2005 7:19 pm
by Chris Corbyn
Posted: Fri Apr 08, 2005 8:32 pm
by cbrian
Example:
Code: Select all
$a = "What is your name?"
$seperator = ' ';
$result = explode($seperator,$a);
$i = 0;
while($result[$i]) {
echo $result[$i] . "<br>";
$i++;
}
This would return
Posted: Sat Apr 09, 2005 11:27 am
by pinehead18
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
Posted: Sat Apr 09, 2005 11:41 am
by Chris Corbyn
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'
Posted: Sat Apr 09, 2005 12:14 pm
by pinehead18
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
Posted: Sat Apr 09, 2005 12:40 pm
by pinehead18
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;
Ok, so you can see what i'm attempting to do..
Thank you
anthony
Posted: Sat Apr 09, 2005 1:35 pm
by Chris Corbyn
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):
Code: Select all
$list_f = explode($f);
for ($i=0; $i<count($list_f); $i++) {
echo $list_f[$i] . '<br />';
}
Have I misunderstood you?
Posted: Sat Apr 09, 2005 2:53 pm
by pinehead18
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..
Posted: Sat Apr 09, 2005 2:59 pm
by Chris Corbyn
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..
???? 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).
Code: Select all
$string = "one,two,pinehead";
$somearray = array():
if (strstr('pinehead', $string)) {
array_push('pinehead', $somearray);
} else {
$somearray = explode(',' $string);
}
Posted: Sat Apr 09, 2005 3:44 pm
by SystemWisdom
I think he just wants to search the array you exploded.. something like:
Code: Select all
if( in_array( 'pinhead', $list_f ) )
{
// do stuff..
}
Reference for in_array() function at
http://php.net/in_array
Posted: Sat Apr 09, 2005 8:08 pm
by pinehead18
Code: Select all
$list_friends = explode(',', $f);
if (in_array('$name', $list_f)) { }
that does not work.. any ideas?
Posted: Sat Apr 09, 2005 8:20 pm
by Chris Corbyn
pinehead18 wrote:Code: Select all
$list_friends = explode(',', $f);
if (in_array('$name', $list_f)) { }
that does not work.. any ideas?
You're using single quotes... variables wont work inside single quotes.
Posted: Sat Apr 09, 2005 9:36 pm
by pinehead18
awsome it works, thanks for putting up with me
- Anthony