Foreach显示一个额外的空回显字段

时间:2015-12-22 08:39:08

标签: php foreach

以下foreach循环显示一个额外的空字段。它具有类似于foreach的html标记,但不会从数据库加载任何内容。数据库中没有多余的空字段,我查了一下。

   //  content
    $content            = "SELECT * FROM `lb_content` WHERE catid = 13";
    $contentcon         = $conn->query($content);
    $contentcr          = array();
    while ($contentcr[] = $contentcon->fetch_array());

    foreach($contentcr as $content) 
    {

        $article_images = $content['images']; // Get image parameters of the article

        $pictures = json_decode($article_images); // Split the parameters apart

        $contentje .= '<li class="job_listing">
        <a href="http://www.website.nl/_extern/website/content.php?alias='.$content['alias'].'">
            <div class="location job_img">
                <img name="images" src="/_extern/website/cms/'.$pictures->{'image_intro'}.'" alt="" class="company_logo">
            </div>
            <div class="location boldfont">
                '.$content['title'].'
            </div>
            <div class="location specificaties">
            '.$content['introtext'].'
            </div>
            <div class="rating location">
                <div class="rating-stars">
                    <i class="fa fa-star"></i>
                    <i class="fa fa-star"></i>
                    <i class="fa fa-star"></i>
                    <i class="fa fa-star"></i>
                    <i class="fa fa-star"></i>
                </div>
            </div>
            <div class="location prijs">
                '.$content['metakey'].'
                <br>
                <div class="cursief">Vanaf 2 weken</div>
            </div>
            <div class="location">
                <button type="submit" class="btn btn-primary buttonblock" onClick="location.href="http://www.website.nl/_extern/website/content.php?page="'.$content['alias'].'">Nu huren</button>
            </div>
        </a>
    </li>'; // Here
    }

echo $contentje;

这是一张带有最后一个工作区和一个额外工作区的图像。额外的空场来自哪里?

这是数组输出的一部分,因为您可以看到数组值“6”为空。

            [5] => 

De ideale tijdelijke oplossing voor o.a.:
 Grotere kantoren
 Middelgrote winkels
 Serverruimtes
 Transformator ruimtes
 Feest tenten
 Meet- en regelkamers
 Hotels
 Restaurants
 Conferentieruimtes
 Laboratoria




Koelcapaciteit
    : 13,0 KW


Voeding
    : 230 V / 1 Fase / 50 Hz


Opgenomen vermogen per set
    : 4,82 kW


Nominale stroomsterkte
    : 3 x 7,6 A


Afzekerwaarde
    : 3 x 20 A T


Max. Leidinglengte
    : 40 m


Max. Hoogteverschil
    : 25 m


Koelleiding diameters
    : 3/8 - 3/4 inch


Voorzien van winterregeling
    : (-15 °C)



Binnenunit specificaties:




Luchtvolume
    : 900 / 1200 m3/h


Benodigde condensaansluiting
    : met ingebouwde condenspomp


Geluidsniveau (laag / hoog)*
    : 42 / 51 dB(A)


Gewicht
    : 50 kg


Afmetingen unit (HxBxD)
    : 1750 x 540 x 300 mm



Buitenunit specificaties:




Geluidsniveau
    : 58 dB(A)


Gewicht
    : 75 kg


Afmetingen (HxBxD)
    : 950 x 840 x 412 mm


Voeding
    : voorzien van een CEE stekker, 32A, 5-polig



            [fulltext] => 

De ideale tijdelijke oplossing voor o.a.:
 Grotere kantoren
 Middelgrote winkels
 Serverruimtes
 Transformator ruimtes
 Feest tenten
 Meet- en regelkamers
 Hotels
 Restaurants
 Conferentieruimtes
 Laboratoria




Koelcapaciteit
    : 13,0 KW


Voeding
    : 230 V / 1 Fase / 50 Hz


Opgenomen vermogen per set
    : 4,82 kW


Nominale stroomsterkte
    : 3 x 7,6 A


Afzekerwaarde
    : 3 x 20 A T


Max. Leidinglengte
    : 40 m


Max. Hoogteverschil
    : 25 m


Koelleiding diameters
    : 3/8 - 3/4 inch


Voorzien van winterregeling
    : (-15 °C)



Binnenunit specificaties:




Luchtvolume
    : 900 / 1200 m3/h


Benodigde condensaansluiting
    : met ingebouwde condenspomp


Geluidsniveau (laag / hoog)*
    : 42 / 51 dB(A)


Gewicht
    : 50 kg


Afmetingen unit (HxBxD)
    : 1750 x 540 x 300 mm



Buitenunit specificaties:




Geluidsniveau
    : 58 dB(A)


Gewicht
    : 75 kg


Afmetingen (HxBxD)
    : 950 x 840 x 412 mm


Voeding
    : voorzien van een CEE stekker, 32A, 5-polig



            [6] => 1
            [state] => 1
            [7] => 13
            [catid] => 13
            [8] => 2015-11-30 10:28:23
            [created] => 2015-11-30 10:28:23
            [9] => 361
            [created_by] => 361
            [10] => 
            [created_by_alias] => 
            [11] => 2015-12-08 13:14:13
            [modified] => 2015-12-08 13:14:13
            [12] => 361
            [modified_by] => 361
            [13] => 0
            [checked_out] => 0
            [14] => 0000-00-00 00:00:00
            [checked_out_time] => 0000-00-00 00:00:00
            [15] => 2015-11-30 10:28:23
            [publish_up] => 2015-11-30 10:28:23
            [16] => 0000-00-00 00:00:00
            [publish_down] => 0000-00-00 00:00:00
            [17] => {"image_intro":"images\/website\/AELIA_30_45.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [images] => {"image_intro":"images\/website\/AELIA_30_45.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [18] => {"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}
            [urls] => {"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}
            [19] => {"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":""}
            [attribs] => {"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":""}
            [20] => 14
            [version] => 14
            [21] => 0
            [ordering] => 0
            [22] => € 195
            [metakey] => € 195
            [23] => 
            [metadesc] => 
            [24] => 1
            [access] => 1
            [25] => 0
            [hits] => 0
            [26] => {"robots":"","author":"","rights":"","xreference":""}
            [metadata] => {"robots":"","author":"","rights":"","xreference":""}
            [27] => 0
            [featured] => 0
            [28] => *
            [language] => *
            [29] => 
            [xreference] => 
        )

    [5] => Array
        (
            [0] => 16
            [id] => 16
            [1] => 84
            [asset_id] => 84
            [2] => Aelia 55 (AL-55)
            [title] => Aelia 55 (AL-55)
            [3] => aelia-55-al-55
            [alias] => aelia-55-al-55
            [4] => 



Type
    : AL-55


Koelvermogen
    : 18,0 Kw


Afvoervermogen
    : Zuilkoeling staand model


Ruimtes tot
    : 520m3




            [introtext] => 



Type
    : AL-55


Koelvermogen
    : 18,0 Kw


Afvoervermogen
    : Zuilkoeling staand model


Ruimtes tot
    : 520m3




            [5] => 



            [fulltext] => 



            [6] => 1
            [state] => 1
            [7] => 13
            [catid] => 13
            [8] => 2015-11-30 10:38:11
            [created] => 2015-11-30 10:38:11
            [9] => 361
            [created_by] => 361
            [10] => 
            [created_by_alias] => 
            [11] => 2015-12-09 14:07:30
            [modified] => 2015-12-09 14:07:30
            [12] => 361
            [modified_by] => 361
            [13] => 361
            [checked_out] => 361
            [14] => 2015-12-09 14:07:30
            [checked_out_time] => 2015-12-09 14:07:30
            [15] => 2015-11-30 10:38:11
            [publish_up] => 2015-11-30 10:38:11
            [16] => 0000-00-00 00:00:00
            [publish_down] => 0000-00-00 00:00:00
            [17] => {"image_intro":"images\/website\/AELIA_55.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [images] => {"image_intro":"images\/website\/AELIA_55.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
            [18] => {"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}
            [urls] => {"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}
            [19] => {"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":""}
            [attribs] => {"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":""}
            [20] => 8
            [version] => 8
            [21] => 0
            [ordering] => 0
            [22] => € 235
            [metakey] => € 235
            [23] => 
            [metadesc] => 
            [24] => 1
            [access] => 1
            [25] => 0
            [hits] => 0
            [26] => {"robots":"","author":"","rights":"","xreference":""}
            [metadata] => {"robots":"","author":"","rights":"","xreference":""}
            [27] => 0
            [featured] => 0
            [28] => *
            [language] => *
            [29] => 
            [xreference] => 
        )

    [6] => 

1 个答案:

答案 0 :(得分:1)

无论如何,行while ($contentcr[] = $contentcon->fetch_array());将继续访问数据库结果并将某些内容放入数组中。

您拥有的是$contentcr中的最后一个数组索引实际为FALSE,因为它无法再收集结果集。仍然会返回一些内容,但不会返回结果集。

您的数组看起来像:

$contentcr[0] = array(...);
$contentcr[1] = array(...);
$contentcr[n-1] = array(...);
$contentcr[n] = FALSE;

这就是为什么你总是以空场结束。

您可以通过将foreach($contentcr as $content)行替换为while ($content = $contentcon->fetch_array())来更好地解决此问题。这将在$contentcon->fetch_array()为假时停止,并且不会为您提供空字段