代码之家  ›  专栏  ›  技术社区  ›  dmgig

php foreach在随机位置失败,未给出错误,仅在某些查询上失败。

  •  0
  • dmgig  · 技术社区  · 15 年前

    我完全无法解决这个问题。

    我只是用一个参数运行一个简单的查询,将结果放入一个数组中,然后在数组中循环以在表中显示信息。

    我会在结尾处发布代码,虽然这有点密集,但大多数情况下我想知道什么会导致PHP脚本死而不出错?

    我要注意的是,在将mysql信息放入数组后,我会在foreach循环之前将其打印出来,所有信息都会显示出来。

    我打开了错误报告功能。

    该错误只发生在具有某些参数的查询上,而不是其他参数的查询上。

    错误并不总是发生在同一个地方,但它总是发生。当它遍历记录时,在将一些记录绘制到表中之后,它会随机停止。

    我不认为这可能是我的函数的问题,因为它会给出一个错误。

    不管怎样,我希望我在做一些愚蠢的疏忽。如有任何反馈,我将不胜感激。

    <?php if(isset($_SESSION['submit']) && $_SESSION['search_param'] != ''){ ?>
    
    <br />
    <br />
    
    <table id="results_box" cellpadding="0" cellspacing="0">
    
    <?php $bg = 'alt2'; ?>
    
    <?php //echo '<pre>',print_r($results['rows']),'</pre>'; ?>
    
    <?php foreach( $results['rows'] as $row){ ?>
    
        <?php $podcasts = getRelatedPodcasts('item',$row['record_id']); ?>
        <?php $images = getRelatedImages('item',$row['record_id']); ?>
        <?php $main_image = getAndShowMainImage('item',$row['record_id'],'mini'); ?>
        <?php $color_class = str_replace(' ','-',$row['category']); ?>
        <?php $people = getRelatedPeople('item',$row['record_id']); ?>
    
        <?php $bg = ($bg == 'alt2' ? 'alt1' : 'alt2'); ?>
    
        <tr class="<?php echo $bg; ?>" onClick="Link('index.php?page=entry&permalink=<?=$row['record_id']; ?>')">
        <td class="leftrows <?=$color_class?>">
        <?=$main_image?>
        </td>
        <td class="next-to-leftrows " width="25%">
        <div class="text-headroom">
        <font class="title-medium">
        <?php echo highlight($row['name_title'],$_SESSION['search_param']); ?>
        </font>
        <br />
        <span class="small">
        <?=highlight($row['city'],$_SESSION['search_param'])?>, <?=$row['state']?> 
        &bull; <?=highlight($row['category'],$_SESSION['search_param'])?>
        </span>
        </div>
        </td>
        <td class="rows" width="25%">
        <div class="text-headroom">
        <ul class="small">
        <?php 
        foreach($people['record_ids'] as $key => $person){ ?>
            <li><?=highlight(getPersonName($person,'FL'),$_SESSION['search_param'])?></li>
        <?php } ?>
        </ul>
        </div>
        </td>
        <td class="rows small" width="45%"><?php echo constrainLongText($row['remarks'],150); ?></td>
        <td class="rightrows small" align="right" width="25">
        <?php if($podcasts['count'] > 0){ ?>
            <img src="ui/images/headphones.png" />
        <?php } ?>
        </td>
        </tr>
    
    <?php }
    
    } // end if submit for results ?>
    
    </table>
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   pferate    15 年前

    你说没有错误,但你后来提到了一个错误。是“我桌子的其余部分不显示”还是类似的错误?

    您是否检查了输出源以查看其结束位置?

    可能有一些无效的字符被打印出来,这反过来可能会破坏HTML的其余部分。您可能需要研究HTML环境卫生(如果您不在其他地方)。试着看 htmlspecialchars()

    作为旁注,您的代码很难读取。你不需要使用 <?php ?> 如此之多,您可以将PHP代码包含在更少的集合中,而不是在每一行中都包含它们。

    你也可能希望与 <?= <?php echo . 就我个人而言,我远离速记版本,一些网络服务器不允许速记。

    编辑:这里是一个清理过的代码版本。每一个程序员都有自己的代码格式,所以用它来做你想做的…

    <?php
    if (isset($_SESSION['submit']) && (!empty($_SESSION['search_param'])) {
        echo <<<EOT
    <br />
    <br />
    <table id="results_box" cellpadding="0" cellspacing="0">
    EOT;
        $bg = 'alt2';
        //echo '<pre>',print_r($results['rows']),'</pre>';
        foreach ($results['rows'] as $row){
            $podcasts    = getRelatedPodcasts('item', $row['record_id']);
            $images      = getRelatedImages('item', $row['record_id']);
            $main_image  = getAndShowMainImage('item', $row['record_id'],'mini');
            $color_class = str_replace(' ', '-', $row['category']);
            $people      = getRelatedPeople('item', $row['record_id']);
            $bg          = ($bg == 'alt2' ? 'alt1' : 'alt2');
    
            $record_id            = $row['record_id'];
            $state                = $row['state'];
            $highlight_name_title = highlight($row['name_title'], $_SESSION['search_param']);
            $highlight_city       = highlight($row['city'], $_SESSION['search_param']);
            $highlight_category   = highlight($row['category'], $_SESSION['search_param']);
            echo <<<EOT
    
        <tr class="$bg" onClick="Link('index.php?page=entry&permalink=$record_id')">
            <td class="leftrows $color_class">
                $main_image
            </td>
            <td class="next-to-leftrows " width="25%">
                <div class="text-headroom">
                    <font class="title-medium">$highlight_name_title</font>
                    <br />
                    <span class="small">
                        $highlight_city, $state &bull; $highlight_category
                    </span>
                </div>
            </td>
            <td class="rows" width="25%">
                <div class="text-headroom">
                    <ul class="small">
    EOT;
                foreach ($people['record_ids'] as $key => $person) {
                    $highlight_person = highlight(getPersonName($person, 'FL'), $_SESSION['search_param']);
                    echo "<li>$highlight_person</li>";
                }
    
            $shortened_remarks = constrainLongText($row['remarks'], 150);
            echo <<<EOT
    
                    </ul>
                </div>
            </td>
            <td class="rows small" width="45%">$shortened_remarks</td>
            <td class="rightrows small" align="right" width="25">
    EOT;
            if($podcasts['count'] > 0){
                echo "<img src=\"ui/images/headphones.png\" />";
            }
            echo <<<EOT
    
            </td>
        </tr>
    EOT;
        }
    echo "</table>";
    } // end if submit for results
    ?>
    
        2
  •  0
  •   dmgig    15 年前

    我在其中一个函数中遇到了问题,但由于函数在另一个文件中,所以没有出现错误。不幸的是,我的主机很糟糕,我没有访问php.ini的权限,但是通过消除每个函数并从零开始,问题就变得明显了。谢谢你的指点。