循环显示错误数据的循环

时间:2017-02-17 09:38:50

标签: php loops pdf

我有一份分类为列表的PDF文件列表。

像这样:

Terms:
PDF1.pdf
PDF2.pdf

Conditions:
PDF1.pdf
PDF3.pdf

所以我首先循环所有类别以在我的页面上显示类别的标题,并在该循环中我循环pdf文件URL(仅用于测试目的,最终代码将获得锚标记等)。

问题是我的代码显示所有标题上的所有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
}

以及$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
}

1 个答案:

答案 0 :(得分:3)

var_dump输出中,我假设您的相应字段为$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;