Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

waubain

Technical User
Dec 13, 2011
200
US
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

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.

 
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:
[COLOR=#4E9A06]$control_var=false;[/color]
  foreach($handout_array as $row){
                    if ($row['meetdate'] === $meetdate) {  
[indent][/indent][indent][b][COLOR=#4E9A06]$control_var=true;[/color][/b][/indent]
                        echo '<tr>';
                            echo '<td>'.'<a href="../../handouts/' . $row['filename']. '">' . $row['type'] . '</a>' . '</td>';
                        echo '</tr>';
                    }
                }
[COLOR=#4E9A06]if($control_var==false)[/color]
{
[indent]echo '<tr>'; 
                            echo '<td>' ."Handouts not available at this time.". '</td>';
                        echo '</tr>';   [/indent]
}
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
 
Works perfectly. Thank you for your assistance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top