Reputation: 881
I am trying to fetch data from multiple SQL tables (if they have matching IMEI) and display them in html table. If the IMEI doesn't match then I want it to display '-' in that box.
For this I have display data from table A(import) then tried to match the IMEI. While doing this I used if else to define the condition, and the main problem occurs in that if and else. while executing code it will execute the if section following the else section it doesn't follow the condition.
for example:
while ($row = mysqli_fetch_assoc($result)){
...
...
*****************this while shows error
while($row1 = mysqli_fetch_assoc($result1)){
********here after executing if it will execute the else also. it does not follow the condition
if(import_imei == sales_imei){
<td>imei</td>
}else{
<td> - </td>
}
}
...
************the same condition work here perfectly
while($row2 = mysqli_fetch_assoc($result2)){
if(import_imei == ret_imei){
<td>name</td>
}else{
<td> - </td>
}
}
}
Here below is the code. I had written 'error' in code where the error occurs
$sql ="SELECT model,importDate,IMEI1,IMEI2 FROM mainreport ORDER BY id DESC LIMIT $start_from, $limit";
$result = mysqli_query($con,$sql);
while ($cgimport = mysqli_fetch_assoc($result)) {
$cgimport_modelName = $cgimport['model'];
$cgimport_importDate = $cgimport['importDate'];
$cgimport_IMEI1 = $cgimport['IMEI1'];
$cgimport_IMEI2 = $cgimport['IMEI2'];
?>
<tr>
<!-- ************** DISPLAYING DATA IN TABLE FROM mainreport **************** -->
<td style="text-align: center;"><?php echo $cgimport_modelName ;?></td>
<td style="text-align: center;"><?php echo $cgimport_importDate ;?></td>
<td style="text-align: center;"><?php echo $cgimport_IMEI1 ;?></td>
<!-- ************** FETCHING SALES DATA MADE BY CG TO DISTRIBUTOR **************** -->
<?php
$sql1 = "SELECT billMiti,invoiceDate,billno,price,nameOfParty,IMEINo FROM salesreport ";
$result1 = mysqli_query($con, $sql1);
while ($cgSalesReport = mysqli_fetch_assoc($result1)) {
$cgSalesReport_BillMiti = $cgSalesReport['billMiti'];
$cgSalesReport_invoiceDate = $cgSalesReport['invoiceDate'];
$cgSalesReport_billno = $cgSalesReport['billno'];
$cgSalesReport_price = $cgSalesReport['price'];
$cgSalesReport_distName = $cgSalesReport['nameOfParty'];
$cgSalesReport_IMEINo = $cgSalesReport['IMEINo'];
// echo $cgimport_IMEI1;
//******************************************
//ERROR IN THIS IF
//******************************************
if ( ($cgSalesReport_IMEINo == $cgimport['IMEI1']) || ($cgSalesReport_IMEINo == $cgimport['IMEI2']) ) {
?>
<!-- ************* DISPLAYING TABLE FROM salesreport *********************** -->
<td style="text-align: center;"><?php echo $cgSalesReport_BillMiti; ?></td>
<td style="text-align: center;"><?php echo $cgSalesReport_invoiceDate; ?></td>
<td style="text-align: center;"><?php echo $cgSalesReport_billno; ?></td>
<td style="text-align: center;"><?php echo $cgSalesReport_price; ?></td>
<?php
// ***************** FETCHING DATA FROM cdistributor ****************************
$sql2 = "SELECT rdCode,name FROM cdistributor ";
$result2 = mysqli_query($con, $sql2);
while ($cgDistributor = mysqli_fetch_assoc($result2)) {
$cgDistributor_rdCode = $cgDistributor['rdCode'];
$cgDistributor_name = $cgDistributor['name'];
if ($cgDistributor_name == $cgSalesReport_distName) {
?>
<!-- **************** DISPLAYING TABLE FROM cdistributor ***************** -->
<td align='center'><?php echo $cgDistributor_rdCode ; ?></td>
<td align='center'><?php echo $cgDistributor_name ; ?></td>
<?php
}
else{
?>
<td style="text-align: center;">-</td>
<td style="text-align: center;">-</td>
<?php
}
}
// ************ END FETCHING cdistributor *******************
}
// *************************
// ERROR IN THIS ELSE
//*************************
else{
?>
<td style="text-align: center;">-</td>
<td style="text-align: center;">-</td>
<?php
}
}
// *************** END FETCHING salesreport *****************
?>
<!-- ******************* FETCHING DISTRIBUTOR SALES DATA MADE BY DISTRIBUTOR TO RETAILER ****************** -->
<?php
$sql3 = "SELECT rinvoiceDate,rbillno,rPrice,rIMEINo,rnameOfParty,distname FROM rrdsalesreport";
$result3 = mysqli_query($con, $sql3);
while ($cgDistributorSales = mysqli_fetch_assoc($result3)) {
$cgDistributorSales_rinvoiceDate = $cgDistributorSales['rinvoiceDate'];
$cgDistributorSales_rbillno = $cgDistributorSales['rbillno'];
$cgDistributorSales_rPrice = $cgDistributorSales['rPrice'];
$cgDistributorSales_rIMEINo = $cgDistributorSales['rIMEINo'];
$cgDistributorSales_rnameOfParty = $cgDistributorSales['rnameOfParty'];
$cgDistributorSales_distname = $cgDistributorSales['distname'];
if ($cgimport_IMEI1 == $cgDistributorSales_rIMEINo || $cgimport_IMEI2 == $cgDistributorSales_rIMEINo) {
?>
<!-- ************* DISPLAYING TABLE FROM rrdsalesreport *********************** -->
<td style="text-align: center;"><?php echo $cgDistributorSales_rbillno; ?></td>
<td style="text-align: center;"><?php echo $cgDistributorSales_rinvoiceDate; ?></td>
<td style="text-align: center;"><?php echo $cgDistributorSales_rPrice; ?></td>
<?php
// ***************** FETCHING DATA FROM retailer ****************************
$sql4 = "SELECT rCode,rname,rd FROM retailer ";
$result4 = mysqli_query($con, $sql4);
while ($cgRetailer = mysqli_fetch_assoc($result4)) {
$cgRetailer_rCode = $cgRetailer['rCode'];
$cgRetailer_rname = $cgRetailer['rname'];
$cgRetailer_rd = $cgRetailer['rd'];
if ($cgRetailer_rname == $cgDistributorSales_rnameOfParty) {
?>
<!-- **************** DISPLAYING TABLE FROM retailer ***************** -->
<td align='center'><?php echo $cgRetailer_rCode ; ?></td>
<td align='center'><?php echo $cgRetailer_rname ; ?></td>
<?php
}
else{
?>
<td style="text-align: center;">-</td>
<td style="text-align: center;">-</td>
<?php
}
}
// ************ END FETCHING retailer *******************
}else{
?>
<td style="text-align: center;">-</td>
<td style="text-align: center;">-</td>
<td style="text-align: center;">-</td>
<td style="text-align: center;">-</td>
<td style="text-align: center;">-</td>
<?php
}
}
// *************** END FETCHING rrdsalesreport *****************
?>
<!-- ***************** FETCHING DATA FROM activereport ******************** -->
<?php
$sql5 = "SELECT aIMEI,dateOfActivation FROM activereport";
$result5 = mysqli_query($con, $sql5);
while ($cgActivereport = mysqli_fetch_assoc($result5)) {
$cgActivereport_aIMEI = $cgActivereport['aIMEI'];
$cgActivereport_dateOfActivation = $cgActivereport['dateOfActivation'];
if ($cgimport_IMEI1 == $cgActivereport_aIMEI || $cgimport_IMEI2 == $cgActivereport_aIMEI) {
?>
<td align='center'><?php echo $cgActivereport_dateOfActivation ; ?></td>
<?php
}
}
// ************* END OF FETCHING activereport ***************
?>
</tr>
<?php
} //************ END FETCHING MAINREPORT***********************
?>
Upvotes: 1
Views: 727
Reputation: 202
$sql = "Your First Query..";
$result = mysql_query($sql);
$jsonData = array();
while ($array = mysql_fetch_assoc($result)) {
$follower_id=$array['to_follow_user_id']
$SQL_ONE = "Your Second Query.....";
$result_one = mysql_query($SQL_ONE);
while ($array_one = mysql_fetch_assoc($result_one)) {
$jsonData[] = $array_one;
}
}
I think you want something like above..
Upvotes: 1