Дерево категорий из базы данных.Сделать кликабельным только cat_parent_id> 0 - PullRequest
0 голосов
/ 28 февраля 2019

У меня есть этот скрипт, чтобы получить дерево категорий из базы данных и показать его в виде HTML SELECT

function CategoryTree(&$output=null, $cat_parent_id=0, $indent=null){

$con = new PDO("mysql:host=localhost;dbname=test-2", 'root', '');

try {
// prepare select query
$query = "SELECT cat_id, cat_name FROM category WHERE cat_parent_id=:parentid";
$stmt = $con->prepare($query);

// this is the first question mark
$stmt->bindParam(2, $id);

// execute our query
$stmt->execute(array( 'parentid' => $cat_parent_id));

// show the categories one by one
while($c = $stmt->fetch(PDO::FETCH_ASSOC)){
    $output .= '<option value=' . $c['cat_id'] . '>' . $indent . $c['cat_name'] . "</option>\n";
    if($c['cat_id'] != $cat_parent_id){

        CategoryTree($output, $c['cat_id'], $indent . "&nbsp;&nbsp;");
    }
}
// return the list of categories
return $output;
 }
  // show error
    catch(PDOException $exception){
   die('ERROR: ' . $exception->getMessage());
   }
    }

и HTML:

<td><select name="category" id="category" required="">
 <option value='0'>Select the category</option>

 <?php

   echo CategoryTree();
 ?>  
     </select>
  </td>

Я хотел бы, чтобы когда cat_parent_id =0 в html select, cat_name не кликабелен, просто выделите его жирным шрифтом.Хотя, если cat_parent_id> 0 остаются доступными для выбора в HTML выберите.

Как я могу это сделать?Спасибо

1 Ответ

0 голосов
/ 28 февраля 2019

Вы должны изменить следующий код:

while($c = $stmt->fetch(PDO::FETCH_ASSOC)){
    $output .= '<option value=' . $c['cat_id'] . '>' . $indent . $c['cat_name'] . "</option>\n";
    if($c['cat_id'] != $cat_parent_id){

        CategoryTree($output, $c['cat_id'], $indent . "&nbsp;&nbsp;");
    }
}

на:

while($c = $stmt->fetch(PDO::FETCH_ASSOC)){
$disable= "";
if($cat_parent_id==0 ){
        $disable= 'disabled="disabled"';
    }
    $output .= '<option  '. $disable.'  value=' . $c['cat_id'] . '>' . $indent . $c['cat_name'] . "</option>\n";
    if($c['cat_id'] != $cat_parent_id){

        CategoryTree($output, $c['cat_id'], $indent . "&nbsp;&nbsp;");
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...