Taking data from two tables with PDO and printing it to the table with foreach loop

Viewed 32

I have two tables: "tbl_users" and "deposited". I want to get data from these two tables. I have to print this data that I will pull with foreach. I have columns "users_refcode" and "subs_references" in my table "tbl_users". The logged in user should be able to see their subreferences on their own page. The subreferences come to the table, but they come back as many times as the number of data in the "deposited" table. This results in a long table with repetitive data.

Function:

public function subs_usrefcode(){

$dep_sql = "SELECT * FROM deposited"; 
$dep_cek_stmt = $this->db->pdo->prepare($dep_sql);
$dep_cek_stmt->execute();
$dep_cek_return=$dep_cek_stmt->fetchAll(PDO::FETCH_ASSOC);

$deposited_refcode = isset($deposited_refcode['deposited_refcode']);

$cek_user_sql = "SELECT *, subs_references FROM tbl_users, deposited";
$cek_user_stmt = $this->db->pdo->prepare($cek_user_sql);
$cek_user_stmt->execute();
$cek_user_return=$cek_user_stmt->fetchAll();

if ($cek_user_return){

  $subs_references = isset($cek_user_return['subs_references']);
  $deposited_refcode = isset($deposited_refcode['deposited_refcode']);
  $userfererencescode = isset($cek_user_return['user_refcode']);

  if ($userfererencescode == $subs_references){
      return $cek_user_return;
  }else{
    echo 'hata';
  }}
}

This is my page where I call the foreach loop. My purpose here: To list the users who entered with the reference code of the logged in user. But to fetch the data saved in the "deposited" table of these users.

<section id="amount" style="padding: 0px 0px;">
<div class="col-lg-6 col-md-12 col-xs-12 align-self-center wow fadeIn" data-animate="fadeIn" data-duration="1.0s" data-delay="0.4s">
    <div class="container">
        <div class="table-responsive py-5"> 
            <div class="container" amnt="dhis">Deposit History</div>
            <table class="table table-bordered table-hover">
                <thead class="thead-dark">
                    <tr>
                      <th scope="col">Amount</th>
                      <th scope="col">Time Select</th>
                      <th scope="col">Deposited TXID</th>
                      <th scope="col">Deposited Date</th>
                  </tr>
                </thead>
                <tbody>
                <?php
                $selectAllUserData = $users->selectAllUserData();
                
                $subs_usrefcode = $users->subs_usrefcode();
                  $i = 0;
                  foreach ($subs_usrefcode as $value) {
                    $i++;
                    
                    if($user_refcode === $value['subs_references']){
                    ?>

                    <tr>
                        <td id="card"><?php echo $value['deposited_amount']; ?> </td>
                        <td><?php echo $value['username']; ?> </td>
                        <td><?php echo $value['deposited_txid']; ?> </td>
                        <td><?php echo $value['deposited_date']; ?> </td>
                    </tr>

                <?php } } ?>
                    <?php 
                        function percentcalc($sayi,$yuzde,$per){ 
                        return ($sayi*$yuzde)/100+$per;
                        }
                    ?>
                    <tr>
                       <td></td>
                       <td class="text-right" tot="tot">Total Earnings</td>
                       <td id="total"></td>
                       <td></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

Users who entered with the reference code of the logged in user are listed. But the data they entered into the "deposited" table is not coming. Moreover, users repeat as many times as the number of data in the "deposited" table. I look forward to your help in this matter. Thank you

enter image description here

0 Answers
Related