PHP内爆不起作用

时间:2016-04-18 18:23:53

标签: php implode

我正在尝试使用包含会话行和页面URL的表,并将它们放在一个表中,每行包含会话,所有访问的页面都以“|”分隔。

以下代码仅返回其旁边没有网址的会话。我在这里做错了什么想法?

<?php
$html = "<table>\n";
$html .= "<tr>\n<th>SESSION</th>\n<th>PATH</th>\n</tr>\n";
$paths = array();
$sql = "SELECT session_id, page_url FROM pageviews";
$result = mysqli_query($conn, $sql);
$got_rows = mysqli_num_rows($result);

if ($got_rows) {
    while ($row = mysqli_fetch_array($result)) {
        array_push($paths[$row['session_id']], $row['page_url']);
    }

    foreach ($paths as $session => $page) {
        $html .= "<tr>\n";
        $html .= '<td>' . $session . "</td>\n";
        $html .= '<td>' . implode('| ', $page) . "</td>\n";
        $html .= "</tr>\n";
    }

} else {
    $html .= '<td colspan="2">No results</td>' . "\n";
}

$html .= "</table>\n";
echo $html;

if (!mysqli_query($conn,$sql))  {
    die('Error: ' . mysqli_error($conn));
}

mysqli_close($conn);
?>

1 个答案:

答案 0 :(得分:1)

如果您启用错误报告,则应收到public interface Interactable <E extends Interactable> { ... boolean tryOccupiedBy (final Person person, final Interactions interaction) throws InteractionNotPossibleException; ... } public abstract class InteractiveObject implements Interactable { private final List<Person> personsInteracting = new ArrayList<>(); private final List<Person> personsWaiting = new ArrayList<>(); ... @Override public final boolean tryOccupiedBy (final Person person, final Interactions interaction) throws InteractionNotPossibleException { boolean isOccupied = false; if (!isFurtherActionsAllowed()) { throw new InteractionNotPossibleException(this + " is already in use by some other " + "person."); } personsInteracting.add(person); currentInteraction = interaction; return isOccupied; } ... } 的错误消息:

  

警告:array_push()期望参数1为数组,给定

为null

请参阅:3v4l.org example of error

在您的示例中,您已将数组定义为:

array_push()

但是,在$paths = array(); 中,您尝试将新项目添加到不存在的数组中:

while()

这样的事情可以解决你的问题:

array_push($paths[$row['session_id']], $row['page_url']);
// $paths[$row['session_id']] hasn't been defined as an array, which array_push() expects

请参阅:3v4l.org working example

作为旁注,您可以移除while ($row = mysqli_fetch_array($result)) { // Create an array for the session_id if it doesn't exist yet if ( ! array_key_exists($row['session_id'], $paths) || ! is_array($paths[$row['session_id']])) { $paths[$row['session_id']] = []; } // Add to the array now that we know it exists array_push($paths[$row['session_id']], $row['page_url']); } 并简单地使用更快的array_push($arr, $var)。来自PHP docs

  

注意:如果您使用$arr[] = $var向阵列添加一个元素,最好使用array_push(),因为这样就不会有调用函数的开销。