简体   繁体   中英

PHP: Require a file with prices

I am implementing a small online purchase page with a very few products.

<?php  
$item_name = "lamp";
$item_price = 29.99;
$item_category = "furniture";
$item_Dollar = 29;
$item_Cents = 99;
?>

How do I go about it? It's only 2 product types and basically only prices would differ so there is no need for a database. I was thinking of including a .php file containing all the prices and having a function there that will switch($id) between them and reassign the values.

<?php
    function selectProduct($id) {

     switch($id) {
          case 1:
              $item_name = "lamp";
              $item_price = 19.99;
              $item_category = "furniture";
              $item_Dollar = 19;
              $item_Cents = 99;
              break;

          case 2:
              // and so on...

          default: break;
     }
    }
?>

The logic enters the respective cases but it does not reassign the instance variables. For some reason when I try to access them on another page after require_once(pricings.php); they are empty. I am not very familiar with PHP which probably made itself obvious from the code. What am I doing wrong?

I would build an array of items, where each element is a key/value pair, the key being the item id and the value being another array of key/value pairs for the item's attributes:

$items = array(
    1 => array(
        'name' => 'lamp',
        'price' => '19.99',
        'category' => 'furniture',
    ),
    2 => array(
        'name' => 'chair',
        'price' => '29.99',
        'category' => 'furniture',
    ),
);

Then you can simply refer to the array by using its product id as the index:

$id = 1;
echo "Name is: " . $items[$id]['name'];
echo "Price is: " . $items[$id]['price'];

Personally I would forget about functionizing your data and just use an array. Thats what there for. also this will make it much easier to port your data to a database in the future.

<?php 
$products = array(
    array('item_name'=>'lamp', 'item_price'=>19.99, 'item_category'=>'furniture', 'item_dollar'=>19, 'item_cents'=>99),
    array('item_name'=>'chair', 'item_price'=>19.99, 'item_category'=>'furniture', 'item_dollar'=>19, 'item_cents'=>99),
);
//
echo $products[0]['item_name'];
?>

I would store your items as a JSON object, which can easily be transformed to a native php array, and also would allow a straightforward approach to updating and adding items as needed. I won't get into those benefits here, but the items could be stored as:

{
  "1": {
    "name": "lamp",
    "price": "19.99",
    "category": "furniture"
  },
  "2": {
    "name": "chair",
    "price": "29.99",
    "category": "furniture"
  }
}

In a file such as items.json and when needing to interact with it, convert it to native php:

$items_json = file_get_contents('items.json');
$items = json_decode($items_json, true);

Then as Alex has shown, you can interact with your item list like so:

$id = 1;
echo "Name is: " . $items[$id]['name'];
echo "Price is: " . $items[$id]['price'];

To change variables scope, add global keyword and list all global variables, try:

function selectProduct($id) {
  global $item_name, $item_price, $item_category, $item_Dollar, $item_Cents;

but it's not good practise. imho, you should wrap data into object, and return objects, ie:

function selectProduct($id) {
  $item = new StdClass;
  //...
  switch($id) {
    case 1: $item->name = "name";
            //...
            break;
    //...
  }
  return $item;
}
<?php
$array_to_use = array();  
$array_to_use['item_name'] =  $item_name = "lamp";
 $array_to_use['item_price'] = $item_price = 29.99;
 $array_to_use['item_category'] = $item_category = "furniture";
 $array_to_use['item_Dollar'] = $item_Dollar = 29;
$array_to_use['item_Cents'] =  $item_Cents = 99;
?>

<?php
    function selectProduct($id) {
    global $array_to_use; 
   extract($array_to_use); 
     switch($id) {
          case 1:
              $item_name = "lamp";
              $item_price = 19.99;
              $item_category = "furniture";
              $item_Dollar = 19;
              $item_Cents = 99;
              break;

          case 2:
              // and so on...

          default: break;
     }
    }
?>

Updated Should work .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM