DeclanFell
DeclanFell

Reputation: 11

PHP - Checks Date ID and inserts Data

I am looking for some help. Creating an attendance system.

I would like it to Fetch All ASSOC from 2 tables. If the datesID match up, then displays the data from that Attendance Table. If not, displays default data. It works with the first Row but breaks onwards. I've tried foreach but getting confused

$row = mysqli_fetch_assoc($result);
while ($row2 = mysqli_fetch_assoc($defaulttable)) {
    if ($row2['id'] === $row['date_id']) {
        echo '<tr> <
            td > '.$row2['
        date '].' < /td> <
            td > '.$row['
        status '].' < /td> <
            th scope = "row" > '.$row['
        notes '].' < /th> <
            td > <button data-toggle="modal" class="btn btn-primary" data-target="#myModalNorm">Edit</button> < /td>  <
            /tr>';
    } else {
        echo '<tr> <
            td > '.$row2['
        date '].' < /td> <
            td > None < /td> <
            th scope = "row" > N / A < /th> <
            td > <button data-toggle="modal" class="btn btn-primary" data-target="#myModalNorm">Edit</button> < /td>  <
            /tr>';
    }
}

Upvotes: 0

Views: 52

Answers (3)

Wyrone
Wyrone

Reputation: 92

You can try this:

$rows = mysqli_fetch_assoc($result);
$rows2 = mysqli_fetch_assoc($defaulttable)

foreach($rows as $row){

    $is_found = false;
    foreach($rows2 as $row2){

        if($row2['id'] == $row['date_id']){
            $is_found = true;
            echo 'Display attendance data'.'<br />'; //echo to see if it has matched an id
            //Display attendance table
        }
    }


    if($is_found == false){
        echo 'Display default'.'<br />'; // echo to see if its default
        //Display default
    }
}

Upvotes: 1

Sachin Maurya
Sachin Maurya

Reputation: 5

what is the main issue?

  • output is different then your expectation
  • getting any error? if yes, then please paste your error


if there is no error showing please do the following :
on top of the page (after <?php ) type this command

<?php
ini_set('display_errors','1');           // it will show you any error occurs in your current page

Upvotes: 0

Luis Colmenarez
Luis Colmenarez

Reputation: 14

One method can be: (although in the same way if $ row2 has fewer records than $ row, the cycle is closed before going through all the records)

$row = mysqli_fetch_assoc($result);
$row2 = mysqli_fetch_assoc($defaulttable);

if(count($row2) > 0){
    foreach($row2 as $r2){
        foreach($row as $r){
            if($r2['id'] === $r['id']){
    // // // // // // // // // //
    // Code +
    echo '<tr> <
            td > '.$row2['
        date '].' < /td> <
            td > '.$row['
        status '].' < /td> <
            th scope = "row" > '.$row['
        notes '].' < /th> <
            td > <button data-toggle="modal" class="btn btn-primary" data-target="#myModalNorm">Edit</button> < /td>  <
            /tr>';
    // // // // // // // // // //
            } else {
    // // // // // // // // // //
    // Code -
        echo '<tr> <
            td > '.$row2['
        date '].' < /td> <
            td > None < /td> <
            th scope = "row" > N / A < /th> <
            td > <button data-toggle="modal" class="btn btn-primary" data-target="#myModalNorm">Edit</button> < /td>  <
            /tr>';
    // // // // // // // // // //
            }
        }
    }
}

Upvotes: 0

Related Questions