Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with implode()

Status
Not open for further replies.

DeepBlerg

Technical User
Jan 13, 2001
224
0
0
AU
Hi,

I have a form on a page that has 5 text fields (tfield0, tfield1, tfield2 etc etc) which once the form is processed, it creates an array with the 5 values from the text fields and puts into database.

Here's some of the code:

$thedata = array();
$tcount = 5
$i = 0;
while ($tcount != 0) {
$thedata[] = $tfield[$i];
$i++;
$tcount--;
}
$thevar = implode(&quot;<sp>&quot;, $thedata);

For example, the values in the 5 text boxes where a,b,c,d,e. Then I would expect $thevar to equal &quot;a<sp>b<sp>c<sp>d<sp>e&quot;

But it is blank. What am I doing wrong?

Thanks in advance.
 
There's nothing wrong. I just checked it and it works without a glitch. Here's the codefragment I used to test it:

Code:
<?php

$tfield = array('a', 'b', 'c', 'd', 'e');

$thedata = array();
$tcount = 5;
$i = 0;
while ($tcount != 0) {
$thedata[] = $tfield[$i];
$i++;
$tcount--;
}

$thevar = implode(&quot;<sp>&quot;, $thedata);

echo $thevar;

?>

The echo shows &quot;abcde&quot; and by doing a view-source you can see that the HTML-code contains &quot;a<sp>b<sp>c<sp>d<sp>e&quot; It's not a lie if YOU believe it ;-)
 
Hmm, by looking closer at your post it see that you're using $tfield to read the values but your variables are called $tfield0, $tfield1, etc right? So you're not accessing those variables at all, that's why you get an empty result. It's not a lie if YOU believe it ;-)
 
The dr. is right. You also seem to have the arguments reversed in the implode call. Try:

$thedata = array();
$i = 0;
while ($i < 5) {
$varname = 'tfield'.$i++;
array_push($thedata,$$varname);
}

$thevar = implode($thedata,'<sp>');

Good luck! Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top