php - 奇数类问题

标签 php mysql

在编写我的代码 OO 过程中,遇到了这个问题。接下来的类(class)给了我一个奇怪的问题。当我创建函数 CartTotal() 时,它会回显正确的值,但函数 DisplayListings();没有显示任何 MySQL 数据(都使用 $this->displaylistings)。如果我更改页面上调用这两个函数的顺序,CartTotal();不产生任何结果,并且 DisplayListings();正常工作。所以看起来使用 $this->displaylistings 会把它搞乱。如果您知道我在做什么,请告诉我。这是类:(仍然没有完成并且需要进行一些标准化)。

class Listing {

    public $displaylistings;
    public $displaycount;
    public $viewall;
    public $ShopperID;
    public $numrows;

    public function __construct() {

        $Person = new User();

        $ShopperID = mysql_query("SELECT * FROM users WHERE Username = '$Person->username'");
        $row_ShopperID = mysql_fetch_object($ShopperID);
        $this->ShopperID = $row_ShopperID->UserID;

        $this->displaylistings=mysql_query("SELECT * FROM nordstromlisting WHERE ShopperID = '$this->ShopperID'
        UNION ALL SELECT * FROM saksfifthavenuelisting WHERE ShopperID = '$this->ShopperID'
        UNION ALL SELECT * FROM guesslisting WHERE ShopperID = '$this->ShopperID'
        UNION ALL SELECT * FROM gnclisting WHERE ShopperID = '$this->ShopperID'
        UNION ALL SELECT * FROM urbanoutfitterslisting WHERE ShopperID = '$tnis->ShopperID'
        UNION ALL SELECT * FROM bananarepubliclisting WHERE ShopperID = '$this->ShopperID'
        UNION ALL SELECT * FROM americaneaglelisting WHERE ShopperID = '$this->ShopperID'
        UNION ALL SELECT * FROM neimanmarcuslisting WHERE ShopperID = '$this->ShopperID'
        UNION ALL SELECT * FROM footlockerlisting WHERE ShopperID = '$this->ShopperID'");
        if (mysql_num_rows($this->displaylistings) == 0) {
            echo "<div id='nolistings'><a href='shop.php'>You have no active listings, <br />click here to get started.</a></div>";
            $this->numrows = mysql_num_rows($this->displaylistings);
        }
        elseif (mysql_num_rows($this->displaylistings) == 1 ) {
            $this->displaycount = 1;
            $this->numrows = mysql_num_rows($this->displaylistings);
        }
        elseif (mysql_num_rows($this->displaylistings) == 2 || mysql_num_rows($this->displaylistings) > 2) {
            $this->displaycount = 2;
            $this->numrows = mysql_num_rows($this->displaylistings);
        }
    }

    public function CartTotal() {
        for ($i=0;$i<$this->numrows;$i++) {
            $row = mysql_fetch_assoc($this->displaylistings);
            echo $row['DiscountedPrice'];
        }
    }

    public function DeleteListing() {
        if ($_POST['deletelisting'] == 'yes') {
            $deleteid = $_POST['deleteid'];
            $deletestore = "$_POST[storename]";
            $idstorehistory = str_replace("listing", "history", $deletestore);
            $movetohistory = mysql_query("INSERT INTO $idstorehistory SELECT * FROM $identifystore WHERE id = $deleteid");
            $userdelete = mysql_query("DELETE FROM $deletestore WHERE id = $deleteid");
        }
    }

    public function DisplayListing() { 

            for($i=0;$i<$this->displaycount;$i++) {
            $row=mysql_fetch_assoc($this->displaylistings);

            // Check to what store this listing belongs to
            $nordstromtest=mysql_query("SELECT * FROM nordstromlisting WHERE ShopperID = '$this->ShopperID'");
            $nordstromstore=mysql_fetch_assoc($nordstromtest);
            if ($row == $nordstromstore) {
                $storename = "Nordstrom";
                $storetbl = "nordstromlisting";
            }

            $saksfifthavenuetest=mysql_query("SELECT * FROM saksfifthavenuelisting WHERE ShopperID = '$this->ShopperID'");
            $saksfifthavenuestore=mysql_fetch_assoc($saksfifthavenuetest);
            if ($row == $saksfifthavenuestore) {
                $storename = "Sak's Fifth Avenue";
                $storetbl = "saksfifthavenuelisting";
            }

            $neimanmarcustest=mysql_query("SELECT * FROM neimanmarcuslisting WHERE ShopperID = '$this->ShopperID'");
            $neimanmarcusstore=mysql_fetch_assoc($neimanmarcustest);
            if ($row == $neimanmarcusstore) {
                $storename = "Neiman Marcus";
                $storetbl = "neimanmarcuslisting";
            }

            $guesstest=mysql_query("SELECT * FROM guesslisting WHERE ShopperID = '$this->ShopperID'");
            $guessstore=mysql_fetch_assoc($guesstest);
            if ($row == $guessstore) {
                $storename = "GUESS";
                $storetbl = "guesslisting";
            }

            $americaneagletest=mysql_query("SELECT * FROM americaneaglelisting WHERE ShopperID = '$this->ShopperID'");
            $americaneaglestore=mysql_fetch_assoc($americaneagletest);
            if ($row == $americaneaglestore) {
                $storename = "American Eagle";
                $storetbl = "americaneaglelisting";
            }

            $gnctest=mysql_query("SELECT * FROM gnclisting WHERE ShopperID = '$this->ShopperID'");
            $gncstore=mysql_fetch_assoc($gnctest);
            if ($row == $gncstore) {
                $storename = "GNC";
                $storetbl = "gnclisting";
            }

            $footlockertest=mysql_query("SELECT * FROM footlockerlisting WHERE ShopperID = '$this->ShopperID'");
            $footlockerstore=mysql_fetch_assoc($footlockertest);
            if ($row == $footlockerstore) {
                $storename = "Foot Locker";
                $storetbl = "footlockerlisting";
            }

            $bananarepublictest=mysql_query("SELECT * FROM bananarepubliclisting WHERE ShopperID = '$this->ShopperID'");
            $bananarepublicstore=mysql_fetch_assoc($bananarepublictest);
            if ($row == $bananarepublicstore) {
                $storename = "Banana Republic";
                $storetbl = "bananarepubliclisting";
            }

            $urbanoutfitterstest=mysql_query("SELECT * FROM urbanoutfitterslisting WHERE ShopperID = '$this->ShopperID'");
            $urbanoutfittersstore=mysql_fetch_assoc($urbanoutfitterstest);
            if ($row == $urbanoutfittersstore) {
                $storename = "Urban Outfitters";
                $storetbl = "urbanoutfitterslisting";
            }

            //Delete old listings (test with minutes...)

            $timenow = date("Y-m-d");
            $time = strtotime($timenow);
            $storehistory = str_ireplace("listing","history","$storetbl");
            $movetohistory = mysql_query("INSERT INTO $storehistory SELECT * FROM $storetbl WHERE $time - datecreated >= 432000") or die ("Query failed: " . mysql_error() . " Actual query: " . $query);
            $deltefromlisting = mysql_query("DELETE FROM $storetbl WHERE $time - datecreated >= 432000") or die ("Query failed: " . mysql_error() . " Actual query: " . $query);

            //Item link modifactions
            if ($row['link'] != null) {
                $shortlink = substr($row['link'],0,30);
                $shortlink .= '...';
            }


            if ($row['itemsize'] != null ) {
                $mensshirt = $row['itemsize'];
            }

            if ($row['waistsize'] != 0) {
                $waistsize = $row['waistsize'];
                $w = 'Waist';
            }

            if ($row['lengthsize'] != 0) {
                $lengthsize = $row['lengthsize'];
                $l = 'Length';
            }

            if ($row['ladystopsize'] != null) {
                $ladystopsize = $row['ladystopsize'];
            }

            if ($row['ladysdresssize'] != 0) {
                $ladysdresssize = $row['ladysdresssize'];
            }

            if ($row['ladysbottomsize'] != 0) {
                $ladysbottomsize = $row['ladysbottomsize'];
            }

            if ($row['shoesize'] != 0) {
                $shoesize = $row['ladysshoesize'];
            }

            echo "<b><div id='storenametab'><span class='storename'>$storename</span></div></b>";
            echo "<div id='listings1'><span id='orderdetails'>Item Number : $row[itemnum] <br />";
            echo "Discount Price: $$row[DiscountedPrice] <br />";
            echo "Item Type : $row[itemtype]<br />";
            echo "Item Size :
            $mensshirt 
            $waistsize $w 
            $lengthsize $l 
            $ladystopsize 
            $ladysdresssize 
            $ladysbottomsize 
            $shoesize
            <br />";
            echo "Item Color: $row[itemcolor] <br />";

            if ($row['link'] != null) {
            echo "Item URL: <a href='$row[link]' target='_blank'>$shortlink</a><br />";
            }           
        //STATUS

            if ($row['VillainID'] == 0)
                echo "Status: Unclaimed <br />";

            else {

                $VillainPaypalEmail = mysql_query("SELECT PaypalEmail FROM villains WHERE VillainID = '$row[VillainID]'");
                $fetchVillain = mysql_fetch_assoc($VillainPaypalEmail);
                $VillainPaypal = $fetchVillain['PaypalEmail'];

                $itemname = "Item Number: $row[itemnum] -- $row[itemtype] -- $mensshirt $waistsize $w $lengthsize $l $ladystopsize $ladysdresssize $ladysbottomsize $shoesize -- $row[itemcolor]";
                $discprice = $row['DiscountedPrice'];
                $state = mysql_query("SELECT state from users WHERE Username = '$username'");
                $fetchstate = mysql_fetch_assoc($state);
                $state = $fetchstate['state'];

                switch ($storetbl) {
                    case "nordstromlisting":
                        $shipping = 8;
                        if ($state == 'AZ' || 'CA' || 'CO' || 'CT' || 'FL' || 'GA' || 'HI' || 'IA' || 'ID' || 'IL' || 'IN' || 'KS' || 'MA' || 'MD' || 'MN' || 'MO' || 'NC' || 'NJ' || 'NV' || 'NY' || 'OH' || 'PA' || 'RI' || 'SD' || 'TX' || 'UT' || 'VA' || 'WA') {
                            $tax = $discprice * .09;
                        }
                        else {
                            $tax = $discprice * .02;
                        }
                        break;
                    case "saksfifthavenuelisting":
                        switch ($discprice) {
                            case ($discprice <= 25):
                                $shipping = 5;
                                break;
                            case ($discprice > 25 && $discprice <= 50):
                                $shipping = 8;
                                break;
                            case ($discprice > 50 && $discprice <= 100):
                                $shipping = 10;
                                break;
                            case ($discprice > 100 && $discprice <= 200):
                                $shipping = 13;
                                break;
                            case ($discprice > 200 && $discprice <= 300):
                                $shipping = 15;
                                break;
                            case ($discprice > 300 && $discprice <= 500):
                                $shippig = 17;
                                break;
                            case ($discprice > 500):
                                $shipping = 20;
                                break;
                        }
                        if ($state == 'AL' || 'AZ' || 'CA' || 'CO' || 'CT' || 'FL' || 'GA' || 'IL' || 'IN' || 'LA' || 'MA' || 'MD' || 'MN' || 'MO' || 'NC' || 'NJ' || 'NV' || 'NY' || 'OH' || 'OK' || 'PA' || 'SC' || 'TX' || 'VA' || 'DC') {
                            $tax = $discprice * .09;
                        }
                        else {
                            $tax = .02;
                        }
                        break;
                    case "neimanmarcuslisting":
                        switch ($discprice) {
                            case ($discprice <= 25):
                                $shipping = 6;
                                break;
                            case ($discprice > 25 && $discprice <= 50):
                                $shipping = 9;
                                break;
                            case ($discprice > 50 && $discprice <= 100):
                                $shipping = 11.50;
                                break;
                            case ($discprice > 100 && $discprice <= 200):
                                $shipping = 16.50;
                                break;
                            case ($discprice > 200 && $discprice <= 300):
                                $shipping = 18.50;
                                break;
                            case ($discprice > 300 && $discprice <= 500):
                                $shippig = 21.50;
                                break;
                            case ($discprice > 500 && $discprice <= 700):
                                $shipping = 25.50;
                                break;
                            case ($discprice > 700 && $discprice <= 1000):
                                $shipping = 29.00;
                                break;
                            case ($discprice > 1000):
                                $shipping = 32.00;
                                break;
                        }
                        if ($state == 'AZ' || 'CA' || 'CO' || 'FL' || 'GA' || 'HI' || 'IL' || 'MA' || 'MD' || 'MI' || 'MN' || 'MO' || 'NC' || 'NJ' || 'NV' || 'NY' || 'PA' || 'TX' || 'VA' || 'WA' || 'DC') {
                            $tax = $discprice * .09;
                        }
                        else {
                            $tax = $discprice * .02;
                        }   
                        break;
                    case "urbanoutfitterslisting":
                        switch ($discprice) {
                            case ($discprice <= 50):
                                $shipping = 6;
                                break;
                            case ($discprice > 25 && $discprice < 50):
                                $shipping = 8;
                                break;
                            case ($discprice >= 50 && $discprice < 100):
                                $shipping = 10;
                                break;
                            case ($discprice >= 100 && $discprice < 150):
                                $shipping = 12;
                                break;
                            case ($discprice > 150):
                                $shipping = 0;
                                break;
                        }
                        if ($state == 'AL' || 'AZ' || 'CA' || 'CO' || 'CT' || 'FL' || 'GA' || 'IL' || 'IN' || 'KS' || 'LA' || 'MA' || 'MD' || 'MI' || 'MN' || 'MO' || 'NB' || 'NM' || 'NC' || 'OH' || 'OR' || 'NV' || 'NY' || 'PA' || 'RI' || 'SC' || 'TN' || 'TX' || 'UT' || 'VA' || 'VT' || 'WA' || 'WI' || 'DC') {
                            $tax = $discprice * .09;
                        }
                        else {
                            $tax = $discprice * .02;
                        }
                        break;
                    case "gnclisting":
                        $shipping = 6;
                        break;
                    case "bananarepubliclisting";
                        $shipping = 7;
                        //call Banana Republic for list of taxable states
                        break;
                    case "guesslisting":
                        $shipping = 7.95;
                        if ($state == 'AZ' || 'CA' || 'CO' || 'CT' || 'FL' || 'GA' || 'HI' || 'IL' || 'IN' || 'KS' || 'LA' || 'MA' || 'MD' || 'MI' || 'MN' || 'MO' || 'NJ' || 'NV' || 'NY' || 'NC' || 'OH' || 'PA' || 'RI' || 'SC' || 'TN' || 'TX' || 'VA' || 'WA') {
                            $tax = $discprice * .09;
                        }
                        else {
                            $tax = $discprice * .02;
                        }
                        break;
                    case "footlockerlisting";
                        $shipping = 6.99;
                        if ($state == 'AK' || 'DE'|| 'MT' || 'NH' || 'OR') {
                            $tax = $discprice * .02;
                        }
                        else {
                            $tax = $discprice * .09;
                        }
                        break;
                    case "americaneaglelisting":
                        switch($discprice) {
                            case ($discprice < 100):
                            $shipping = 7.00;
                            break;
                            case ($discprice > 100):
                            $shipping = 0;
                            break;
                        }
                            $tax = $discprice * .09;
                        break;
                }

                echo "Status: Claimed!"?>
                <form action='https://www.paypal.com/cgi-bin/webscr' target='blank' method='post'> 
                <input type='hidden' name='business' value='<?php echo $VillainPaypal ?>'> 
                <input type='hidden' name='cmd' value='_xclick'> 
                <input type='hidden' name='item_name' value="<?php echo $itemname ?>"> 
                <input type='hidden' name='shipping' value="<?php echo $shipping ?>"> 
                <input type='hidden' name='tax' value="<?php echo $tax ?>"> 
                <input type='hidden' name='amount' value='<?php echo $row['DiscountedPrice'] ?>'> 
                <input type='hidden' name='currency_code' value='USD'> 
                <input type='image' name='submit' border='0' 
                src='https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif' 
                alt='PayPal - The safer, easier way to pay online'> 
                <img alt='blahblah' border='0' width='1' height='1' 
                src='https://www.paypal.com/en_US/i/scr/pixel.gif' > 
                </form>
                <br />
                <?php ;
        //STATUS
        }           
            ?><br /><form action="MyAccountNEW.php" method="POST">
                <input type="hidden" name="deletelisting" id="deletelisting" value="yes">
                <input type="hidden" name="deleteid" id="deleteid" value="<?php echo $row['id']; ?>">
                <input type="hidden" name="storename" id="storename" value="<?php echo "$storetbl"; ?>">
                <input type="submit" class="deletelistingbutton" value="Delete listing">
               </form>

            <?php

            echo "</span></div>";

            $mensshirt = "";
            $waistsize = "";
            $lengthsize = "";
            $ladystopsize = "";
            $ladysdresssize = "";
            $ladysbottomsize = "";
            $shoesize = "";
        }

    }
}

最佳答案

您正在从两个不同的函数消耗相同的资源。

如果您在查询句柄上使用 mysql_fetch_something 读取一行(在本例中为 $this->displaylistings),您将无法再次读取它。

尝试将此行放在 DisplayListing 和 CartTotal 的开头

mysql_data_seek($this->displaylistings, 0)

它应该将 mysql 内部游标重新定位到与 $this->displaylistings 关联的结果集的第一行。让我知道它是否有效

关于php - 奇数类问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1021611/

相关文章:

php - 过滤输入和事件

php - 如何将 HTML 表单数据提交到 MySql 数据库?

mysql - Doctrine DQL - 预期字符串结尾,得到 'inner'

java - 我将 JSON 插入 mysql 表的语法有什么问题?

mysql - 链接到 libmariadb 时出现奇怪的错误

php - 这会产生某种永久循环吗? PHP 无法正常执行

php - 无法在 Zend 中的分支文件夹之间执行 JOIN

MySQL 在 SELECT 中引用嵌套查询结果?

php - mysql在数据库中两次插入记录

php - 如何在 json 的帮助下使用 PHP,MySql 在 android 中验证用户登录凭据