2013-01-11 39 views
0

我將使用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

你將不得不有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>'; 
+0

然後我如何使用while循環? – TNK

+0

你可以選擇任何你喜歡的東西,通過 – Techie

+0

@Dasun循環..對我來說很難理解?你能用例子來闡述你的答案嗎?謝謝。 – TNK