-2

I did a function that calculates the total price, by a specific user and his cart shopping, with PHP.

I`m getting this error:

Parse error: syntax error, unexpected '$user' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) in C:\xampp\htdocs\myPrograms\projectTest.php on line 43.

This is my code (the problematic line is the 2nd line below):

    Class bill {

    $username= 'almog';

    echo "Your total price for this bought is:" .Total_bill($username). "Dollars"."<br>";

    function Total_bill($username) 
    {
        $total = 0;
        $query= $this-> db->query ("select serial_number, count_purchase, price_item from bought_history LEFT JOIN
        product ON bought_history. serial_number = product. serial_number where username=username and date_purchase= date_purchase");

    foreach($query as $key=> $value )
    {

        if ($bought_history. $serial_number->$count_purchase ==1)
        {
            $total = $total + $product. $serial_number->$price_item;
        }
        else 
        {
            $total = $total + ($product. $serial_number->$price_item * $bought_history. $serial_number->$count_purchase);
        }
    }

    return $total; 
    }
}
MrWhite
  • 43,179
  • 8
  • 60
  • 84
Almog Davidof
  • 21
  • 1
  • 3
  • Can you tell us what you've tried and share your code? – paul-shuvo Feb 12 '20 at 00:12
  • this is the db This is my DB: -- Table structure for table `bought_history` -- CREATE TABLE `bought_history` ( `username` varchar(10) NOT NULL, `serial_number` int(11) NOT NULL, `date_purchase` int(11) NOT NULL, `count_purchase` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; – Almog Davidof Feb 12 '20 at 00:16
  • You will need to update / edit your post with everything used in your project. Use the [edit](https://stackoverflow.com/posts/60179236/edit) button under your question to do that. @AlmogDavidof – Funk Forty Niner Feb 12 '20 at 01:23
  • updated my post – Almog Davidof Feb 15 '20 at 13:07

1 Answers1

2
Class bill {

    $username= 'almog';

    echo "Your total price for this bought is:" .Total_bill($username). "Dollars"."<br>";

    function Total_bill($username) 
    {

You can't put "code" directly inside the class definition - which is what it looks like you are trying to do here. By "code" I'm refering to the $username variable assignment and the echo statement that follows. (Fix the $username parse error and you'll get the same parse error refering to the echo statement.)

However, it's not clear where that code should go (inside a class method OR called from outside the class OR not use a class at all?) since you are referencing the Total_bill() class method as a global function (which it is not in your example).

To get your code into a "working" state, either:

  1. Move your "code" outside of the class definition entirely. For example:

    Class bill {
    
        // Class definition here...
    
    }
    
    $username = 'almog';
    
    $billInstance = new bill();
    
    echo "Your total price for this bought is:" .$billInstance->Total_bill($username). "Dollars"."<br>";
    
  2. OR, move your code into another class method (although this probably doesn't make much sense given what you are trying to do). For example:

    Class bill()
    
        // Rest of class definition here...
    
        function outputTotalBill()
        {
            $username = 'almog';
            echo "Your total price for this bought is:" .$this->Total_bill($username). "Dollars"."<br>";
        }
    }
    
    $billInstance = new bill();
    
    $billInstance->outputTotalBill();
    
  3. OR, perhaps you shouldn't be using a class here at all? And Total_bill() should be a global function in your example?

Reference:

MrWhite
  • 43,179
  • 8
  • 60
  • 84