【问题标题】:short_open_tags displaying NULL values in while loopshort_open_tags 在 while 循环中显示 NULL 值
【发布时间】:2015-09-17 09:11:25
【问题描述】:

我有以下代码应该使用 oci_fetch_array() 函数来填充一些参数,以便在网站上显示新链接,但不是

" http://website.com/reporttest.php?type=1&mode=1&dis=MECH&unit=3&rfo=20 "

正在输出

http://website.com/reporttest.php?type=1&mode=&dis=&unit=3&rfo=20

注意“Mode”和“Dis”不存在。我确定我在 while 循环中做错了什么,但我对 PHP 不太熟悉,无法弄清楚为什么它会以这种方式执行。

<td valign="top" width=12%>
<?
$count = 0;
?>
        <h2>None<br>
        <table border=1 align="center">

<?
while ($count < $TotalB) {
    $row = oci_fetch_array($stid, OCI_NUM);
?>
    <tr>
        <td><?= trim($row[1]) ?></td><td><a href="http://website.com/reporttest.php?type=1&mode=<?= urlencode($row[2]) ?>&dis=<?= urlencode(trim($row[1])) ?><?= $endofurl ?>"><?= trim($row[0]) ?></a></td>
    </tr>
<?
    $count = $count + trim($row[0]);
}
?>
        </table>
</td>

行输出:

Array ( [0] => 1 [1] => [2] => ) 
Array ( [0] => 20 [1] => ELEC [2] => ) 
Array ( [0] => 1 [1] => EPCP [2] => )
Array ( [0] => 4 [1] => EPPG [2] => )
Array ( [0] => 13 [1] => I&C [2] => )
Array ( [0] => 43 [1] => MECH [2] => )
Array ( [0] => 15 [1] => MNTS [2] => )
Array ( [0] => 1 [1] => OPS [2] => )
Array ( [0] => 7 [1] => VLVT [2] => )
Array ( [0] => 1 [1] => WHSE [2] => ) 

$TotalB 在这里定义:

while ($row = oci_fetch_array($stid, OCI_NUM)) {
    if (trim($row[1]) == "") {$TotalB = $row[0];}
    else if (trim($row[1]) == 0) {$Total0 = $row[0];}
    else if (trim($row[1]) == 1) {$Total1 = $row[0];}
    else if (trim($row[1]) == 2) {$Total2 = $row[0];}
    else if (trim($row[1]) == 3) {$Total3 = $row[0];}
    else if (trim($row[1]) == 4) {$Total4 = $row[0];}
    else if (trim($row[1]) == 5) {$Total5 = $row[0];}
    else if (trim($row[1]) == 6) {$Total6 = $row[0];}
}

在这种情况下,print $TotalB 输出 106

【问题讨论】:

  • 您可以在 oci_fetch_array 之后使用 print_r($row) 来验证您的结果(来自 SQL 请求)。判断是sql pb还是php pb。
  • 我刚刚验证$row是正确的值,所以不是sql问题。
  • 请发print_r($row) 并发布内容
  • 输出如下: Array ( [0] => 1 [1] => [2] => ) Array ( [0] => 20 [1] => ELEC [2] => ) 数组 ( [0] => 1 [1] => EPCP [2] => ) 数组 ( [0] => 4 [1] => EPPG [2] => ) 数组 ( [0] => 13 [1] => I&C [2] => ) 阵列 ( [0] => 43 [1] => MECH [2] => ) 阵列 ( [0] => 15 [1] => MNTS [2] => ) 数组 ( [0] => 1 [1] => OPS [2] => ) 数组 ( [0] => 7 [1] => VLVT [2] => ) 数组 ( [0] => 1 [1] => WHSE [2] => )
  • ( [0] => 43 [1] => MECH [2] => ) 如您所见,MECH 条目的 [2] 为空...所以 PHP 似乎正确但不好数据:)

标签: php php-shorttags


【解决方案1】:

尝试用 html 摆脱内联 php,也不要忘记关闭那个 h2 标记。

<?php

$data = array(
    array ( 1, "",""),
    array ( 20, "ELEC", ""),
    Array ( 1, "EPCP", ""),
    Array ( 4, "EPPG", ""),
    Array ( 13, "I&C", ""),
    Array ( 43, "MECH", ""),
    Array ( 15, "MNTS", ""),
    Array ( 1, "OPS", ""),
    Array ( 7, "VLVT", ""),
    Array ( 1, "WHSE", "")
);

//print_r($data);

$endofurl = "#"; //if you don't set this you get a notice because you call this var, i assume it's the unit=3&rfo=20 bit

// mode will be empty because you set mode=$row[2] and that last key is always empty

foreach ($data as $row){
?>
<tr>
    <td><?= trim($row[1]) ?></td><td><a href="http:website.com/reporttest.php?type=1&mode=<?=urlencode($row[2])?>&dis=<?=urlencode(trim($row[1]))?><?=$endofurl?>"><?=trim($row[0])?></a></td>
</tr>

<?php } ?>

或者你可以尝试不使用内联 php,像这样

<?php
while ($count < $TotalB) {
    $row = oci_fetch_array($stid, OCI_NUM);
    print'
    <tr>
        <td>'.trim($row[1]).'</td><td><a href="http:website.com/reporttest.php?type=1&mode='.urlencode($row[2]).'&dis='.urlencode(trim($row[1])) . $endofurl.'">'.trim($row[0]).'</a></td>
    </tr>';

    $count = $count + trim($row[0]);
}
print '
    </table>
</td>';

?>

【讨论】:

  • 午休后我会试试这个,然后回复你!
  • 这非常有效,感谢您让更新 5 年的脚本变得更加简单!
猜你喜欢
  • 2017-07-26
  • 2015-07-15
  • 2013-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 2020-03-26
相关资源
最近更新 更多