【问题标题】:Display values of array of objects显示对象数组的值
【发布时间】:2011-08-08 10:24:28
【问题描述】:

如何拨号播放存储在会话中的对象数组的值。

我是这样的

//从数据库中获取国家/地区数组

$countries = array();
    while($row = mysql_fetch_array($result)){

        $country = new Country();

        $country->setCountryName($row['country_name']);
        $country->setCountryNo($row['country_no']);
        $country->setCountryZipCode($row['country_zipcode']);

        $countries[]=$country;
    }

$_SESSION['countries']=$countries;

然后像这样显示会话的值

<select name="countries" id="countries">
        <?php foreach ($_SESSION['countries'] as $i=>$country ){?>
        <option><?php echo $_SESSION['countries'][$i]=>$country.getCountryName()?></option>   //here's the error
        <?php
        }
    ?>
    </select>

显示会话的值时出现错误,请帮助?

【问题讨论】:

  • 解析错误:语法错误,意外的 T_DOUBLE_ARROW,需要 ',' 或 ';'在第 14 行的 C:\AppServ\www\crm\subpages\SearchCustomer.sub.php 中。第 14 行是我打印会话值的地方

标签: php arrays session object


【解决方案1】:

试试这个:

<?php echo $country->getCountryName(); ?>

【讨论】:

  • 出现此错误:main() [function.main]:脚本试图执行方法或访问不完整对象的属性.请确保类定义“国家”您尝试操作的对象在 之前 被调用 unserialize() 或提供 __autoload() 函数来加载 C:\AppServ\www\crm\subpages\SearchCustomer 中的类定义。 sub.php上线
  • 我假设您在问题中发布的两个 sn-ps 来自两个不同的脚本。 Country 类定义在哪个文件中?您需要将该文件包含在显示&lt;select&gt; 元素的脚本中。
  • 我已经包含了具有国家类的国家文件,我尝试使 echo 'hi';它工作并循环作为返回国家的数量。
【解决方案2】:

您不能在会话中保存对象。对不起。但是您可以将它们存储为属性数组并在每次脚本启动时恢复它。

【讨论】:

猜你喜欢
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多