×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

If primary key in array availble but no foreign key in many array echo a comment

If primary key in array availble but no foreign key in many array echo a comment

If primary key in array availble but no foreign key in many array echo a comment

(OP)
I have 2 arrays that have a one to many relationship. The one side array has the meeting information and the many sided array contains the handout links. The meeting info is loaded before the handouts, which are loaded days before. The primary and foreign key is 'meetdate'. As you can see there are no handouts available for "meetdate"=>"2019-08-28".

I display the meeting info in a html table. My difficulty is showing the user if no handouts are available then leave the message "No handouts available at this time".

Here is my code. The code between the commented section is where I am having difficulty. I have tried different if and elseif combinations without success. My last attempt below was to check if 'meetdate' exists in one array but not the other. As

CODE --> PHP

<?php

    $meetinfo_array = array(
        array("meetdate"=>"2019-06-18","topic"=>"Hypertension","speaker"=>"Bob"),
        array("meetdate"=>"2019-07-10","topic"=>"Myocardial Infarction","speaker"=>"Jane"),
        array("meetdate"=>"2019-08-28","topic"=>"Cancer","speaker"=>"Pete")
        );
        
    $handout_array = array(
        array("meetdate"=>"2019-06-18","type"=>"Handout 6 slides per page","filename"=>"htn_6.pdf"),
        array("meetdate"=>"2019-06-18","type"=>"References","filename"=>"htn_ref.pdf"),
        array("meetdate"=>"2019-06-18","type"=>"Appendix","filename"=>"htn_app.pdf"),
        array("meetdate"=>"2019-07-10","type"=>"Handout 6 slides per page","filename"=>"mi_6.pdf"), 
        );
        
    echo '<table>';
        foreach($meetinfo_array as $result){
            echo '<tr>';
            echo '<td>'. "Date: " .date('F d, Y', strtotime($result['meetdate'])). '</td>'; 
            $meetdate = $result['meetdate']; 
            echo '</tr>';                    
            echo '<tr>';
                echo '<td>'. "Topic: " .$result['topic']. '</td>';
            echo '</tr>';
            echo '<tr>';
                echo '<td>'. "Speaker: " .$result['speaker']. '</td>';
            echo '</tr>';
                foreach($handout_array as $row){
                    if ($row['meetdate'] === $meetdate) {  
                        echo '<tr>';
                            echo '<td>'.'<a href="../../handouts/' . $row['filename']. '">' . $row['type'] . '</a>' . '</td>';
                        echo '</tr>';
                    }
/** ************************************
                    elseif ((in_array($meetdate, $meetinfo_array, true) && (!in_array($meetdate, $handout_array, true)))) {
                        echo '<tr>'; 
                            echo '<td>' ."Handouts not available at this time.". '</td>';
                        echo '</tr>';   
                    }
***************************************/
                }
            echo '<tr>'; 
            echo '<td>' ."&nbsp". '</td>';
            echo '</tr>';
        }
    echo '</table>';
?> 

Any help or suggestions are appreciated.

RE: If primary key in array availble but no foreign key in many array echo a comment

Since your echoing out on the fly, it prevents you from checking whether there's a matching value or not.

To maintain your logic you can add a control variable, and act on it after the foreach loop is done.

CODE

$control_var=false;
  foreach($handout_array as $row){
                    if ($row['meetdate'] === $meetdate) {  
$control_var=true;
echo '<tr>'; echo '<td>'.'<a href="../../handouts/' . $row['filename']. '">' . $row['type'] . '</a>' . '</td>'; echo '</tr>'; } } if($control_var==false) {
echo '<tr>'; echo '<td>' ."Handouts not available at this time.". '</td>'; echo '</tr>';
}
If the control variable remains false, it means it never entered your If statement, and you can assume no handouts where found nor echoed out, and you can echo out the "no handouts..." message.

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: If primary key in array availble but no foreign key in many array echo a comment

(OP)
Works perfectly. Thank you for your assistance.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close