Jump to content

how I create this form dynamically.


thara

Recommended Posts

Its difficulty to explain my problem with my English knowledge. But I will try my best. I am going to create a HTML form dynamically with php. Using that form users can select subjects under different category. A category and Its subjects have broken to one logical section and have applied jquery accordion. In my form I need to create more logical section like that.

 

this is my HTML

 

<form action="" method="post">
<div id="accordion">
<!-- logical section 1 -->
<div>
<h3>Category 01: <span>category name</span><span></span></h3>
<div class="container">
<table>
<tr>
<td width="50%">
<input type="checkbox" value="1" name="subject[]">subject1
</td>
<td width="50%">
<input type="checkbox" value="2" name="subject[]">subject2
</td>
</tr>		
</table>											
</div>
</div>

<!-- logical section 2 -->
<div>
<h3>Category 02: <span>category name</span><span></span></h3>
<div class="container">
<table>
<tr>
<td width="50%">
<input type="checkbox" value="1" name="subject[]">subject1
</td>
<td width="50%">
<input type="checkbox" value="2" name="subject[]">subject2
</td>
</tr>		
</table>											
</div>
</div>
</div>									
</form>

 

Now I am trying to generate above form dynamically using category values which come from sessions. This is the PHP code I have tried so far..

 

<form action="" method="post"><br />
<div id="accordion">

<?php
// Check SESSION fromm category page
if ( isset ( $_SESSION['category']) && is_array( $_SESSION['category'])) {

$categoryIds = implode(',', $_SESSION['category']);

$q = "SELECT c. category_id AS ci, c.category_name AS cn, s.subject_name AS sn, s.subject_id AS si
FROM category AS c
INNER JOIN category_subjects AS cs ON cs.category_id = c.category_id
INNER JOIN subjects AS s ON s.subject_id = cs.subject_id
WHERE c.category_id IN ($categoryIds)";

$r = mysqli_query( $dbc, $q) ;


$catID = false;
$max_columns = 2;

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

 $categoryId = $row['ci'];
 $category = $row['cn'];

 //Detect change in category
 if($catID != $row['ci'])
 {
	 if($catID!=false)
	 {
		 if($recCount % $max_columns != 0)
		 {
			 //Close previous row
			 echo "</tr>\n";
		 }
		 //Close previous table
		 echo "</table>\n";
	 }

	 $catID = $row['ci'];
	 echo "<div>\n";
	 echo "<h3>Category 01: <span>{$category}</span><span></span></h3>\n";
	 echo "<div class='container'>\n";
echo "<table><tr>\n";
	 $recCount = 0;
 }

 $recCount++;

 if($recCount % $max_columns == 1)
 {
	 echo "<tr>\n";
 }

 $value = "{$row['ci']}:{$category}:{$row['si']}:{$row['sn']}";
 echo "<td width='50%'>";
 echo "<input type='checkbox' name='subject[]' value='{$value}' />{$row['sn']}";
 echo "</td>\n";

 if($recCount % $max_columns == 0)
 {
	 echo "</tr>\n";
 }

if($recCount % $max_columns != 0)
{
//Close last row
echo "</tr>\n";
}
//Close last table
echo "</table>\n";

echo "</div></div>";
}
}

?>

</div> <!-- end #accordion div -->
</form> 

 

Can anybody tell my where I am going wrong? I troubled how to create inner table with 2coloms.....

Thank you..

post-123452-0-13498900-1357927568_thumb.jpg

Edited by thara
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.