【问题标题】:Php code shows offset errorphp代码显示偏移错误
【发布时间】:2015-03-10 17:26:35
【问题描述】:

我是 smarty php 模板系统的初学者,我正在阅读一本书Smarty PHP Template Programming。第 2 章的第一个例子如下。

在根文件夹中,我们有一个名为data.txt 的文件,其中包含以下信息。

Title=> Building Websites with VB.NET and DotNetNuke 3.0 
Image=> 1904811272.jpg 
Author=> Daniel N. Egan 
Description=> A practical guide to creating and maintaining your own website with DotNetNuke, the free, open source evolution of Microsoft's IBuySpy Portal 
Year=> 2005 
Price=> 39.99 

Title=> SSL VPN : Understanding, evaluating and planning secure, web-based remote access 
Image=> 1904811078.jpg 
Author=> Joseph Steinberg, Tim Speed, Simon Jenner 
Description=> A comprehensive overview of SSL VPN technologies and design strategies 
Year=> 2005 
Price=> 49.99 

Title=> Windows Server 2003 Active Directory Design and Implementation=> Creating, Migrating, and Merging Networks 
Image=> 1904811086.jpg 
Author=> John Savill 
Description=> A unique, scenario-based approach to selecting and implementing the best Active Directory design for your environment 
Year=> 2005 
Price=> 59.99

下一步是从data.txt 检索数据。为此,在根文件夹中有一个名为 get_data.php 的文件,其内容如下:

<?php

    class books {

        public $title = array(); 
        public $image = array(); 
        public $author = array(); 
        public $description = array(); 
        public $year = array();
        public $price = array(); 

        private $filename = "data.txt";

        //class constructor
        function __construct()
            {
                //get the lines as an array
                $i=-1;
                $lines = file($this->filename);
                // strip "\n" at the end of each array
                // get each variable in an array

                foreach ( $lines as $line) {
                if (strlen($line) > 2) {

                    $line = rtrim($line);

                    list($what, $content) = explode("=>", $line);

                    if ($what == "Title") {
                        $i++;
                        $this->title[$i]=$content;
                        }
                    elseif ($what == "Image") {
                        $this->image[$i]=$content;
                        }
                    elseif ($what == "Author") {
                        $this->author[$i]=$content;
                        }
                    elseif ($what == "Description") {
                        $this->description[$i]=$content;
                        }
                    elseif ($what == "Year") {
                        $this->year[$i]=$content;
                        }
                    elseif ($what == "Price") {
                        $this->price[$i]=$content;

                        };
                        };
                    };

                } // end constructor

                } // end GetData

业务逻辑层负责从业务角度进行数据验证。在本例中,我们将为 2005 年出版的书籍应用 10% 的折扣,为 2004 年出版的书籍应用 20% 的折扣。我们需要在根文件夹中创建一个名为 books.php 的文件,如图所示

<?php
class bo_books {
//public
public $title = array();
public $image = array();
public $author = array();
public $description = array();
public $year = array();
public $price = array();
public $discount = array();
public $discounted = array();
//private
protected $DataObject;
function __construct()
{
$this->DataObject = new books();
}
public function apply_discount()
{
$this->title = $this->DataObject->title;
$this->image = $this->DataObject->image;
$this->author = $this->DataObject->author;
$this->description = $this->DataObject->description;
$this->year = $this->DataObject->year;
$this->price = $this->DataObject->price;
$j = 0;
foreach($this->year as $year)
{
if ($this->year[$j] == '2004')
$this->discount[$j] = '20';
elseif ($this->year[$j] == '2005')
$this->discount[$j] = '10';
$this->discounted[$j] = intval($this->price[$j] * (100 - $this->discount[$j]) ) / 100 ;
$j++;
};
} // end function apply_discount()
} // end class bo_books

表示层:首先,我们需要将变量从业务逻辑层传递到表示层。在根文件夹中,我们创建一个名为 index.php 的文件,如下所示:

<?php

require("libs/Smarty.class.php");
require_once("get_data.php");
require_once("books.php");

$book = new bo_books();
$book->apply_discount();

$smarty = new Smarty; 

$smarty->assign("book",$book);

$smarty->display('index.tpl');

?>

接下来,我们在templates文件夹中创建一个index.tpl文件

<html>
<head> <title> Site Architecture Example </title>
</head>
<body>
<table border="0" width="100%">
<tr>
<td align="left">
<a href="http://www.packtpub.com">
<img src="images\Packt.png" border="0">
</a>
<img src="images\focused.gif">
</td>
<td>
<h1> Chapter 2 Example </h1>
</td>
</tr>
</table>
<br>
Here are the books in a two-column table :
<br> <br>
<table border="1" width="100%">
{section name=tbl loop=$book->title}
{if %tbl.index% is not odd}
<tr>
{/if}
<td align="left">
<table>
<tr>
<td>
<img src="images\{$book->image[tbl]}" width="220">
</td>
<td valign="top">
<font size=+1><b> {$book->title[tbl]} </b></font><br>
<font size=-1 color=blue><b> {$book->author[tbl]} </b></font><br>
{$book->description[tbl]} <br>
Year: {$book->year[tbl]} <br>
<font size=-1>Cover Price: <s>${$book->price[tbl]}</s></font> <br>
Our Price: ${$book->discounted[tbl]}
<font color=red> save {$book->discount[tbl]} % </font>
</td>
</tr>
</table>
</td>
{if %tbl.index% is odd}
</tr>
{/if}
{/section}
</table>
</body>
</html>

当我运行程序时

注意:未定义的偏移量:C:\wamp\www\MyCode\get_data.php 第 28 行中的 1" 已显示。

get_data.php 的第 28 行是

list($what, $content) = explode("=>", $line);

任何人都可以提出建议甚至提供帮助吗?

【问题讨论】:

  • 这意味着explode返回一个只有1个元素的数组,所以你不能将它分配给两个变量。
  • 嘿 Manish,这是一篇非常详尽的帖子,很好,但在这种情况下,我们真的需要一个分解的例子。似乎这里唯一相关的是您的 get_data.php。您的 bo、smarty 模板和其他所有内容都与您的问题无关。您能否将其分解为一个可靠的具体示例。在循环之前粘贴var_dump($lines); 的输出。
  • 如果$line 上没有=&gt;,就会发生这种情况。输入var_dump($line);,看看错误发生时显示的内容。
  • 我怀疑问题在于标题之间的空白行并不是真正的空行,它们上面有一堆空格。在测试之前尝试修剪strlen()
  • @Manish 够公平的。我认为你这次走得太远了 :) 你真的只需要与问题相关的部分代码。我不认为显示层在这里有什么不同。这只是get_data.php 第 28 行和上游调用堆栈上发生的任何事情,包括您的数据源。

标签: php oop templates file-io runtime-error


【解决方案1】:

正如 Barmar 所说,问题是您的文件中某个点的数组元素少于或多于两个元素。请参阅data.txt 的第 15 行,您会发现第二个 =&gt;,所以我想这就是问题所在。

尝试将get_data.php的第28行修复为:

list($what, $content) = array_pad(explode('=>', $line, 2), 2, null);

所以你的数组中总是有两个元素。

基于this answer

【讨论】:

    【解决方案2】:

    $line 的值为 1。

    $str = 1;
    list($what, $content) = explode("=>", $str);
    

    发出此通知:

    注意:未定义的偏移量:第 7 行 test.php 中的 1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 2020-03-17
      相关资源
      最近更新 更多