duanci5913 2015-12-22 08:39
浏览 51
已采纳

Foreach显示一个额外的空回声场

The following foreach loop shows an extra empty field. It has the html markup like in the foreach, but doesn't load anything from the database. There is no extra empty field in the database, I checked.

   //  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;

Here is an image with the last working field and one extra. Where does the extra empty field come from?

This is part of the output of the array, as you can see array value '6' is empty.

            [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条回答 默认 最新

  • duangelin7513 2015-12-22 12:57
    关注

    The line while ($contentcr[] = $contentcon->fetch_array()); will keep making trips to the database result and put something into the array, regardless.

    What you then have is the last array index in $contentcr effectively being FALSE, as it can no longer gather a result set. Something is still returned, but not a result set.

    Your array then looks something like:

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

    Which is why you always end up with an empty field.

    You can better get round this by replacing your foreach($contentcr as $content) line with while ($content = $contentcon->fetch_array()). This will stop when $contentcon->fetch_array() is false, and won't give you an empty field

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题