0

I have a table display records from database. One of my column is for delete, what I need is if the value is in both table1 and table2, void the link. Or if value is in only on table1 delete button should not be void. Help please?

$query1 = $mysqli1->query("select * from code WHERE item LIKE '%$search%' OR item_code LIKE '%$search%' OR cat_code LIKE '%$search%' order by item_code ASC");
$query2 = $mysqli1->query("SELECT count(*) FROM code");
$query3 = $mysqli->query("SELECT count(*) FROM app");

while($r = $query1->fetch_assoc()){
echo"<tr>
    <td>".$r['item']."</td>
    <td>".$r['cat_code']."</td>
    <td>".$r['item_code']."</td>";
    if(mysqli_num_rows($query3) == 0 && mysqli_num_rows($query2) == 0) {
    echo "<td><a href='#' id='".$r['id']."' class='del'><img src='../images/del.png'></a></td>";
    } else {
    echo "<td><a href='javascript:void(0)'><img src='../images/stop.png'></a></td>";
    echo"</tr>";
}

If ACR-100 is in on both table in database, link should be void

Table1 -------------- Table2

 Code        |         Code  
------       |       ------
ACR-100      |       ACR-100 

Else, if ACR-100 is only on table1, link should not be void.

Table1 -------------- Table2

 Code        |         Code  
------       |       ------
ACR-100      |        
user3462269
  • 71
  • 1
  • 7

1 Answers1

0

If all you need to know is whether the code exists in table2 then maybe something along the lines of this (I'm guessing some column names etc. so you have to adapt it to your database):

$query1 = $mysqli1->query("select code.*, app.item_code as app_code from code LEFT JOIN app ON code.item_code = app.item_code WHERE code.item LIKE '%$search%' OR code.item_code LIKE '%$search%' OR code.cat_code LIKE '%$search%' order by code.item_code ASC");

while($r = $query1->fetch_assoc()){
echo"<tr>
<td>".$r['item']."</td>
<td>".$r['cat_code']."</td>
<td>".$r['item_code']."</td>";
if($r['app_code'] == '') {
echo "<td><a href='#' id='".$r['id']."' class='del'><img src='../images/del.png'></a></td>";
} else {
echo "<td><a href='javascript:void(0)'><img src='../images/stop.png'></a></td>";
echo"</tr>";
}

The left join will return values that are in table1 even if there is no corresponding value in table2 but will return NULL for any columns selected from table2.

robjingram
  • 306
  • 1
  • 7