如何从静态数组中检索元素?

时间:2014-01-17 07:31:49

标签: php sql arrays for-loop

我的数组列表不存储甚至检索项目,当用户按下租用它获取项目的值(id项目)然后它将它存储在arryList,并从它应该检索的篮子文件用户使用项目ID从数据库中选择的项目。我的主要问题是添加和检索似乎无法正常工作

    class Shop {

           static $_item = array();
          public function __construct(){

          }

    public function addItem($id) 
                { self::$_item[] = $id; 

                }
                public function getId()
                {
                    foreach(self::$_item->s as $s)
                    {
                        return $s;
                    }
                }
    }





<?php

require_once('Models/Dvd_sql.php'); 

require_once('Models/Shop.php');  


$view = new stdClass(); 
$view->dd = 'SQL'; 

$dvd_sql = new Dvd_sql(); 
$view->dd  = $dvd_sql->fetchAllStudents();    //->fetchAllStudents(); 



if(isset($_POST['rent']))
{
  $shop = new Shop();
  $shop->addItem($_POST['trying']);


}




   require_once('Views/dvdDetails.phtml');
<?php
require_once('Models/Basket.php'); 

require_once('Models/Shop.php');

$view = new stdClass();
$view->login = 'Homepage';
$view->dd = 'SQL'; 
$shop = new Shop();
 $basket = new Basket();

$d = $shop->getId();

$view->dd = $basket->getFrom($d);

1 个答案:

答案 0 :(得分:1)

我看到的一个问题是,在下面的foreach中,它将退出return上的函数并仅返回第一个项目。此外,因为它是一个数组,它没有's' property


由于$shop中有多个项目,您可以检索完整的数组,然后从类外部处理它们

            public function getId()
            {
                    return self::$_item;
            }

在课外

$shop = new Shop();
$basket = new Basket();

$d = $shop->getId();
foreach ($d as $id){
    $view->dd = $basket->getFrom($id);
    // do whatever you wish to do with the retrieved basket item
    // it's not clear what you wish to do with the item.
}