www.webdeveloper.com
Results 1 to 3 of 3

Thread: need help in array_key_exists

  1. #1
    Join Date
    Oct 2011
    Posts
    357

    need help in array_key_exists

    Hi,

    I have report form created that consist of reject_type per process and rejects per compound_type.
    Now, I have a problem in displaying the total rejects per reject_type.

    I ued array_key_exists and I'm not too familiar on that syntax.
    here is my code:

    PHP Code:
    <?php 
    ob_start
    (); 
    include 
    "connection.php"

    $id "30";

    if(isset(
    $id)){ 
      
        
    $Reports = array();     
        
    $dates = array();  
        
    $process_names = array(); 
        
    $reject_types = array(); 
        
    $compound_types = array(); 
        
    $rejects = array(); 
        
    $reject_total = array();
        
    $sql "SELECT  
        r.reject_date,  
        r.reject,  
        r.process_id,  
        r.reject_type,  
        r.compound_type,  
        p.process_name  
        FROM op_reject AS r  
        JOIN process_list AS p  
        ON (p.process_id = r.process_id) 
        WHERE WEEK(reject_date)+1= '
    $id
        ORDER BY p.process_id, r.reject_date, r.compound_type ASC"
    ;  
        
    $res mysql_query($sql);             
        while(
    $row mysql_fetch_assoc($res)){ 
            
    //Would have been nice to work off this single array, but found it difficult so I built sub-arrays below.                     
            
    $Reports[$row['reject_date']][$row['process_name']][$row['reject_type']][$row['compound_type']][]  = $row['reject']; 
             
            
    //build some unique sub arrays for displaying data  
            
    if (!in_array($row['reject_date'],$dates)){ 
                
    $dates[] = $row['reject_date']; 
            } 
            if (!
    in_array($row['process_name'],$process_names)){ 
                
    $process_names[] = $row['process_name']; 
            }     
            if (
    array_key_exists($row['process_name'],$compound_types) && !in_array($row['compound_type'],$compound_types[$row['process_name']]) ||  
            !
    array_key_exists($row['process_name'],$compound_types)){                                                                                                                                                
                
    $compound_types[$row['process_name']][] = $row['compound_type']; 
            }     
                
    $reject_types[$row['reject_date']][$row['process_name']][] = $row['reject_type']; 
                
    $rejects[$row['reject_date']][$row['process_name']][$row['reject_type']][$row['compound_type']] = $row['reject']; 
              
        } 
        
        
    //----query for total rejec
        
        
    $sql_total "SELECT  
        r.reject_date,  
        SUM(r.reject) AS reject_total,  
        r.process_id,  
        r.reject_type,   
        p.process_name  
        FROM op_reject AS r  
        JOIN process_list AS p  
        ON (p.process_id = r.process_id) 
        WHERE WEEK(reject_date)+1= '
    $id'
       GROUP BY r.reject_type 
        ORDER BY r.reject_date, reject_type, p.process_id  ASC"
    ;
        
        
    $res_total mysql_query($sql_total);
        
         while(
    $row_total mysql_fetch_assoc($res_total)){ 
            
    $Reports[$row_total['reject_date']][$row_total['process_name']][$row_total['reject_type']]  = $row_total['reject_total']; 
             
            
    //build some unique sub arrays for displaying data  

                
    $rejects_total[$row_total['reject_date']][$row_total['process_name']][$row_total['reject_type']] = $row_total['reject_total']; 
              
        } 
         

    ////Build display data BEFORE output to browser//// 
    $display "<table border=1 cellpadding=2 cellspacing=0> 
            <thead> 
                <tr> 
                    <th>Compound</th>\r"

                    
    //I'll keep your id identifier in place                 
                    
    $i=1;                 
                    foreach(
    $dates as $date){ 
                        
    $display .= "<th id=col".$i." colspan=\"8\">$date</th>\r"
                        
    $i++; 
                    } 
                
    $display .= "</tr> 
                </thead>"

                    
    $datecnts = array();     
                    foreach(
    $process_names as $process_name){                 
                        
    $display .= "<tr>\r";     
                            
    $display .= "<td>$process_name</td>\r"
                                         
                        foreach(
    $dates as $date){ 
                            if (
    array_key_exists($process_name,$Reports[$date])){ 
                                
    $datecnts[$date] = count($Reports[$date][$process_name]); 
                                
    $d=0;                     
                                foreach(
    $Reports[$date][$process_name] as $reject_type => $arry){ 
                                    
    $display .= "<td>$reject_type</td>\r";     
                                    
    $d++;                             
                                }                             
                            }else{                             
                                for(
    $c=0;$c<=$d;$c++){ 
                                    
    $display .= "<td>&nbsp;</td>\r"
                                } 
                            }                                                                         
                        }                      
                
    $display .= "</tr>\r"
                
    // Even with sub-arrays it get quite messy checking for all the keys and attempting to fill empty table cells  
                        
    foreach($compound_types[$process_name] as $compound_type){                 
                            
    $display .= "<tr>\r";     
                                
    $display .= "<td>$compound_type</td>\r";                    
                            foreach(
    $dates as $date){ 
                                if (
    array_key_exists($date,$rejects) && array_key_exists($process_name,$rejects[$date])){ 
                                    foreach(
    $rejects[$date][$process_name] as $reject_type => $arry){ 
                                        if (
    array_key_exists($date,$rejects) && array_key_exists($process_name,$rejects[$date]) && !array_key_exists($reject_type,$rejects[$date][$process_name])){ 
                                            
    $display .= "<td>&nbsp;</td>\r"
                                        }elseif (
    array_key_exists($date,$rejects) && array_key_exists($process_name,$rejects[$date]) && array_key_exists($reject_type,$rejects[$date][$process_name]) && !array_key_exists($compound_type,$rejects[$date][$process_name][$reject_type])){ 
                                            
    $display .= "<td>&nbsp;</td>\r"
                                        }elseif (
    array_key_exists($date,$rejects) && array_key_exists($process_name,$rejects[$date]) && array_key_exists($reject_type,$rejects[$date][$process_name]) && array_key_exists($compound_type,$rejects[$date][$process_name][$reject_type])){ 
                                            
    $display .= "<td>{$rejects[$date][$process_name][$reject_type][$compound_type]}</td>\r"
                                        }else{ 
                                            
    $display .= "<td>&nbsp;</td>\r"
                                        } 
                                    } 
                                }else{ 
                                
    $display .= "<td colspan=\"{$datecnts[$date]}\">&nbsp;</td>\r"
                                }                                                         
                            }
                            
                                                                                                                      
                        }  
               
    //---total rejects per process and per reject type---//
                        
                            
    $display .= "<tr>"
                            
    $display .= "<td>Total</td>";
                     
                            foreach(
    $dates as $date1){ 
                                if (
    array_key_exists($date1,$rejects_total) && array_key_exists($process_name,$rejects_total[$date1])){ 
                                    foreach(
    $rejects_total[$date1][$process_name] as $reject_type => $arry){ 
                                        if (
    array_key_exists($date1,$rejects_total) && array_key_exists($process_name,$rejects_total[$date1]) && !array_key_exists($reject_type,$rejects_total[$date1][$process_name])){ 
                                            
    $display .= "<td>&nbsp;</td>\r"
                                        }elseif (
    array_key_exists($date1,$rejects_total) && array_key_exists($process_name,$rejects_total[$date1]) && !array_key_exists($reject_type,$rejects_total[$date1][$process_name])){ 
                                            
    $display .= "<td>&nbsp;</td>\r"
                                        }elseif (
    array_key_exists($date1,$rejects_total) && array_key_exists($process_name,$rejects_total[$date1]) && array_key_exists($reject_type,$rejects_total[$date1][$process_name])){ 
                                            
    $display .= "<td>{$rejects_total[$date1][$process_name][$reject_type][$compound_type]}</td>\r"
                                        }else{ 
                                            
    $display .= "<td>&nbsp;</td>\r"
                                        } 
                                    } 
                                }else{ 
                                
    $display .= "<td colspan=\"{$datecnts[$date1]}\">&nbsp;</td>\r"
                                }                                                         
                            }
                            
                            
    $display .= "</tr>";  
                    } 


                    
        
    $display .= "</table>"


    ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
        <html xmlns="http://www.w3.org/1999/xhtml"> 
        <head> 
        <title>Operator's Output and Reject</title> 
        </head> 
        <body> 
            <?php 
            
    if(isset($display)){ 
                echo 
    "$display"
            } 
            
    ?> 
        </body> 
    </html>
    I attached my database and also the screenshots of the output display of this code.

    As you can see the display total is wrong..it should be sum all reject per reject_type.

    The reject type display list is based on what is the reject_date.


    I hope somebody can help me. Thank you so much.
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,902
    If one is "not too familiar on that syntax", may I suggest that one Get familiar with the manual? It is absolutely great at passing along syntax.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,864
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14066 seconds
  • Memory Usage 2,977KB
  • Queries Executed 16 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (2)postbit_attachment
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • fetch_postattach_query
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_attachment
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates