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
JimiH
Forum Commoner
Posts: 92 Joined: Thu Jun 15, 2006 6:10 am
Post
by JimiH » Tue Nov 21, 2006 7:47 am
Hi I have the following Array, $Project_members
Code: Select all
if ( $update_project_members == 'on' ) {
$Project_Members=$_POST["Project_Members_Select"];
for ( $ii = 0 ; $ii < count($Project_Members) ; $ii++ )
{
$choose_branch = $choose_branch . " " . $Project_Members[$ii] ;
}
//$result = "You selected : " . $choose_branch ;
$Project_Members = $choose_branch;
} else {
$Project_Members=$_POST["Project_Members"];
}
I would like seperate this Array and place it in a string seperated by ";"
Example Array (bill , ben)
Would come out "bill;ben;"
Thanks
Geoff
Example
Array
BILL
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Nov 21, 2006 7:51 am
JimiH
Forum Commoner
Posts: 92 Joined: Thu Jun 15, 2006 6:10 am
Post
by JimiH » Tue Nov 21, 2006 8:18 am
Thanks
I've been looking at that function, cant get it to work
If I echo
I get
BILL BENN
How do I put "$choose_branch" into the implode() code?
This is what I have
$result = implode (";",$choose_branch);
echo $result;
Code: Select all
This returns nothing.
Thanks
Geoff
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Nov 21, 2006 8:26 am
what does
Code: Select all
echo '<pre>';
print_r($choose_branch);
echo '</pre>';
$result = implode (";",$choose_branch);
echo $result;
yield?
JimiH
Forum Commoner
Posts: 92 Joined: Thu Jun 15, 2006 6:10 am
Post
by JimiH » Tue Nov 21, 2006 8:41 am
BILL BEN
With
Code: Select all
echo '<pre>';
print_r($choose_branch);
echo '</pre>';
$result = implode (";",$choose_branch);
echo $result;
if I comment the following line the result is the same
Code: Select all
echo '<pre>';
print_r($choose_branch);
echo '</pre>';
//$result = implode (";",$choose_branch);
//echo $result;
??
Thanks
Geoff
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Nov 21, 2006 8:45 am
$choose_branch isn't an array!?
This should work, although it is only a quick fix.
Code: Select all
$names = explode(" ", $choose_branch);
$result = implode (";",$names);
echo $result;
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Nov 21, 2006 8:46 am
Okay I see the error, you are overwriting your variable $choose_branch every iteration.
Code: Select all
$choose_branch = array();
for ( $ii = 0 ; $ii < count($Project_Members) ; $ii++ )
{
$choose_branch[] = $choose_branch . " " . $Project_Members[$ii] ;
}
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Nov 21, 2006 8:48 am
infact, what i think you are trying to acchieve is this
Code: Select all
if ( $update_project_members == 'on' )
{
$Project_Members=$_POST["Project_Members_Select"];
for ( $ii = 0 ; $ii < count($Project_Members) ; $ii++ )
{
$choose_branch[] = $Project_Members[$ii] ;
}
$result = implode(";", $choose_branch);
//$result = "You selected : " . $choose_branch ;
$Project_Members = $choose_branch;
}
else
{
$Project_Members=$_POST["Project_Members"];
}
JimiH
Forum Commoner
Posts: 92 Joined: Thu Jun 15, 2006 6:10 am
Post
by JimiH » Tue Nov 21, 2006 9:10 am
Hi thanks for all your help
The below works fine
Code: Select all
if ( $update_project_members == 'on' ) {
$Project_Members=$_POST["Project_Members_Select"];
for ( $ii = 0 ; $ii < count($Project_Members) ; $ii++ )
{
$choose_branch = $choose_branch . " " . $Project_Members[$ii] ;
}
$result = implode(";",$Project_Members);
$Project_Members = $choose_branch;
} else {
$Project_Members=$_POST["Project_Members"];
}
echo $result;
As you can see I replaced
Code: Select all
$result = implode(";",$choose_branch);
with
Code: Select all
$result = implode(";",$Project_Members);
This worked fine, can someone tell me why?
Thanks
Geoff
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Nov 21, 2006 9:18 am
JimiH wrote:
This worked fine, can someone tell me why?
Becuase $choose_branch wasn't an array
JimiH
Forum Commoner
Posts: 92 Joined: Thu Jun 15, 2006 6:10 am
Post
by JimiH » Tue Nov 21, 2006 4:34 pm
Thanks
I understand now
Geoff