2 Replies - 1488 Views - Last Post: 28 March 2013 - 03:08 PM Rate Topic: -----

#1 djdave106  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-March 12

generate html listbox markup using php

Posted 28 March 2013 - 11:29 AM

I want to generate html listbox markup using php. The values and displayed items for the listbox are to be taken from arrays. Easy enough. But the listbox is to retain its value when the form is posted back. Here is an example of the hand-written (non-generated) HTML/PHP:

<select size=2 name="lstResource" onchange="doPostBack();"> 
   <option value="3" <?php echo ((IsSet($_POST["lstResource"]) and $_POST["lstResource"]=="3")?"SELECTED":""); ?> >3G 5-a-side</option>
   <option value="1" <?php echo ((IsSet($_POST["lstResource"]) and $_POST["lstResource"]=="1")?"SELECTED":""); ?> >3G 7-a-side</option>
</select>



This works as I want it. But I want something more general. I wanted to create a function that would generate this HTML/PHP. So I wrote

<?php 

echo generateListBox('lstResource', Array(3, 1), Array('3G 5-a-side', '3G 7-a-side'));

function generateListBox($name, $values, $items) {
  if (count($values) <> count($items)) { echo 'Unable to generate ListBox due to unequal numbers of items in source arrays'; }
  $options = '';
  for ($i = 0; $i < count($values); $i++) {
    $options .= '<option value="' . $values[$i] . '" <?php echo ((IsSet($_POST["' . $name . '"]) and $_POST["' . $name . '"]=="' . $values[$i] . '")?"SELECTED":""); ?> >' . $items[$i] . '</option>';
  }
  return '<select size=' . count($values) . ' name="' . $name . '" onchange="doPostBack();">' . $options . '</select>';
}
?> 



If I run this as a standalone file, it generates the HTML I want. But when I insert it into my app, it no longer works as I want. And of course it shouldn't, but forgive me, I've only been using PHP for 4 days so it took a while for me to realise that what I was trying to do wouldn't work, as I am asking PHP to generate code and then interpret it's own product.

There is some kind of encoding takes place, with the result that the values displayed in the ListBox are '>3G 5-a-side' and '>3G 7-a-side'. The spurious '>' is not being interpreted as the close of the <option> tag. I don't understand why. Here is what is actually generated:

<select size="2" name="lstResource" onchange="doPostBack();"><option value="3" <?php="" echo="" ((isset($_post["lstresource"])="" and="" $_post["lstresource"]="=&quot;3&quot;)?&quot;SELECTED&quot;:&quot;&quot;);" ?=""> &gt;3G 5-a-side</option><option value="1" <?php="" echo="" ((isset($_post["lstresource"])="" and="" $_post["lstresource"]="=&quot;1&quot;)?&quot;SELECTED&quot;:&quot;&quot;);" ?=""> &gt;3G 7-a-side</option></select>



I see that the '>' has become &gt; - and a lot more strange shenanigans besides. So my function generates HTML/PHP, but it isn't interpreted as HTML/PHP. I thought what I wanted was to use eval() on what I generate, but eval expects PHP, not HTML. The first user-comment on the eval manual page:

http://php.net/manua...nction.eval.php

offers some code to use if you have a blend of HTML and PHP - this looed to be just what I wanted, but I get

Parse error: syntax error, unexpected T_ECHO in C:\Program Files (x86)\Apache Group\Apache2\htdocs\booking\booking.php(79) : eval()'d code on line 1

Line 79 is where eval is called within the PhpStringParser Class.

The HTML that is generated is

<b>Parse error</b>:  syntax error, unexpected T_ECHO in <b>C:\Program Files (x86)\Apache Group\Apache2\htdocs\booking\booking.php(79) : eval()'d code</b> on line <b>1</b><br>
<br>
<b>Parse error</b>:  syntax error, unexpected T_ECHO in <b>C:\Program Files (x86)\Apache Group\Apache2\htdocs\booking\booking.php(79) : eval()'d code</b> on line <b>1</b><br>
<select size="2" name="lstResource" onchange="doPostBack();"><option value="3">3G 5-a-side</option><option value="1">3G 7-a-side</option></select>



so I get the <SELECT> but no value retention. Is there a way to do what I want?

Is This A Good Question/Topic? 0
  • +

Replies To: generate html listbox markup using php

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3734
  • View blog
  • Posts: 13,054
  • Joined: 12-December 12

Re: generate html listbox markup using php

Posted 28 March 2013 - 01:55 PM

You are embedding PHP within PHP, which you cannot do. Take this part out:

<?php echo ((IsSet($_POST["' .
    $name . '"]) and $_POST["' . $name . '"]=="' . $values[$i] . '")?"SELECTED":""); ?>

- this is the embedded part.

Create a separate variable for the result of this expression (without the <?php and echo), and then use this new variable when you are forming the HTML. This will have the side effect of making the HTML expression slightly easier to read.

BTW Wrong forum.

BTWW Use isset() rather than IsSet(), which is distracting :)

This post has been edited by andrewsw: 28 March 2013 - 01:54 PM

Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3075
  • Posts: 10,783
  • Joined: 08-August 08

Re: generate html listbox markup using php

Posted 28 March 2013 - 03:08 PM

This seems simple to me:
<?php
// You might read the heredocs from a file to keep HTML out of your PHP code.
$listbox =<<<here
<select size=2 name="lstResource" onchange="doPostBack();"> 
	{options}
</select>
here;
$option =<<<opt
   <option value="{value}" {sel} >{text}</option>
opt;


echo generateListBox('lstResource', Array(3, 1), Array('3G 5-a-side', '3G 7-a-side'), $listbox, $option);


function generateListBox($name, $values, $items, $box, $opt) {
	$cnt = count($values);
	if ($cnt<> count($items)) { 
		echo 'Unable to generate ListBox due to unequal numbers of items in source arrays'; 
	}
	
	$replace = array("{value}","{sel}","{text}");
	$thelist = "";
	for($i = 0; $i < $cnt; $i++) {
		$with = array($values[$i], isset($_POST[$name]) && $_POST[$name] == $values[$i] ? "SELECTED":"", $items[$i]);
		$thelist .= str_replace($replace, $with, $opt);
	}

	echo str_replace("{options}", $thelist, $box);
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1