Jump to content

array empty


jamesxg1

Recommended Posts

<?php
class Tesco {

    private $quantity;  
    private $productid;
    private $storeid;

    ##########################################################################################################
    ###ALL INTEGER DATA FILTERED USINT THE BELOW OPTIONS WILL HAVE A MIN VALUE OF 1 AND A DEFAULT VALUE OF 1##
    ###E.G. IF FILTER FAILS TO VALIDATE AS INTEGER OF 1+ IT WILL DEFAULT THE VALUE TO ONE (1)#################
    ##########################################################################################################

    protected static $options = array( 'options'   => 
                                array( 'min_range' => 1,
                                       'default'   => 1 
                                     )
                                     );

    public function getPrice($quantity, $productid, $storeid = '1')
    {
        #####################################################################################################
        ### ONLY NEED TO VALIDATE THESE AS INTEGERS, THIS IS SUFFICIENT TO PROTECT QUERY FROM SQL INJECTION##
        #####################################################################################################

        $this->quantity  = filter_var($quantity , FILTER_VALIDATE_INT, self::$options);
        $this->productid = filter_var($productid, FILTER_VALIDATE_INT, self::$options);
        $this->storeid   = filter_var($storeid  , FILTER_VALIDATE_INT, self::$options);

        #############################################################################
        ###ONLY %d NEEDED DUE TO ALL VARS USED IN SPRINTF() BEING IN DECIMAL FORMAT##
        #############################################################################

        $query = sprintf("SELECT price, price * %d as total FROM products WHERE id = %d AND store = %d", $this->quantity,
                        $this->productid, $this->storeid);

        $result = mysql_query($query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

        $price = mysql_fetch_assoc($result);

        print_r($price);
    }      
}

?>

 

 

Link to comment
Share on other sites

<?php
class Tesco {

    private $quantity;  
    private $productid;
    private $storeid;

    ##########################################################################################################
    ###ALL INTEGER DATA FILTERED USINT THE BELOW OPTIONS WILL HAVE A MIN VALUE OF 1 AND A DEFAULT VALUE OF 1##
    ###E.G. IF FILTER FAILS TO VALIDATE AS INTEGER OF 1+ IT WILL DEFAULT THE VALUE TO ONE (1)#################
    ##########################################################################################################

    protected static $options = array( 'options'   => 
                                array( 'min_range' => 1,
                                       'default'   => 1 
                                     )
                                     );

    public function getPrice($quantity, $productid, $storeid = '1')
    {
        #####################################################################################################
        ### ONLY NEED TO VALIDATE THESE AS INTEGERS, THIS IS SUFFICIENT TO PROTECT QUERY FROM SQL INJECTION##
        #####################################################################################################

        $this->quantity  = filter_var($quantity , FILTER_VALIDATE_INT, self::$options);
        $this->productid = filter_var($productid, FILTER_VALIDATE_INT, self::$options);
        $this->storeid   = filter_var($storeid  , FILTER_VALIDATE_INT, self::$options);

        #############################################################################
        ###ONLY %d NEEDED DUE TO ALL VARS USED IN SPRINTF() BEING IN DECIMAL FORMAT##
        #############################################################################

        $query = sprintf("SELECT price, price * %d as total FROM products WHERE id = %d AND store = %d", $this->quantity,
                        $this->productid, $this->storeid);

        $result = mysql_query($query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

        $price = mysql_fetch_assoc($result);

        print_r($price);
    }      
}

?>

 

That does the job absolutely perfect. Cheers mate. on prob i got is this.

 

<?php
class Tesco {

    private $quantity;  
    private $productid;
    private $storeid;


    protected static $options = array( 'options'   => 
                                array( 'min_range' => 1,
                                       'default'   => 1 
                                     )
                                     );

    public function getPrice($quantity, $productid, $storeid = '1') {


        $this->quantity  = filter_var($quantity , FILTER_VALIDATE_INT, self::$options);
        $this->productid = filter_var($productid, FILTER_VALIDATE_INT, self::$options);
        $this->storeid   = filter_var($storeid  , FILTER_VALIDATE_INT, self::$options);

        $query = sprintf("SELECT price, price * %d as total FROM products WHERE id = %d AND store = %d", $this->quantity,
                        $this->productid, $this->storeid);

        $result = mysql_query($query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

        $price = mysql_fetch_assoc($result);

        $total += $price['total'];
        
        echo $total;
    }      
}

?>

 

so if i put 2 items in the process

 

Item 1 = 100 * 6 = 600

Item 2 = 1 * 8 = 8

 

returns 6008 :S lol.

 

Many many thanks

 

James.

Link to comment
Share on other sites

Thats because the class doesn't "remember" the value of $total from the last call made to an instance of itself, it simply echoes out the totals from the two calls made without whitespace/formatting. I.E

 

echo '600';
echo '8';
//would output 6008

 

This should work.

 

<?php
class Tesco {

    private $quantity;  
    private $productid;
    private $storeid;
    private $total;


    protected static $options = array( 'options'   => 
                                array( 'min_range' => 1,
                                       'default'   => 1 
                                     )
                                     );

    public function getPrice($quantity, $productid, $storeid = '1') {


        $this->quantity  = filter_var($quantity , FILTER_VALIDATE_INT, self::$options);
        $this->productid = filter_var($productid, FILTER_VALIDATE_INT, self::$options);
        $this->storeid   = filter_var($storeid  , FILTER_VALIDATE_INT, self::$options);

        $query = sprintf("SELECT price, price * %d as total FROM products WHERE id = %d AND store = %d", $this->quantity,
                        $this->productid, $this->storeid);

        $result = mysql_query($query) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

        $price = mysql_fetch_assoc($result);

        $this->total += $price['total'];
    } 


    public function getTotal() {
        ###########################################
        ###GET TOTAL TO TWO DECIMAL PLACES IN GBP##
        ###########################################
        return "£" . number_format($this->total, 2);
    }   
     
}

?>

 

Then you can use the class to get the total like so:

 

$tesco = new Tesco;
$tesco->getPrice($param1, $param2);
$tesco->getPrice($param3, $param4);

echo $tesco->getTotal();

 

Link to comment
Share on other sites

Andy-H you are a star, thank you so much may I add you as a friend as when I have finished the site i am working on (the one you have just helped me with) I would like to let you know and if your interested make you mod?.

 

Cheers mate.

 

Just a quick question. Sorry.

 

How do i find the lowest value from all these...

            echo $asda->getTotal() . '<br />';
            echo $tesco->getTotal() . '<br />';
            echo $morrisons->getTotal() . '<br />';

 

and echo the value along with the shop name ?

 

Cheers bud

 

Many thanks

 

James.

 

Link to comment
Share on other sites

Of course, I think my addy is on my profile. If not I will send it to you in a PM.

 

As for finding the lowest value; you can use the min function, although it would require making a small change to the getTotal() method.

 

    public function getTotal($sign = true) {
        ###########################################
        ###GET TOTAL TO TWO DECIMAL PLACES IN GBP##
        ###########################################
        if ($sign === true)
            return "£" . number_format($this->total, 2);

        return number_format($this->total, 2);

    }   

 

Then you can use:

 

$lowest = min( $asda->getTotal(false), $tesco->getTotal(false), $morrisons->getTotal(false) );
            echo $asda->getTotal() . '<br />';
            echo $tesco->getTotal() . '<br />';
            echo $morrisons->getTotal() . '<br />';

            echo 'Lowest price: £' number_format($lowest, 2) . '<br />';

Link to comment
Share on other sites

Of course, I think my addy is on my profile. If not I will send it to you in a PM.

 

As for finding the lowest value; you can use the min function, although it would require making a small change to the getTotal() method.

 

    public function getTotal($sign = true) {
        ###########################################
        ###GET TOTAL TO TWO DECIMAL PLACES IN GBP##
        ###########################################
        if ($sign === true)
            return "£" . number_format($this->total, 2);

        return number_format($this->total, 2);

    }   

 

Then you can use:

 

$lowest = min( $asda->getTotal(false), $tesco->getTotal(false), $morrisons->getTotal(false) );
            echo $asda->getTotal() . '<br />';
            echo $tesco->getTotal() . '<br />';
            echo $morrisons->getTotal() . '<br />';

            echo 'Lowest price: £' number_format($lowest, 2) . '<br />';

 

Worked. Thank you so much I have PM'ed you so we should be able to keep in contact. Once again thanks for this mate.

 

Take care, all the best.

 

James.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.