tags:

views:

29

answers:

4

I've got an array of sites like so...

$data = array(
'http://site1.net/',
'http://site2.net/',
'http://site3.org/'
);

And I'd like to create a script that iterates over the array and creates a list of input text fields containing each array element...

Example:
 1. [] http://site1.net/
 2. [] http://site2.net/
 3. [] http://site2.net/

Where [] represents the checkbox element and the URL is inside an input text field.

<ol>
 <li><input type="checkbox" id="check1" /><input type="text" id="text1" value="http://site1.net/" /></li>
 <li><input type="checkbox" id="check2" /><input type="text" id="text2" value="http://site2.net/" /></li>
 <li><input type="checkbox" id="check3" /><input type="text" id="text3" value="http://site3.net/" /></li>
etc...
A: 

Seems simple enough. I'd use a foreach() loop. What trouble are you having?

Scott Saunders
A: 
foreach( $data as $item ) {

echo "<input type='checkbox' ... "

}
Laykes
+1  A: 
foreach ($data as $key => $site)
{
    echo sprintf('<li><input type="checkbox" id="check%d" /><input type="text" id="text%d" value="%s" /></li>', $key, $key, $site);
}
Tom
Actually, that will give you "check0", etc. If you want to start at 1, just use $key + 1.
Tom
Thanks Tom. That's pretty sweet! I would've used a for loop and counters but I really like the cleanliness of your approach.
Scott B
A: 

Try this

$data = array(
'http://site1.net/',
'http://site2.net/',
'http://site3.org/'
);

function getCechkbox($array)
{
foreach( $array as $key=>$item ) 
  echo "$item :<input type='checkbox'>";
}

<form action="select.htm">
  <p>
  <?php getCechkbox($data); ?>
  </p>
</form>
streetparade