Computer Science Era

Advertisement

Saturday, 22 February 2014

PHP to HTML
Check boxes, Option buttons, Drop down List, 
Text box and Text area can be generated inside foreach/for/while loop. In case of every element except drop down list, there should be a counter which should be incremented every time so that every element assigned unique key. For example: $tempNo is a variable/counter which is
incremented till data is coming from database tables / defined condition remains true.

To generate checkbox dynamically 2-dimensional array should be used in name of the checkbox

$tempNo = 1;
foreach(Array/object coming from database as $i)
{
echo("<input type='checkbox' name='checkAnswers[$tempNo][]' value='$object-value-1' />object-value/array-value-1");
echo("<input type='checkbox' name='checkAnswers[$tempNo][]' value='$object-value-2' />object-value/array-value-2");
echo("<input type='checkbox' name='checkAnswers[$tempNo][]' value='$object-value-3' />object-value/array-value-3");
            $tempNo = $tempNo + 1;
}
and so on..

To generate Option/Radio Buttons dynamically 1-dimensional array should be used in name of the option/radio buttons

$tempNo = 1;
foreach(Array/object coming from database as $i)
{
echo("<input type='radio' name='radioAnswer[$tempNo]' value='$object-value-1' /> object-value/array-value-1");
echo("<input type='radio' name='radioAnswer[$tempNo]' value='$object-value-2' /> object-value/array-value-2");
echo("<input type='radio' name='radioAnswer[$tempNo]' value='$object-value-3' /> object-value/array-value-3");
$tempNo = $tempNo + 1;
}
and so on…

To generate Text Fields/Text Area dynamically 1-dimensional array should be used in name of the Text Fields/Text Area

$tempNo = 1;
foreach(Array/object coming from database as $i)
{
echo("<textarea id='textarea' name='textanswerlarge[$tempNo]'></textarea>");
or
echo("<input type='text' name='textAnswerSmall[$tempNo]' value='$object-value-1'>");
}
and so on..

To generate Drop Down List / <select><option> dynamically. <select> tag should be used outside the foreach or any other loop.

$output .= "\n<select name='category' id='category'>";
foreach(Array/object coming from database as $i)
{
            $output .= "\n<option value='$i'>$i</option>";
}
$output .= "\n</select>"; 

To generate Drop Down List/<select><option> dynamically with pre-selected value. 

$output .= "\n<select name='category' id='category'>";
$output .= "\n<option value='0' selected='selected'>----------Category-----------</option>";
foreach(Array/object coming from database as $i)
{
if($i === Parameters coming in functions)
//use === in condition in case of different data types
//use == in condition in case of same data types
{
            $output .= "\n<option value='$i' selected='selected'>$i</option>";
}
else
{
            $output .= "\n<option value='$i'>$i</option>";
}
}
$output .= "\n</select>"; 


0 comments:

Post a Comment