【问题标题】:try to create tree list from database by php .. whats wrong尝试通过 php 从数据库创建树列表 .. 出了什么问题
【发布时间】:2013-12-16 19:33:55
【问题描述】:

我刚刚得到




  • second
  • secondsecond
  • 这是我的代码

    
    
        function getListaa($id){
                $sql = "select * from programs where itemRoot = '".$id."'";
                $strss = "";
                $this->dao1 = new DataAccess("localhost", "root", "", "ecommercedb");
                $this->dao1->fetch($sql);
                while(($rows = $this->dao1->getNextRow()) != null)
                {
                    if (($this->dao1->executeNum("select * from programs where itemRoot = '".$rows['itemId']."'")) > 0) 
                    {
                        if($id != 0) $strss .= "
  • ".$rows['itemName']; $strss .= ""; $strss .= $this->getListaa($rows['itemId']); $strss .= ""; if($id != 0) $strss .= "
  • "; } else { $strss = "
  • ".$rows['itemName']."
  • "; } } return $strss; }


    这是我的桌子

    --
    -- Table structure for table `programs`
    --
    
    CREATE TABLE IF NOT EXISTS `programs` (
      `itemId` int(11) NOT NULL AUTO_INCREMENT,
      `itemName` varchar(250) NOT NULL,
      `itemPrice` float DEFAULT NULL,
      `itemDesc` text,
      `itemRoot` int(11) NOT NULL,
      `itemDateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`itemId`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
    
    --
    -- Dumping data for table `programs`
    --
    
    INSERT INTO `programs` (`itemId`, `itemName`, `itemPrice`, `itemDesc`, `itemRoot`, `itemDateTime`) VALUES
    (1, 'root', NULL, NULL, 0, '2013-12-15 13:22:03'),
    (3, 'second', NULL, NULL, 1, '2013-12-15 13:22:13'),
    (4, 'frist', NULL, NULL, 1, '2013-12-15 13:22:36'),
    (5, 'secondfrist', NULL, NULL, 3, '2013-12-15 13:23:46'),
    (6, 'secondsecond', NULL, NULL, 3, '2013-12-15 13:23:46'),
    (7, 'fristfrist', NULL, NULL, 4, '2013-12-15 13:24:02');
    


    这是我的数据

    +--------------------+--------------+------------- --+------------+----------+ |项目 ID |物品名称 |商品价格 |项目描述 |项目根 | +--------------------+--------------+------------- --+------------+----------+ | 1 |根 |空 |空 | 0 | | 3 |第二 |空 |空 | 1 | | 4 |第一|空 |空 | 1 | | 5 |第二名 |空 |空 | 3 | | 6 |秒秒 |空 |空 | 3 | | 7 |第一手 |空 |空 | 4 | +--------------------+--------------+------------- --+------------+----------+

    这是数据访问层代码

    
    
        conn = mysql_pconnect($host, $user, $pass);
                mysql_select_db($dbname, $this->conn);
            }
    
            /**
            * Execute a query like insert, delete or update
            * @param $sql string
            * @return void
            */
            function execute($sql) {
    
                return mysql_query($sql,$this->conn);
            }
            function executeNum($sql) {
    
                return mysql_num_rows(mysql_query($sql,$this->conn));
            }
            /**
            * Fetches a query resources and stores it in a local member
            * @param $sql string
            * @return void
            */
            function fetch($sql) {
                $this->result = mysql_query($sql,$this->conn);
            }
            /*function getRowsNum(){
                return $this->result->num_rows;
            }*/
            /**
            * Returns an associative array of a query row
            * @return Array
            */
            function getNextRow() {
                if( $row = mysql_fetch_array($this->result) )
                    return $row;
                else
                    return null;
            }
        }
        ?>
    
    

    【问题讨论】:

      标签: php list tree


      【解决方案1】:

      现在,问题解决了!!!!!!! 这是我的解决方案:

      
      
          function getListaa($id){
                  $sql = "select * from programs where itemRoot = '".$id."'";
                  $strss = "";
                  $dao1 = new DataAccess("localhost", "root", "", "ecommercedb");
                  $dao1->fetch($sql);
                  while(($rows = $dao1->getNextRow()) )
                  {
                      if (($dao1->executeNum("select * from programs where itemRoot = '".$rows['itemId']."'")) > 0) 
                      {
                          if($id != 0) $strss .= "
    • ".$rows['itemName']; $strss .= ""; $strss .= $this->getListaa($rows['itemId']); $strss .= ""; if($id != 0) $strss .= "
    • "; } else { $strss .= "
    • ".$rows['itemName']."
    • "; } } unset($dao1); return $strss; }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-20
        • 2013-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多