-2

I am trying to display products by pulling from mysql database. The code uses a function called getProductById that pulls products from the database. The idea is to match the product with the right price and then display the product with the function cartElement. I would like to thank you so much in advance for taking your time to help.

Note: Using PHP Version 7.3.11

Error Message:

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in /Library/WebServer/Documents/J_Renzo_Mobile_Website_Draft/cart.php on line 90

My code:

            $total = 0;
            if(isset($_SESSION['cart']) && count($_SESSION['cart']) > 0) {
                foreach ($_SESSION['cart'] as $cartProduct) {
                    $product = $database->getProductById((int)$cartProduct['product_id']);
                    $price = match($cartProduct['option']) {
                        'exclusive' => $product['exclusive'],
                        'unlimited' => (int)$product['unlimited'],
                        'premium' => (int)$product['premium'],
                        default => (int)$product['basic'],
                    };
                    if (is_numeric($price)) {
                        $total += $price;
                    }
                    cartElement($product['product_image'], $product['product_name'],$price,$product['id']);
                    //break; I don't know if I need this...
                }
            } else {
                echo "<h5>Cart is empty.</h5>";
            }

Bind_param() statement giving errors:

//get product by id from database...
public function getProductById(int $id): array{
    //$stmt = $this->con->prepare('SELECT * FROM $tablename WHERE id = ?');
    $sql = "SELECT * FROM Productdb WHERE id = ?";
    $stmt = $this->con->prepare($sql);
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    return $result->fetch_assoc();
}

1 Answers1

2

Revise the match statement below

 $price = match($cartProduct['option']) {
                        'exclusive' => $product['exclusive'],
                        'unlimited' => (int)$product['unlimited'],
                        'premium' => (int)$product['premium'],
                        default => (int)$product['basic'],
                    };

to read something like this for equivalent functionality as an example

    $price = call_user_func(function($cartProduct, $product) {
         switch($cartProduct['option']??null) {
              case 'exclusive': return $product['exclusive'];
              case 'unlimited' : return (int)$product['unlimited'];
              case 'premium' : return (int)$product['premium'];
              default : return (int)$product['basic'];
          }
    }, $cartProduct, $product);
Vahe
  • 1,699
  • 3
  • 25
  • 76
  • I followed your answer just as you said @Vahejabagchourian but now I am get a different error which is...: Fatal error: Uncaught Error: Call to a member function bind_param() on bool in /Library/WebServer/Documents/J_Renzo_Mobile_Website_Draft/core/database/CreateDb.php:72 Stack trace: #0 /Library/WebServer/Documents/J_Renzo_Mobile_Website_Draft/cart.php(88): CreateDb->getProductById(1) #1 {main} thrown in /Library/WebServer/Documents/J_Renzo_Mobile_Website_Draft/core/database/CreateDb.php on line 72 – johnnyjohn400 Aug 10 '21 at 21:48
  • Post the bind_param statement, which is located in getProductById function – Vahe Aug 10 '21 at 21:53
  • 1
    ok I just posted it up in my post. I really appreciate your time. – johnnyjohn400 Aug 10 '21 at 22:19
  • 1
    @johnnyjohn400 `Call to a member function bind_param() on bool` means your call to prepare() failed. (You could google this error and find that out very quickly, and how to fix it). Turn on mysqli error reporting to get a more detailed error message - see https://stackoverflow.com/a/22662582/5947043 for instructions – ADyson Aug 10 '21 at 22:21
  • $sql = "SELECT * FROM Productdb WHERE id = ?"; is a prepared statement, so when bind_param is used, use a named param like :id instead of the question mark (?) – Vahe Aug 10 '21 at 22:21
  • 1
    @VaheJabagchourian No, mysqli doesn't support named parameters. You're probably thinking of PDO. – ADyson Aug 10 '21 at 22:22
  • 1
    (int)$cartProduct['product_id'] is passed in as an argument to the getProductById function, can you double check that product_id key is in cartProduct via array_key_exists('product_id', $cartProduct) && isset($cartProduct['product_id']) before calling the getProductById, or at least echo the value of $cartProduct['product_id'] to see if it is undefined (i.e. likely key does not exist, or value is null)? – Vahe Aug 10 '21 at 22:27
  • 1
    I used print_r to check it. It exists and the value is currently 1. – johnnyjohn400 Aug 10 '21 at 22:48
  • 1
    Apologies for leading down the wrong path, it appears that $this->con is false, and that your property of your object (namely $con) does not have a proper handle to the database, i'd suggest looking at the function that sets con and troubleshoot it, usually when the error message says call to function on some type, it means the object is not valid (in this case false, since the php highlights that your object is bool, and a connection handle is either valid object or false if there is an error in this case. – Vahe Aug 10 '21 at 22:51
  • 1
    Thank you @VaheJabagchourian and everyone. The last problem was that I misnamed my table I needed to put $this->tablename instead of just tablename. – johnnyjohn400 Aug 10 '21 at 23:54
  • Thank you everyone once again for the help. Just to wrap things up and provide some context for anyone experiencing this same problem. The main problem was that I was using the wrong code for my php version. The next thing was that my database needed to be named correctly as $this->tablename. This code was put together using multiple sources and as a no experienced coder I kept getting confused. Anyways, this code is used to display my products in a cart summary page. These products have multiple price options depending on what package you want to purchase (cont…) – johnnyjohn400 Aug 11 '21 at 00:09
  • (Continued) As you can see from the code I have different price options which are basic, premium, etc. What I was trying to do was pull the product from the database table by its id and then match it with the correct price for displaying. I had another code that was working fine before this but it wasn’t the best way to do it because when my product selection gets bigger and more complicated it would be hard to maintain. Anyways, this code slaps the product id and its correct price together and displays it in the cart summery. I hope this code saves anyone else hours of head ache. – johnnyjohn400 Aug 11 '21 at 00:15