Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 修复非法字符串偏移量_Php - Fatal编程技术网

Php 修复非法字符串偏移量

Php 修复非法字符串偏移量,php,Php,代码 Foreach循环 foreach ($LS::getBase() as $base) { $fromDatabaseThumb = $base['base-thumbnail']; $fromDatabaseDescription = $base['base-description']; $fromDatabaseTitle = $base['base-title']; echo "

代码
Foreach循环

foreach ($LS::getBase() as $base) {
            $fromDatabaseThumb = $base['base-thumbnail'];
            $fromDatabaseDescription = $base['base-description'];
            $fromDatabaseTitle = $base['base-title'];
        echo " 
        <div style='background-color: #ffffff; width: 725px; height: 280px; display: inline-block; position: relative;''>
        <div style='display: inline-block; max-width: 450px; max-height: 260px; margin: 12px; float: left;' class='baseThumb'>
            <img src='$fromDatabaseThumb' style='max-width: 450px; max-height: 260px'>
        </div>
        <div class='baseInfo' style='display: inline-block; width: 250px; height: 253px; margin-top: 12px;'>
            <h3 style='margin: 0; font-family: helvetica; max-width: 250px; max-height: 22px; overflow: hidden;'>$fromDatabaseTitle</h3>
            <p style='margin-top: 8px; word-wrap: break-word;'>
                <strong>Description:</strong><br>
                " . $fromDatabaseDescription . "
            </p>
        </div>
        </div>
        ";
        }

错误:


我所做的

我做过研究,没有找到任何有效的方法。所以我对代码做了一些修改,但仍然无法修复


额外信息

我做了一个var_dump(),因为我在其他帖子中看到它面临着与我做var_dump()相同的问题,所以我想我会这样做,以防对你们有所帮助

var\u dump()


这样的foreach循环不会在每次迭代时调用该方法。它在第一个循环中调用一次,然后在随后的每个迭代中对返回的值进行循环。因此,调用
getBase
将从查询中返回一行,然后在结果的每一列上循环,使
$base
在循环中成为该列中的值,而不是行的数组。var_转储似乎是从循环中的
getBase
返回的,而不是
$base
。如果您进行了
var\u转储($base)在循环中,每个字符串都会出现一次。要修复它,您可能需要将
getBase
更改为call,它将返回一个2d数组,如
$result[rowNum][colName]


编辑:此外,将循环切换到另一个结构,如
,而
循环将在每次迭代中计算完整语句,这将使您进入一个无限循环,因为每次调用
getBase
时,您将重新查询、返回第一行并重复。

16-18行到底是什么?编辑算出了,你能做
var_dump($LS::getBase())吗在foreach循环之前?@iam解码器是16-18。编辑好,让我现在就编辑。@iam decoder它给了我与上次相同的var_dump()结果。那么您的问题是它将
$base
设置为等于这些值。在foreach循环中执行var\u dump($base)
您将看到,
$base
不是一个数组,因此无法访问您指定的密钥。@RepeaterScreeper如果对您或其他人来说不够清楚,请告诉我。“我现在有点累,所以我可能已经开始漫无目的地闲逛了。”“乔纳桑库恩,好吧!”!我现在明白多了一点。非常感谢您抽出时间。:)
public static function getBase($what = '*') {
self::construct();
if( is_array($what) ){
  $columns = implode("`,`", $what);
  $columns  = "`{$columns}`";
}else{
  $columns = $what != "*" ? "`$what`" : "*";
}

$sql = self::$dbh->prepare("SELECT {$columns} FROM baselayouts");
$sql->execute();
$data = $sql->fetch(\PDO::FETCH_ASSOC);
if( !is_array($what) ){
  $data = $what == "*" ? $data : $data[$what];
}
return $data;
}
array(4) {
    ["id"]=> string(1) "1"
    ["base-title"]=> string(14) "Base Layout #1"
    ["base-thumbnail"]=> string(24) "/res/img/baseLayout1.png"
    ["base-description"]=> string(73) "This is the base test description. Hopefully it does what it needs to do."
}