从模型php进入下一个id

时间:2013-05-14 01:47:54

标签: php object models

您好我先发布有问题的代码,然后问问题。

function printUserActiveAds($currUser){
        $activeUser = new users();
        $userActiveAds = new userAds();

        echo
        '
            <h4>Currently Active Ads:</h4>
            <table class="userActiveAds">
                <tr><td>Title</td><td></td><td>Price</td><td></td><td>Description</td><td></td><td>Delete</td></tr>
        ';

        $numAds = $userActiveAds->getNumberOfAdsByUserId($currUser);
        if($numAds > 0){
            for($i = 0; $i < $numAds; $i++){
                $id = $userActiveAds->getAdIdByUserId($currUser);
                echo
                '
                    <tr>
                        <td>'.$userActiveAds->getAdTitleById($id).'</td><td></td>
                        <td>'.$userActiveAds->getAdPriceById($id).'</td><td></td>
                        <td>'.truncateDescription($userActiveAds->getAdDescriptionById($id),100).'</td><td></td>
                        <td>
                            <form action="" method="post">
                                <input type="submit" name="delete" id="delete" class="button" value="Delete" />
                            </form>
                        </td>
                    </tr>
                ';
                    }
        }else{
            echo
            '
                <tr><td colspan="7"><span class="prompt">Currently No Active Ads</span></td></tr>
            ';
        }

模型(userAds)

//ad model
    class userAds {
        private $id;
        private $title;
        private $price;
        private $location;
        private $category;
        private $subCategory;
        private $description;

        //default constructor
        function _constructor(){
            $this->id = null;
            $this->title = null;
            $this->price = null;
            $this->location = null;
            $this->category = null;
            $this->subCategory = null;
            $this->description = null;
        }

        //set the ad details
        function setAdDetails($adId,$adTitle, $adPrice, $adLocation, $adCategory, $adSubCategory, $adDescription){
            $this->id = $adId;
            $this->title = $adTitle;
            $this->price = $adPrice;
            $this->location = $adLocation;
            $this->category = $adCategory;
            $this->subCategory = $adSubCategory;
            $this->description = $adDescription;
        }

        //get number of ads by user using user id
        function getNumberOfAdsByUserId($userId){
            $numberOfAds = mysql_query("SELECT * FROM ads WHERE user_id='$userId'") or die(mysql_error());
            return $numAds = mysql_num_rows($numberOfAds);
        }

        //get ad id by user id
        function getAdIdByUserId($userId){
            $queryAdId = mysql_query("SELECT id FROM ads WHERE user_id='$userId'") or die(mysql_error());
            $data = mysql_fetch_assoc($queryAdId);
            return $this->id = $data['id'];
        }

好的,所以quesiont中的特定代码段是getAdIdByUserId。它不会迭代与特定用户关联的所有ID。从根本上说,我想要的是将所有广告与特定用户相关联并显示在表格中。但是,当我运行上面的代码时,我得到与该用户关联的正确数量的广告,但该表仅显示与该用户关联的第一个ID。我想也许我应该只增加id,但问题是这个用户有id为1,2,3,7和17的广告。有关如何去做的任何想法?另外从printActiveAds函数中我可以看到我创建了两个对象,另外一个问题是我必须在函数末尾删除它们,或者我不应该担心它们,因为它们在函数完成后被删除(当地范围)。

提前致谢。

1 个答案:

答案 0 :(得分:0)

所以我想出了这个问题。我缺少set语句和基本的get语句。在我进行查询后,我执行了mysql_fetch_array($ query),然后将每行设置为等于userAd属性,然后当我执行echo时调用我设置的内容