简体   繁体   中英

PHP and MySQL To Join 2 Data Table

i have table tb_satker

kode_propinsi | nama_satker  |  kode_satker
500           | A            |  1
500           | B            |  2
500           | C            |  3
500           | D            |  4

also table tb_upload

kode_propinsi | kode_satker  |  month
500           | A            |  1
500           | A            |  2
500           | B            |  3

I want to create php and mysql code to generate table like this

No | UPT | Jan | Feb | Mar | Apr | Mei | Jun | Jul | Ags | Sep | Okt | Nov | Des
1  | A   | 1   | 2   | 0   | 0   | 0   | 0   | 0   |  0  | 0   | 0   | 0   | 0
2  | B   | 0   | 0   | 3   | 0   | 0   | 0   | 0   |  0  | 0   | 0   | 0   | 0
3  | C   | 0   | 0   | 0   | 0   | 0   | 0   | 0   |  0  | 0   | 0   | 0   | 0
4  | D   | 0   | 0   | 0   | 0   | 0   | 0   | 0   |  0  | 0   | 0   | 0   | 0

This is my php code, it's work to create master table, live code at this link

But how to make table with inactive images to show if no value (0) and active images if month filled with value (1/2/3/4... etc)

<table class="table table-hover">
<thead>
    <tr>
        <th>No</th>
        <th>UPT</th>
        <th>Jan</th>
        <th>Feb</th>
        <th>Mar</th>
        <th>Apr</th>
        <th>Mei</th>
        <th>Jun</th>
        <th>Jul</th>
        <th>Ags</th>
        <th>Sep</th>
        <th>Okt</th>
        <th>Nov</th>
        <th>Des</th>
    </tr>
</thead>
<tbody>
<?
$view = "SELECT * FROM $tb_satker WHERE kode_propinsi='$propinsi'";
$result = mysql_query($view);
$jumlah=mysql_num_rows($result);
$nomor=0;
while
($baris=mysql_fetch_array($result))
{
$namasatker=$baris['nama_satker'];
$kodesatker=$baris['kode_satker'];
$kodepropinsi=$baris['kode_propinsi'];
$nomor=$nomor+1;
?>
    <tr>
        <th><? echo $nomor;?></th>
        <th><? echo $namasatker;?></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
<? }?>
    </tr>
</tbody>
</table>

You can use this query:

SELECT a.id,a.code,a.name,
max(case when b.month=1 then 1 else "-" end) as month1,
max(case when b.month=2 then 1 else "-" end) as month2,
max(case when b.month=3 then 1 else "-" end) as month3
FROM `table_a` as a
left join table_b as b on a.name= b.name
group by a.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM