Please help me, how to compare 2 name from different query?
I want to compare subject1 with subject2 if its same words it will printing 'YES' (like the image).
So this is my code, my code just read the last query from subject1.
So how can I compare all query from subject1 with all subject2?
<table class="table table-bordered">
<tr>
<td class="table-bordered">No</td>
<td class="table-bordered">Date</td>
<td class="table-bordered">Subject Name</td>
<td class="table-bordered">Subject No</td>
<td class="table-bordered">Revision No</td>
<td class="table-bordered">Trainer</td>
<td class="table-bordered">Institution</td>
</tr>
<?php
$query = mysql_query("SELECT * FROM training_detail AS s JOIN materi AS t JOIN training_history AS u JOIN employee AS v WHERE v.employee_id = $id1 AND s.nik LIKE v.nik AND u.id_materi LIKE t.id_materi AND s.id_riwayat_training LIKE u.id_riwayat_training ");
$i=1;
while($row1 = mysql_fetch_array($query))
{
$date = $row1['date'];
$subject1 = $row1['subject_name'];
?>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo date("j/F/Y", strtotime($date)); ?></td>
<td class="table-bordered"><?php echo $subject1; ?></td>
<td class="table-bordered"><?php echo $row1['subject_no']; ?></td>
<td class="table-bordered"><?php echo $row1['revision_no']; ?></td>
<td class="table-bordered"><?php echo $row1['trainer']; ?></td>
<td class="table-bordered"><?php echo $row1['institution']; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</br>
<table class="table table-bordered">
<tr>
Training yang belum diikuti
<td class="table-bordered">No</td>
<td class="table-bordered"></td>
<td class="table-bordered">subject id</td>
<td class="table-bordered">subject namei</td>
<td class="table-bordered">subject no</td>
</tr>
<?php
$query = mysql_query("SELECT * FROM job_name AS r JOIN subject AS q JOIN employee AS p WHERE q.subject_id LIKE r.header_id AND r.job_id LIKE p.id_jabatan AND p.id_karyawan = $id1 ORDER BY q.id_materi ASC ");
$i=1;
while($row2 = mysql_fetch_array($query))
{
$subject2 = $row2['subject_name'];
if (strcasecmp($subject1, $subject2) == 0)
{
?>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo "YES" ?></td>
<td class="table-bordered"><?php echo $row2['subject_id'] ?></td>
<td class="table-bordered"><?php echo $subject2; ?></td>
<td class="table-bordered"><?php echo $row2['subject_no']; ?></td>
</tr>
<?php
}
else
{
?>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo "NO" ?></td>
<td class="table-bordered"><?php echo $row2['subject_id'] ?></td>
<td class="table-bordered"><?php echo $subject2; ?></td>
<td class="table-bordered"><?php echo $row2['subject_no']; ?></td>
</tr>
<?php
}
$i++;
}
?>
</table>
You need to use nested loop I guess. Use second while inside of first one.
BTW look at http://php.net/manual/en/control-structures.alternative-syntax.php, it might make your code seem better.
Related
I've a view to displaying some data from database to my php program, but the data cannot be displayed when I running it?
this is my view from phpmyadmin look like
(select `m`.`id_mutasi` AS `id_mutasi`,`m`.`tgl_mutasi` AS `tgl_mutasi`,`m`.`kode_barang` AS `kode_barang`,`a`.`nm_barang` AS `nm_barang`,`m`.`ruang_lama` AS `ruang_lama`,`m`.`ruang_baru` AS `ruang_baru`,`u`.`nm_unit` AS `unit_lama`,`m`.`unit_baru` AS `unit_baru`,`i`.`user` AS `user_lama`,`m`.`userbaru` AS `user_baru`,`m`.`jumlah` AS `jumlah`,`m`.`user_posting` AS `user_posting` from ((((((`simaset`.`mutasi` `m` join `simaset`.`inventarisasi` `i` on((`m`.`kode_inventarisasi_baru` = `i`.`kode_inventarisasi`))) join `simaset`.`aset` `a` on((`a`.`kode_barang` = `m`.`kode_barang`))) join `simaset`.`ruangan` `r` on((`r`.`kode_ruangan` = `m`.`ruang_lama`))) join `simaset`.`ruangan` `rb` on((`rb`.`kode_ruangan` = `i`.`kode_ruangan`))) join `simaset`.`unit_kerja` `u` on((`u`.`kode_unit` = `m`.`unit_lama`))) join `simaset`.`unit_kerja` `ub` on((`ub`.`kode_unit` = `i`.`kode_unit`))))
and this is my php code
<table id="table-example" class="table" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<!-- <th>NO</th> -->
<th>ID Mutasi</th>
<th>Tgl Mutasi</th>
<th>Kode Barang</th>
<th>Barang</th>
<th>Dept Lama</th>
<th>Jabatan Lama</th>
<th>Dept Baru</th>
<th>Jabatan Baru</th>
<!-- <th>User Baru</th> -->
<th>Jumlah</th>
<th>User Posting</th>
<!-- <th>User Posting</th> -->
<th>Aksi</th>
</tr>
</thead>
<tbody>
<!-- <?php $no = 1;?> -->
<?php do { ?>
<tr class=gradeX>
<!-- <td><center><?php echo $no++ ?></center></td> -->
<td><center>
<?php echo $row_rs_data['id_mutasi']; ?>
</center></td>
<td><?php echo $row_rs_data['tgl_mutasi']; ?></td>
<td ><?php echo $row_rs_data['kode_barang']; ?></td>
<td ><?php echo $row_rs_data['nm_barang']; ?></td>
<td ><?php echo $row_rs_data['ruanglama']; ?></td>
<td ><?php echo $row_rs_data['unitlama']; ?></td>
<td ><?php echo $row_rs_data['ruangbaru']; ?></td>
<td ><?php echo $row_rs_data['unitbaru']; ?></td>
<!-- <td ><?php echo $row_rs_data['userbaru']; ?></td> -->
<td ><center><?php echo $row_rs_data['jumlah']; ?></center></td>
<td ><?php echo $row_rs_data['user_posting']; ?></td>
<td ><img src="img/icons/packs/silk/16x16/cross.png" width="16" height="16" alt="Hapus" title="Hapus" /></td>
</tr>
<?php
echo "test";
} while ($row_rs_data = mysql_fetch_assoc($rs_data)); ?>
</tbody>
</table>
Replace your <tbody> block. Use while instead of do..while and mysqli instead of mysql mysql has been deprecated.
<tbody>
<?php while ($row_rs_data = mysql_fetch_assoc($rs_data)){ ?>
<tr class=gradeX>
<!-- <td><center><?php echo $no++ ?></center></td> -->
<td><center>
<?php echo $row_rs_data['id_mutasi']; ?>
</center></td>
<td><?php echo $row_rs_data['tgl_mutasi']; ?></td>
<td ><?php echo $row_rs_data['kode_barang']; ?></td>
<td ><?php echo $row_rs_data['nm_barang']; ?></td>
<td ><?php echo $row_rs_data['ruanglama']; ?></td>
<td ><?php echo $row_rs_data['unitlama']; ?></td>
<td ><?php echo $row_rs_data['ruangbaru']; ?></td>
<td ><?php echo $row_rs_data['unitbaru']; ?></td>
<!-- <td ><?php echo $row_rs_data['userbaru']; ?></td> -->
<td ><center><?php echo $row_rs_data['jumlah']; ?></center></td>
<td ><?php echo $row_rs_data['user_posting']; ?></td>
<td ><img src="img/icons/packs/silk/16x16/cross.png" width="16" height="16" alt="Hapus" title="Hapus" /></td>
</tr>
<?php
echo "test";
} ?>
</tbody>
I am trying to design the table but it seems coming into proper way but for some cases. In this table, that $cal_amount and $cal_amount_key part shouldbe print whole for any numbers of cars.
Code:
PHP
<?php
$TotV=array("car","bus","jeep");
$Nopkg=array(759,159,459);
$Avg=array(20,50,15);
$Qty=array(15000,58000,58922);
$rate=array(31.73,17.15,17.25);
$gro_tot=789456;
$Cex=781254;
$S_Tot=159254;
$Vat=12500;
$frieght=0;
$G_Tot=985251;
?>
CSS
table,td,th,tr {
border:1px solid black;
border-collapse: collapse;
text-align:left;
}
HTML
<table>
<tr>
<th>S.No</th><th align="center">Variety of Goods</th><th style="width:70px">No.and Description of<br/>Packages</th><th style="width:70px">Avg. Contents per Package<br/>(Pcs)</th><th style="width:70px">Total Quantity<br/>(Pcs)</th><th style="width:70px">Rate per pc.</th><th colspan="2" style="text-align:center;">Amount<br/>(Rs.)</th>
</tr>
<?php
for($i=0;$i<count($TotV);$i++)
{?>
<tr>
<td><?php echo $i+1;?></td>
<td><?php echo strtoupper($TotV[$i]);?></td>
<td><?php echo $Nopkg[$i];?></td>
<td><?php echo $Avg[$i];?></td>
<td><?php echo $Qty[$i];?></td>
<td><?php echo $rate[$i];?></td>
<?php
}?>
<td colspan="1">Total Value</td><td style="width:100px;text-align:right;"> <?php echo $gro_tot;?></td>
<tr><td></td><td></td>
<td></td><td></td><td></td><td></td><td style="width:150px;">C.Ex.Duty 12.5%</td><td style="width:150px;text-align:right;"> <?php echo $Cex;?></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td></td><td style="width:150px;">SubTotal</td><td id="num" style="width:150px;text-align:right;"> <?php echo $S_Tot;?></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td style="width:150px;">VAT/CST 2%</td><td style="width:150px;text-align:right;"> <?php echo $Vat;?></td></tr>
<tr><td style="border:none;" colspan="3"><b>qwert</b></td><td style="border:none;"></td><td style="border:none;" colspan="2"><td><b>Freight</b></td><td style="width:150px;text-align:right;"> <STRONG><?php echo $frieght;?></STRONG></td></tr>
<tr><td style="border:none;" colspan="3"><b>asdf</b></td><td style="border:none;"></td><td style="border:none;" colspan="2"><td><b>Grand Total</b></td><td style="width:150px;text-align:right;"> <STRONG><?php echo $G_Tot;?></STRONG></td></tr>
</tr>
</table>
Please suggest some changes to be done in code so that It will start from first row instead of third row. Thanks in advance.
Try this:
First make PHP array for total calculation and label.
$TotV=array("car","bus","jeep");
$Nopkg=array(759,159,459);
$Avg=array(20,50,15);
$Qty=array(15000,58000,58922);
$rate=array(31.73,17.15,17.25);
$gro_tot=789456;
$Cex=781254;
$S_Tot=159254;
$Vat=12500;
$frieght=0;
$G_Tot=985251;
$cal_amount_key = array('Total Value',
'C.Ex.Duty 12.5%',
'SubTotal',
'VAT/CST 2%',
'Freight',
'Grand Total');
$cal_amount = array($gro_tot,
$Cex,
$S_Tot,
$Vat,
$frieght,
$G_Tot);
Put the code inside loop:
<table>
<tr>
<th>S.No</th>
<th align="center">Variety of Goods</th>
<th style="width:70px">No.and Description of<br/>Packages</th>
<th style="width:70px">Avg. Contents per Package<br/>(Pcs)</th>
<th style="width:70px">Total Quantity<br/>(Pcs)</th>
<th style="width:70px">Rate per pc.</th>
<th colspan="2" style="text-align:center;">Amount<br/>(Rs.)</th>
</tr>
<?php
for($i=0;$i<count($TotV);$i++) {?>
<tr>
<td><?php echo $i+1;?></td>
<td><?php echo strtoupper($TotV[$i]);?></td>
<td><?php echo $Nopkg[$i];?></td>
<td><?php echo $Avg[$i];?></td>
<td><?php echo $Qty[$i];?></td>
<td><?php echo $rate[$i];?></td>
<td><?php echo $cal_amount_key[$i];?></td>
<td style="width:100px;text-align:right;"><?php echo $cal_amount[$i];?></td>
</tr>
<?php } ?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="width:150px;">VAT/CST 2%</td>
<td style="width:150px;text-align:right;"> <?php echo $Vat;?></td>
</tr>
<tr>
<td style="border:none;" colspan="3"><b>qwert</b></td>
<td style="border:none;"></td>
<td style="border:none;" colspan="2">
<td><b>Freight</b></td>
<td style="width:150px;text-align:right;"> <STRONG><?php echo $frieght;?></STRONG></td>
</tr>
<tr>
<td style="border:none;" colspan="3"><b>asdf</b></td>
<td style="border:none;"></td>
<td style="border:none;" colspan="2">
<td><b>Grand Total</b></td>
<td style="width:150px;text-align:right;"> <STRONG><?php echo $G_Tot;?></STRONG></td>
</tr>
</table>
image
Hello guys. I wanna ask how to minimize the second table?
because i wanna it just printed once no loop but i want all the value to compare with the first table
here is my code
<?php
$query1 = mysql_query("SELECT * FROM training_detail AS s JOIN subject AS t JOIN training AS u JOIN employee AS v WHERE v.id_employee = $id1 AND s.nik LIKE v.nik AND u.id_subject LIKE t.id_subject AND s.id_training LIKE u.id_training ");
$i=1;
while($row1 = mysql_fetch_array($query1))
{
$date = $row1['date'];
$subject1 = $row1['subject_name'];
?>
<table class="table table-bordered">
<tr>
A
<td class="table-bordered">No</td>
<td class="table-bordered">date</td>
<td class="table-bordered">subject</td>
<td class="table-bordered">subject no</td>
<td class="table-bordered">revision no</td>
<td class="table-bordered">Trainer</td>
<td class="table-bordered">Institution</td>
</tr>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo date("j/F/Y", strtotime($date)); ?></td>
<td class="table-bordered"><?php echo $subject1; ?></td>
<td class="table-bordered"><?php echo $row1['subject_no']; ?></td>
<td class="table-bordered"><?php echo $row1['revision_no']; ?></td>
<td class="table-bordered"><?php echo $row1['trainer']; ?></td>
<td class="table-bordered"><?php echo $row1['institution']; ?></td>
</tr>
</br>
<table class="table table-bordered">
<tr>
B
<td class="table-bordered">No</td>
<td class="table-bordered">subject</td>
<td class="table-bordered">subject name</td>
<td class="table-bordered">subject no</td>
</tr>
<?php
$query2 = mysql_query("SELECT * FROM header_job AS r JOIN subject AS q JOIN employee AS p WHERE q.id_subject LIKE r.id_header AND r.id_job LIKE p.id_job AND p.id_employee = $id1 ORDER BY q.id_subject ASC ");
$x=1;
while($row2 = mysql_fetch_array($query2))
{
$subject2 = $row2['subject_name'];
if (strcasecmp($subject1, $subject2) != 0)
{
?>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo $row2['subject'] ?></td>
<td class="table-bordered"><?php echo $subject2; ?></td>
<td class="table-bordered"><?php echo $row2['subject_no']; ?></td>
</tr>
<?php
}
$x++;
}
$i++;
}
?>
</table>
</table>
sorry about my pic's words is my own language
if you want to minimize the output
try to use limit
like so:
$query2 = mysql_query("SELECT * FROM header_job AS r JOIN subject AS q JOIN employee AS p WHERE q.id_subject LIKE r.id_header AND r.id_job LIKE p.id_job AND p.id_employee = $id1 ORDER BY q.id_subject ASC limit 1");
There's nothing wrong with the result itself.
The image should explain itself.
The table basicly is repeating the whole table for each results instead of displaying the results with multiple lines on one table, its doing 1 line on 1 table many times.
Here's the code that involves it-
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"id");
$f2=mysql_result($result,$i,"date");
$f3=mysql_result($result,$i,"agentclient");
$f4=mysql_result($result,$i,"propertydescription");
$f5=mysql_result($result,$i,"transactiontype");
$f5=mysql_result($result,$i,"applicabledocument");
$f5=mysql_result($result,$i,"received");
$f5=mysql_result($result,$i,"paid");
?>
<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr valign="bottom" bgcolor="#000000">
<td width="24"><span class="style1b"><strong>No.</strong></span></td>
<td width="105"><span class="style1b"><strong>Date</strong></span></td>
<td width="57"><span class="style1b"><strong>Agent/client</strong></span></td>
<td width="170"><span class="style1b"><strong>Property/Description</strong></span></td>
<td width="199"><span class="style1b"><strong>Transaction type </strong></span></td>
<td width="235"><span class="style1b"><strong>Applicable document </strong></span></td>
<td width="58"><span class="style1b"><strong>Received</strong></span></td>
<td width="58"><span class="style1b"><strong>Paid</strong></span></td>
</tr>
<tr valign="top" bgcolor="#FFFFFF">
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
<td><?php echo $f4; ?></td>
<td><?php echo $f5; ?></td>
<td><?php echo $f6; ?></td>
<td><?php echo $f7; ?></td>
<td><?php echo $f8; ?></td>
</tr>
</table>
<?php
$i++;
}
?>
There are lots of way to improve this code, but to fix your immediate problem, just take the header row out of the loop:
<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr valign="bottom" bgcolor="#000000">
<td width="24"><span class="style1b"><strong>No.</strong></span></td>
<td width="105"><span class="style1b"><strong>Date</strong></span></td>
<td width="57"><span class="style1b"><strong>Agent/client</strong></span></td>
<td width="170"><span class="style1b"><strong>Property/Description</strong></span></td>
<td width="199"><span class="style1b"><strong>Transaction type </strong></span></td>
<td width="235"><span class="style1b"><strong>Applicable document </strong></span></td>
<td width="58"><span class="style1b"><strong>Received</strong></span></td>
<td width="58"><span class="style1b"><strong>Paid</strong></span></td>
</tr>
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"id");
$f2=mysql_result($result,$i,"date");
$f3=mysql_result($result,$i,"agentclient");
$f4=mysql_result($result,$i,"propertydescription");
$f5=mysql_result($result,$i,"transactiontype");
$f5=mysql_result($result,$i,"applicabledocument");
$f5=mysql_result($result,$i,"received");
$f5=mysql_result($result,$i,"paid");
?>
<tr valign="top" bgcolor="#FFFFFF">
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
<td><?php echo $f4; ?></td>
<td><?php echo $f5; ?></td>
<td><?php echo $f6; ?></td>
<td><?php echo $f7; ?></td>
<td><?php echo $f8; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
When we run a query in ssms, we can get one or more rows of data per associateID.
However, on a table data,
<table>
<tr>
<td>...</td>
</tr>
</table>
We believe that the solution to our problem is to loop the data and dump contents in a table.
Any ideas how I can accomplish this?
Here is the code I am trying to loop:
<?php
//I need the loop here.
<table>
<tr>
<td class="dataItem" id="SignCode"></td>
<td class="dataItem" id="SignType"></td>
<td class="dataItem" id="SignSize"></td>
<td class="dataItem" id="SignColor"></td>
<td class="dataItem" id="Facing"></td>
<td class="dataItem" id="HorizClear"></td>
<td class="dataItem" id="VertClear"></td>
<td class="dataItem" id="Angle"></td>
<td class="dataItem" id="ReflCoat"></td>
<td class="dataItem" id="Condition"></td>
<td class="dataItem" id="Status"></td>
</tr>
</table>
?>
The data is a combination of query and the Javascript array. The query is
$tsql="select * from mytable where associateId='$aid'";
Then all the form ids are in Javascript like this example:
dojo.byId("SignType").innerHTML = obj["SignType"];
If you show us your php code we can give you a better answer but your code should be somthing like this:
<table>
<?php
// Your loop
while($row = /*fetchData()*/) {
?>
<tr>
<td class="dataItem" id="SignCode"><?php echo $row['SignCode']; ?></td>
<td class="dataItem" id="SignType"><?php echo $row['SignType']; ?></td>
<td class="dataItem" id="SignSize"><?php echo $row['SignSize']; ?></td>
<td class="dataItem" id="SignColor"><?php echo $row['SignColor']; ?></td>
<td class="dataItem" id="Facing"><?php echo $row['Facing']; ?></td>
<td class="dataItem" id="HorizClear"><?php echo $row['HorizClear']; ?></td>
<td class="dataItem" id="VertClear"><?php echo $row['VertClear']; ?></td>
<td class="dataItem" id="Angle"><?php echo $row['Angle']; ?></td>
<td class="dataItem" id="ReflCoat"><?php echo $row['ReflCoat']; ?></td>
<td class="dataItem" id="Condition"><?php echo $row['Condition']; ?></td>
<td class="dataItem" id="Status"><?php echo $row['Status']; ?></td>
</tr>
<?php } ?>
</table>
The code should look something like this:
(It is still unclear how you acquire the data from the DB.)
<?php
$resultSet = ... // <-- somehow aquire a result-set you can loop through
?>
<table>
<?php while ($row = <somehow_get_next_row_from_$resultSet_as_associative_array>) {?>
<tr>
<td class="dataItem" id="SignCode"><?php echo($row["signCode"]); ?></td>
<td class="dataItem" id="SignSize"><?php echo($row["signSize"]); ?></td>
<td class="dataItem" id="SignColor"><?php echo($row["signColor"]); ?></td>
...
<td class="dataItem" id="Status"><?php echo($row["status"]); ?></td>
</tr>
<?php } ?>
</table>
In case the value for id (e.g. "SignCode" etc) is the same as the name of the DB column-name AND the query returns the columns in the order you want the HTML-table columns to be, then the part between <tr> and </tr> could be further simplified as:
...
<tr>
<?php forEach ($row as $key => $value) {?>
<td class="dataItem" id="<?php echo($key); ?>"><?php echo($value); ?></td>
<?php } ?>
</tr>
...