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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php checkbox array with string variable argument.

Status
Not open for further replies.

vjh

Technical User
Dec 14, 2002
46
CA
Hi All,

I'm trying to create a function where I generate checkboxes by querying my database, putting the data into a string variable that is the proper format for the argument for creating the array I want. But it doesn't work!

If I do this:
$ckbx=array('1'=>'Book or Booklet','2'=>'Extension Bulletin','3'=>'Map','9'=>'Other','4'=>'Pamphlet','5'=>'Poster','6'=>'Technical Report','7'=>'Video','8'=>'Web Site');

it works just fine.

But when I dynamically construct a string that has exactly the text inside the parens - lets call it $stuff, I end up with a single element in my array that is the string.

$ckbx=array($stuff);
Here $ckbx[0] is the complete string.


What do I need to do??

Thanks for any and all suggestions!

vj
 
So you're saying that you've set stuff so that:

$stuff ="'1'=>'Book or Booklet','2'=>'Extension Bulletin','3'=>'Map','9'=>'Other','4'=>'Pamphlet','5'=>'Poster','6'=>'Technical Report','7'=>'Video','8'=>'Web Site'";

When you feed that into the array it just takes the whole string and sticks it in as one value. It doesn't know to split it up into key => value pairs. To do that you'd need to somehow split the string and feed that into the array, but that's probably not what you're looking for (i'm guessing). What exactly are you trying to accomplish by feeding the string into the array? There's probably another way to go about it, but I'd need to know more about what you're trying to do...

Jim
 
this is not recommended but you could do:
Code:
$stuff= "array(".$stuff.");";
eval ($stuff);
echo "<pre>";
print_r($stuff);
echo "</pre>";

much better would be to build the array dynamically rather than the string

Code:
$stuff[] = "Book or Booklet";
$stuff[] = "Extension";
//etc
//this can also be done programmatically if the values come from a database or other structured environment
echo "<pre>";
print_r($stuff);
echo "</pre>";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top