(PHP) Categorize into an html opgroup based on mysql value -


basically have database 66 bible books; old testament new. bname value name of book, while bsect has value of o or n(new or old), how can make dropdown box dynamically display book old or new optgroup based on whether its' bsect o or n? teacher said have make array, have no idea how it. thoughts?

my database sample:

+-----------+-------+ | bname     | bsect | +-----------+-------+ | genesis   | o     | | exodus    | o     | | leviticus | o     | +-----------+-------+ 

i don't want have rely on manually setting opgroups based on number of entry, want dynamic based on value of bsect.

right have following query select dropdown puts book old or new based on record number, break if more books added

    $query = $mysqli->query("select distinct bname name kjv");    ?>  <select name="book">     <?php     $i=1;     while($option = $query->fetch_object()){          if($i==1) echo "<optgroup label='old testament'>";         else if($i==40) echo "<optgroup label='new testament'>";         echo "<option value='$i'>".$option->name."</option>";             $i++;         }       ?> 

simply order bsect , display different optgroups dynamically

<?php $query = $mysqli->query("select distinct bsect, bname name kjv order bsect");     ?>  <select name="book"> <?php     $i = 1;     $bsect = "";     while($option = $query->fetch_object()){          if($bsect != $option->bsect) {             $bsect = $option->bsect;             echo "<optgroup label='{$bsect}'>";         }         else if($i==40) echo "<optgroup label='new testament'>";         echo "<option value='$i'>".$option->name."</option>";             $i++;         }  ?> 

of course, books may out of order. want add book-order column (border) stores number defining how order books in given group, e.g.

alter table kjy      add column border int u?nsigned not null default 0; 

then can update data have proper book order , query this:

select distinct bsect, bname name kjv order bsect, border; 

of course, being bible, aren't going adding books, can define static book id defines ordinality of each book. sort id , know "old" , "new" books coming out in right order.

alter table kjy          add column id int unsigned not null primary key before (bname); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -