[SOLVED] What function is..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

[SOLVED] What function is..

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post 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

Code: Select all

What
is
your
name?
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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'
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Code: Select all

print_r($array);
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?
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
}
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post 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
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

Code: Select all

$list_friends = explode(',', $f);  

        if (in_array('$name', $list_f)) { }
that does not work.. any ideas?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

awsome it works, thanks for putting up with me

- Anthony
Post Reply