php - Select field, presetting value -


as part of project have page categories listed. each of them has link single category page in order modify it.

in category page, set category name field select field listing categories pre-set on selected category default. example should clarify question.

                      category                       category b selected category --->category c<--------preset category opening page                       category d                       category e 

i implemented code below , preset category works. difficulty finding on other categories listing. category id passed single category page method:

<a href="categorie_scheda.php?cat_id='.$rows['0'].'">link</a> 

---category page---

<?php     $cat_id = $_get['cat_id'];  //cat id captured here      include '../sys/conn.php';     $category = mysqli_query ($conn, "(mysqli_query)") or die ("query non valida: " . mysqli_error($conn));      $array = mysqli_fetch_array($category, mysql_num);     mysqli_close($conn); ?> 

---html----

<label>categorie</label> <select class="form-control" name='category_list'>     <option selected value=$cat_id><?php echo $array[1]; ?></option>  <?php      while ($listcat=mysqli_fetch_array($category)){         echo '<option value="'.$listcat['0'].'"> '.$listcat['0'].' - '.$listcat['1'].'</option>'; } ?> </select> 

hope can through.

try using in category page:

<?php     $cat_id = intval($_get['cat_id']);  //cat id captured here. converting int avoid xss      include '../sys/conn.php';     $category = mysqli_query ($conn, "(mysqli_query)") or die ("query non valida: " . mysqli_error($conn));      // replace $array = mysqli_fetch_array($category, mysql_num);     $array = mysqli_fetch_all($category, mysql_num);     mysqli_close($conn); ?> 

your html should go this:

<label>categorie</label> <select class="form-control" name='category_list'>  <?php      for($i = 0; $i < count($array); $i++) {         $listcat = $array[$i];         echo '<option ' . ($listcat[0] == $cat_id ? 'selected ' : '')               . 'value="'.$listcat['0'].'"> '               . $listcat['0'].' - '.$listcat['1'].'</option>';     } ?> </select> 

as noted in comments, closing connection without first saving results. either wrote above (with minor bugfixes if appear) or close connection after done.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -