I’m using php and Mysqli,
relationship tables, I want to get the data in multiple selectbox, but! previously selected recordings must be selected in the box. I’ve been looking at the web for hours, looked at the examples, but still failed.
How should the mysql query be?
How should I loop with while?
Table structure in this way:
+----------------------+
| tb_haber |
+----------------------+
| haber_id | haber_tit |
+----------+-----------+
| 1 | text |
+----------+-----------+
+------------------+
| tb_kategori |
+------------------+
| kat_id | kat_tit |
+--------+---------+
| 1 | PHP |
+--------+---------+
| 2 | CSS |
+--------+---------+
| 3 | ASP |
+--------+---------+
| 4 | MYSQL |
+--------+---------+
+-------------------+
| tb_relation |
+-------------------+
| haber_id | kat_id |
+----------+--------+
| 1 | 1 |
+----------+--------+
| 1 | 2 |
+----------+--------+
this code I tried, only selected categories appear.
<?php
$con= mysqli_connect("localhost","root","","db");
mysqli_set_charset($con, 'utf8');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
mysqli_close($con);
}
$id=$_GET['id'];
$query1=mysqli_query($con,"SELECT * FROM tb_haber JOIN tb_relation ON tb_haber.id = tb_relation.haber_id WHERE haber_id=$id ");
$row1=mysqli_fetch_array($query1);
$haber=$row1['haber_id'];
$query2=mysqli_query($con,"SELECT * FROM tb_kategori JOIN tb_relation ON tb_kategori.kat_id = tb_relation.kat_id WHERE haber_id=$haber ");
?>
<select name="haber_kategori[]" class="select-chosen " data-placeholder="Kategori Seç..." multiple="multiple">
<?php
while($row2=mysqli_fetch_array($query2)){
$kategori=$row2['kat_tit'];
$k_id=$row2['kat_id'];
$sql_id=mysqli_query($con, "SELECT * FROM tb_kategori");
while ($bul_id=mysqli_fetch_array($sql_id)) {
$k_ids=$bul_id['kat_id'];
$k_tit=$bul_id['kat_tit'];
if ($k_ids==$k_id) {
?>
<option value="<?php echo $k_ids; ?>" selected>
<?php echo $k_tit; ?>
</option>
<?php } } }?>
</select>