1

I have 2 table like this: Table State id_state state

1        AAA
2        BBB

Table Province id_province id_state province

1           1    qqq
2           1    www
3           2    eee
4           2    yyy
5           2    zzz

I want to show that a state have a few province in select-option HTML form like this:

AAA     qqq
        www
BBB     eee
        yyy
        zzz

Or in HTML code:

<select name='AAA'>
    <option value='qqq'>qqq</option>
    <option value='www'>www</option>
</select>
<select name='BBB'>
    <option value='eee'>eee</option>
    <option value='yyy'>yyy</option>
    <option value='zzz'>zzz</option>

</select>

I want to do it because if value in the database has changed, so the select-option also change. I use codeigniter framework.

So far I have try this in model:

SELECT * FROM state s,province p WHERE s.id_state=p.id_state

But the result become:

AAA     qqq
AAA     www
BBB     eee
BBB     yyy
BBB     zzz

If I use GROUP BY it will only return AAA and BBB. Thanks

2
  • 1
    Using GROUP_CONCAT() you can get a comma delimited list of the province. May 17, 2015 at 9:32
  • How to use GROUP_CONCAT() to implement select option list? Can you explain in detail? thanks May 17, 2015 at 9:40

4 Answers 4

0

You can try this:

SELECT 
  s.state,
  GROUP_CONCAT(p.province) AS province 
FROM state s
  LEFT JOIN province p ON s.id_state = p.id_state
GROUP BY s.id_state

By doing this you will get something like this:

AAA     qqq,www
BBB     eee,yyy,zzz

Then you can loop over each row of query result and for each row loop over the comma delimited list of province.

Edit : If you want to get the results as html code for province. This will avoid the looping over the list.

SELECT 
  s.state,
  GROUP_CONCAT(CONCAT('<option>',p.province,'<\/option>') SEPARATOR ' ') AS provinceOption 
FROM state s
  LEFT JOIN province p ON s.id_state = p.id_state
GROUP BY s.id_state

By doing this you will get something like this:

AAA     <option>qqq</option><option>www</option>
BBB     <option>eee</option><option>yyy</option><option>zzz</option>

Then you can loop over each row of query result and simply output the provinceOption.

9
  • If you're going to loop (which you should) there's absolutely no point in using GROUP_CONCAT
    – Strawberry
    May 17, 2015 at 9:48
  • @Strawberry For creating the options you have to. Is there any better choice rather than returning the html code from query? May 17, 2015 at 9:50
  • Just return a well-ordered array, and loop through that.
    – Strawberry
    May 17, 2015 at 9:51
  • @Strawberry well-ordered array can you clarify? Look at the output. The options are not for same select. May 17, 2015 at 9:53
  • a query with an order by clause can produce a 'well-ordered array', or it can be sorted in php (or even JavaScript)
    – Strawberry
    May 17, 2015 at 10:01
0

I'm not much of a php coder (that much should be obvious) - but this gets you the general idea:

<?php
require('path/to/mysqli/connection/stateme.nts');

$query = "
SELECT s.state
     , p.province
  FROM state s
  JOIN province p
    ON p.state_id = s.state_id
 ORDER
    BY state, province;
";

$result = mysqli_query($conn,$query);

$state = '';

echo "<table>\n";

while ($row = mysqli_fetch_assoc($result)){
if($row['state']==$state){
  $state_out = '';
  } else {
  $state_out = $row['state'];
    $state = $row['state'];

}
echo "<tr>\n
       <td>$state_out</td>\n
       <td>{$row['province']}</td>\n
      </tr>\n
      ";
}

echo "</table>\n";

?>
2
  • By showing tr td this can be done easily. Can you show this for select? May 17, 2015 at 10:45
  • @Beginner Where is your previous answer? It work like a charm. Thanks May 17, 2015 at 10:59
0

If you can put your below data in an array or if it is already in an array. Then please proceed with my below code.

AAA     qqq
AAA     www
BBB     eee
BBB     yyy
BBB     zzz

Please take your data in array as below and then you get the desired result.

    <?php
    $data = array(
        array('state' => 'AAA', 'province' => 'qqq'),
        array('state' => 'AAA', 'province' => 'www'),
        array('state' => 'BBB', 'province' => 'eee'),
        array('state' => 'BBB', 'province' => 'yyy'),
        array('state' => 'BBB', 'province' => 'zzz'),
    );

$prev_state = "";
$current_state = "";
$c = 0;
$max = sizeof($data);
for ($i = 0; $i < $max; $i++) {
    if ($data[$i]['state'] != $prev_state) {
        $current_state = $data[$i]['state'];
        ?>
        <select name="<?php echo $data[$i]['state']; ?>">
            <?php for ($j = 0; $i + $j < $max; $j++) {
                if ($data[$i + $j]['state'] == $current_state) {
                    ?>
                    <option value = "<?php echo $data[$i + $j]['province']; ?>"><?php echo $data[$i + $j]['province']; ?></option>
                <?php
                } else {
                    break;
                }
                $prev_state = $data[$i + $j]['state'];
            }
            ?>
        </select>
        <?php
    }
    $prev_state = $data[$i]['state'];
}
?>
0

At controller's function write code like this

$this->db->from('Province p');
$this->db->select('p.province,s.state');
$this->db->join('State s','s.id_state = p.id_state','INNER');
$results=$this->db->get()->result();
$states=array();
foreach($results as $r)
{
    $states[$r->state][]=$r->province;
}
//now send this $states to your view;
//$this->load->view('your_view_file',array('states'=>$states));

Now at your view

<?php
foreach($states as $state=>$provinces)
{
    ?>
        <select name="<?php echo $state;?>">
            <?php
                foreach($provinces as $province)
                {
                    ?>
                    <option value='<?php echo $province;?>'><?php echo $province;?></option>
                    <?php
                }
            ?>
        </select>
    <?php
}
?>

You can do it lots of way I just provide one.Hope this helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.