2017-02-17 11 views
1

リストに分類されたPDFファイルのリストがあります。だから私は最初に私のページ上のカテゴリのタイトルを表示するすべてのカテゴリをループし、そのループ内で私は最終的、ちょうどテストの目的のために(pdfファイルのURLをループループ内のループが間違ったデータを表示する

Terms: 
PDF1.pdf 
PDF2.pdf 

Conditions: 
PDF1.pdf 
PDF3.pdf 

:このように

コードはアンカータグなどを取得します)。

問題は、私のコードがすべてのタイトルのすべてのPDFファイルを表示していることです。それらに属するPDFファイルだけではありません。私は間違って何をしていますか?ループごとに$attachを空にする必要がありますか?

マイコード:

foreach($manualcr as $mantitle){ 
    if($mantitle['id'] != ''){ 
    $attachoverzicht .= '<h3>'.$mantitle['title'].'</h3>'; 
    foreach($manucr as $attach){ 
     if($attach['id'] != ''){ 
     $attachoverzicht .= '<li>'.$attach['url'].'</li>'; 
     } 
    } 
    } 
} 
echo $attachoverzicht; 

$manualcrは、すべてのカテゴリのタイトルが含まれているとmanucrは、すべてのPDFデータが含まれています。

$manualcrのVaRのダンプ結果はここ

array(3) { 
    [0]=> 
    array(30) { 
    ["id"]=> 
    string(3) "182" 
    ["asset_id"]=> 
    string(3) "299" 
    ["title"]=> 
    string(18) "Terms & Conditions" 
    ["alias"]=> 
    string(16) "terms-conditions" 
    ["introtext"]=> 
    string(0) "" 
    ["fulltext"]=> 
    string(0) "" 
    ["state"]=> 
    string(1) "1" 
    ["catid"]=> 
    string(2) "42" 
    ["created"]=> 
    string(19) "2017-02-16 13:54:45" 
    ["created_by"]=> 
    string(3) "360" 
    ["created_by_alias"]=> 
    string(0) "" 
    ["modified"]=> 
    string(19) "2017-02-16 13:56:43" 
    ["modified_by"]=> 
    string(3) "360" 
    ["checked_out"]=> 
    string(1) "0" 
    ["checked_out_time"]=> 
    string(19) "0000-00-00 00:00:00" 
    ["publish_up"]=> 
    string(19) "2017-02-16 13:54:45" 
    ["publish_down"]=> 
    string(19) "0000-00-00 00:00:00" 
    ["images"]=> 
    string(173) "{"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}" 
    ["urls"]=> 
    string(121) "{"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}" 
    ["attribs"]=> 
    string(593) "{"show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_layout":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}" 
    ["version"]=> 
    string(1) "2" 
    ["ordering"]=> 
    string(1) "0" 
    ["metakey"]=> 
    string(0) "" 
    ["metadesc"]=> 
    string(0) "" 
    ["access"]=> 
    string(1) "1" 
    ["hits"]=> 
    string(1) "0" 
    ["metadata"]=> 
    string(53) "{"robots":"","author":"","rights":"","xreference":""}" 
    ["featured"]=> 
    string(1) "0" 
    ["language"]=> 
    string(1) "*" 
    ["xreference"]=> 
    string(0) "" 
    } 
    [1]=> 
    array(30) { 
    ["id"]=> 
    string(3) "181" 
    ["asset_id"]=> 
    string(3) "298" 
    ["title"]=> 
    string(8) "Shackles" 
    ["alias"]=> 
    string(8) "shackles" 
    ["introtext"]=> 
    string(0) "" 
    ["fulltext"]=> 
    string(0) "" 
    ["state"]=> 
    string(1) "1" 
    ["catid"]=> 
    string(2) "42" 
    ["created"]=> 
    string(19) "2017-02-16 13:54:31" 
    ["created_by"]=> 
    string(3) "360" 
    ["created_by_alias"]=> 
    string(0) "" 
    ["modified"]=> 
    string(19) "2017-02-16 13:57:00" 
    ["modified_by"]=> 
    string(3) "360" 
    ["checked_out"]=> 
    string(1) "0" 
    ["checked_out_time"]=> 
    string(19) "0000-00-00 00:00:00" 
    ["publish_up"]=> 
    string(19) "2017-02-16 13:54:31" 
    ["publish_down"]=> 
    string(19) "0000-00-00 00:00:00" 
    ["images"]=> 
    string(173) "{"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}" 
    ["urls"]=> 
    string(121) "{"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}" 
    ["attribs"]=> 
    string(593) "{"show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_layout":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}" 
    ["version"]=> 
    string(1) "2" 
    ["ordering"]=> 
    string(1) "1" 
    ["metakey"]=> 
    string(0) "" 
    ["metadesc"]=> 
    string(0) "" 
    ["access"]=> 
    string(1) "1" 
    ["hits"]=> 
    string(1) "0" 
    ["metadata"]=> 
    string(53) "{"robots":"","author":"","rights":"","xreference":""}" 
    ["featured"]=> 
    string(1) "0" 
    ["language"]=> 
    string(1) "*" 
    ["xreference"]=> 
    string(0) "" 
    } 
    [2]=> 
    NULL 
} 

とあなたのvar_dump出力から$manucr

array(4) { 
    [0]=> 
    array(26) { 
    ["id"]=> 
    string(1) "1" 
    ["filename"]=> 
    string(13) "dummy_pdf.pdf" 
    ["filename_sys"]=> 
    string(71) "/home/website/public_html/cms/attachments/article/182/dummy_pdf.pdf" 
    ["file_type"]=> 
    string(15) "application/pdf" 
    ["file_size"]=> 
    string(5) "16555" 
    ["url"]=> 
    string(37) "attachments/article/182/dummy_pdf.pdf" 
    ["uri_type"]=> 
    string(4) "file" 
    ["url_valid"]=> 
    string(1) "0" 
    ["url_relative"]=> 
    string(1) "0" 
    ["url_verify"]=> 
    string(1) "0" 
    ["display_name"]=> 
    string(9) "Test naam" 
    ["description"]=> 
    string(0) "" 
    ["icon_filename"]=> 
    string(7) "pdf.gif" 
    ["access"]=> 
    string(1) "2" 
    ["state"]=> 
    string(1) "0" 
    ["user_field_1"]=> 
    string(0) "" 
    ["user_field_2"]=> 
    string(0) "" 
    ["user_field_3"]=> 
    string(0) "" 
    ["parent_type"]=> 
    string(11) "com_content" 
    ["parent_entity"]=> 
    string(7) "article" 
    ["parent_id"]=> 
    string(3) "182" 
    ["created"]=> 
    string(19) "2017-02-16 13:56:03" 
    ["created_by"]=> 
    string(3) "360" 
    ["modified"]=> 
    string(19) "2017-02-16 13:56:03" 
    ["modified_by"]=> 
    string(3) "360" 
    ["download_count"]=> 
    string(1) "0" 
    } 
    [1]=> 
    array(26) { 
    ["id"]=> 
    string(1) "2" 
    ["filename"]=> 
    string(14) "dummy_pdf1.pdf" 
    ["filename_sys"]=> 
    string(72) "/home/website/public_html/cms/attachments/article/182/dummy_pdf1.pdf" 
    ["file_type"]=> 
    string(15) "application/pdf" 
    ["file_size"]=> 
    string(5) "16555" 
    ["url"]=> 
    string(38) "attachments/article/182/dummy_pdf1.pdf" 
    ["uri_type"]=> 
    string(4) "file" 
    ["url_valid"]=> 
    string(1) "0" 
    ["url_relative"]=> 
    string(1) "0" 
    ["url_verify"]=> 
    string(1) "0" 
    ["display_name"]=> 
    string(0) "" 
    ["description"]=> 
    string(0) "" 
    ["icon_filename"]=> 
    string(7) "pdf.gif" 
    ["access"]=> 
    string(1) "2" 
    ["state"]=> 
    string(1) "0" 
    ["user_field_1"]=> 
    string(0) "" 
    ["user_field_2"]=> 
    string(0) "" 
    ["user_field_3"]=> 
    string(0) "" 
    ["parent_type"]=> 
    string(11) "com_content" 
    ["parent_entity"]=> 
    string(7) "article" 
    ["parent_id"]=> 
    string(3) "182" 
    ["created"]=> 
    string(19) "2017-02-16 13:56:39" 
    ["created_by"]=> 
    string(3) "360" 
    ["modified"]=> 
    string(19) "2017-02-16 13:56:39" 
    ["modified_by"]=> 
    string(3) "360" 
    ["download_count"]=> 
    string(1) "0" 
    } 
    [2]=> 
    array(26) { 
    ["id"]=> 
    string(1) "3" 
    ["filename"]=> 
    string(13) "dummy_pdf.pdf" 
    ["filename_sys"]=> 
    string(71) "/home/website/public_html/cms/attachments/article/181/dummy_pdf.pdf" 
    ["file_type"]=> 
    string(15) "application/pdf" 
    ["file_size"]=> 
    string(5) "16555" 
    ["url"]=> 
    string(37) "attachments/article/181/dummy_pdf.pdf" 
    ["uri_type"]=> 
    string(4) "file" 
    ["url_valid"]=> 
    string(1) "0" 
    ["url_relative"]=> 
    string(1) "0" 
    ["url_verify"]=> 
    string(1) "0" 
    ["display_name"]=> 
    string(0) "" 
    ["description"]=> 
    string(0) "" 
    ["icon_filename"]=> 
    string(7) "pdf.gif" 
    ["access"]=> 
    string(1) "2" 
    ["state"]=> 
    string(1) "0" 
    ["user_field_1"]=> 
    string(0) "" 
    ["user_field_2"]=> 
    string(0) "" 
    ["user_field_3"]=> 
    string(0) "" 
    ["parent_type"]=> 
    string(11) "com_content" 
    ["parent_entity"]=> 
    string(7) "article" 
    ["parent_id"]=> 
    string(3) "181" 
    ["created"]=> 
    string(19) "2017-02-16 13:56:55" 
    ["created_by"]=> 
    string(3) "360" 
    ["modified"]=> 
    string(19) "2017-02-16 13:56:55" 
    ["modified_by"]=> 
    string(3) "360" 
    ["download_count"]=> 
    string(1) "0" 
    } 
    [3]=> 
    NULL 
} 
+0

あなたは 'のvar_dump($ manualcr)の出力を示すことができ;'と 'のvar_dump($のmanucr);'?あなたの '$ manucr'カテゴリがあなたの' $ manualcr'カテゴリと同じであるという条件をチェックしていないようです。 – roberto06

+0

はい私はそれらを私の質問に追加します。 @ roberto06 – twan

+0

データベースアクセス呼び出しをmysqli_fetch_assoc()に変更するか、PDOが 'PDO :: FETCH_ASSOC'を使ってフェッチするようにして、配列には各列データが1つしか存在しないようにしてください。それはあなたのコードのメモリ使用量も低下させるでしょう – RiggsFolly

答えて

3

の結果であり、私はあなたの対応するフィールドが$mantitle['id']$attach['parent_id']あると仮定します。あなたのような、あなたの第二のループでこれをチェックする必要があり

foreach($manualcr as $mantitle){ 
    if($mantitle['id'] != ''){ 
    $attachoverzicht .= '<h3>'.$mantitle['title'].'</h3>'; 
    foreach($manucr as $attach){ 
     if($attach['id'] != '' && $attach['parent_id'] == $mantitle['id']){ // Check for condition added here 
     $attachoverzicht .= '<li>'.$attach['url'].'</li>'; 
     } 
    } 
    } 
} 
echo $attachoverzicht; 
+0

ありがとう、完璧な意味があります。 – twan

関連する問題