我如何动态创建此表单

时间:2013-01-11 18:10:31

标签: php html

我将使用php动态创建HTML表单。使用该表单,用户可以选择不同类别的主题。一个类别及其主题已经分解为一个逻辑部分并应用了jquery手风琴。在我的表单中,我需要创建更像这样的逻辑部分。

// 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'];

        $subjects = array();

        $subjects[] = $row['sn'];

        echo '<div>';

        //Detect change in category
        if($catID != $categoryId) 
        {
            echo "<h3>Category 01: <span>{$category}</span><span></span></h3>";
            echo "<div class='container'>";

            echo '<table>'; 

            $j = 0;

            foreach ( $subjects AS $sub ) {
                if($j%2 == 0 && $j == 0)
                {
                    echo '<tr>';
                    echo '<td width="50%"><input type="checkbox" value="1" name="subjects[]">'.$sub.'</td>';
                }
                else if($j%2 == 0)
                {
                    echo '</tr><tr>';
                    echo '<td width="50%"><input type="checkbox" value="1" name="subjects[]">'.$sub.'</td>';
                }
                echo '<td width="50%"><input type="checkbox" value="1" name="subjects[]">'.$sub.'</td>';
                $j++;
            }
            echo '</tr>'; 
            echo '</table>';    
            echo "</div> <!-- End .container DIV -->\n";            
        }
        $catID = $categoryId;       
        echo '</div>';
   }
}

1 个答案:

答案 0 :(得分:1)

你必须有2个循环,如下所示。

foreach/for loop to create the categories
{
 foreach/for another loop inside above loop to create levels
 here you can have a table with two columns`<td>`

  Apply below logic to create a row with two columns`<td>`
  //$i is the counter, when you loop through once you increase the $i by one.
  if($i%2 == 0)
  //add new `<tr>`. If it's $i != 0 close the <tr> before opening one.
}

如果您有任何问题,请告诉我。

我们假设如下。从SQL

创建如下的数组
    levels_array[1][] = "pre school" //cat 1 levels
    levels_array[1][] = "pre school 1" //cat 1 levels 
    levels_array[1][] = "pre school 2"  //cat 1 levels
    levels_array[2][] = "school 2"  //cat 2 levels
    levels_array[2][] = "school 2"  //cat 2 levels
    levels_array[3][] = "college" //cat 3 levels

    cat_array[1] = "catgeory 1" 
    cat_array[2] = "catgeory 2" 
    cat_array[3] = "catgeory 3" 


for($i = 1; < count($cat_array); $i++)
{

foreach(levels_array[$i] as $item)
{
//create the table with the above mentioned logic
}

}

根据更新的问题

echo '<table>'; $j = 0;
foreach ( $subjects AS $sub ) {
if($j%2 == 0 && $j == 0)
{
  echo '<tr>';
  echo '<td>'.$sub.'</td>';
}
else if($j%2 == 0)
 {
  echo '</tr><tr>';
  echo '<td>'.$sub.'</td>';
 }
echo '<td>'.$sub.'</td>';
$j++;

}
echo '</tr>'; 
echo '</table>';