未定义的索引问题和错误返回

时间:2015-07-27 14:04:41

标签: javascript php debugging

标记为已修复!!!

我在MySQL业务数据库中缺少vat_rate。这导致了两个问题。感谢Nytrix和Grimbode的快速和步行支持,非常感谢! :)

我在第16行收到了未定义的索引问题...

<?php
class Business extends Application {

private $_table = 'business';

public function getBusiness() {
    $sql = "SELECT * FROM `{$this->_table}`
            WHERE `id` = 1";
    return $this->db->fetchOne($sql);
}



public function getVatRate() {
    $business = $this->getBusiness();
    return $business['vat_rate'];
}
}

我该如何解决?感谢。

此外,当我点击我的添加到购物篮并从篮子链接中删除时,我收到了一个错误弹出窗口,这是从第13和14行的basket.js调用的。我不知道如何解决这个问题,但认为它可能与上述有关?

此致

编辑;以为我正确地完成了一个var_dump(?),这就是我写的时候为$ business显示的内容

<?php var_dump($business); ?> 

在我的_header.php文件中回显数组(我说虽然没有知识!)。

注意:未定义的变量:/ users / ashleysmith / Documents / Website Tests / Z中的业务 - 来自此/ ecommerce / template / _header.php的第68行 NULL

据我所知,这里是所有相关代码......

Business.php

<?php
class Business extends Application {

    private $_table = 'business';

    public function getBusiness() {
        $sql = "SELECT * FROM `{$this->_table}`
                WHERE `id` = 1";
        return $this->db->fetchOne($sql);
    }



    public function getVatRate() {
        $business = $this->getBusiness();
        return $business['vat_rate'];
    }
 }

basket_small_refresh.php

<?php
require_once("../inc/autoload.php");
$objBasket = new Basket();
$out = array();
$out['bl_ti'] = $objBasket->_number_of_items;
$out['bl_st'] = number_format($objBasket->_sub_total, 2);
$out['bl_vat'] = number_format($objBasket->_vat, 2);
$out['bl_total'] = number_format($objBasket->_total, 2);
echo json_encode($out);

basket_left.php

<div id="small_basket">

<?php $objBasket = new Basket(); ?>

    <p class="your_bas">Your Basket</p>

    <dl id="basket_left">

        <dt>No. of items:</dt>
            <dd class="bl_ti"><span><?php echo $objBasket->_number_of_items; ?></span></dd>

        <dt>Sub-total:</dt>
            <dd class="bl_st">&pound;<span><?php echo number_format($objBasket->_sub_total, 2); ?></span></dd>

        <dt>VAT (<span><?php echo $objBasket->_vat_rate; ?></span>%):</dt>
            <dd class="bl_vat">&pound;<span><?php echo number_format($objBasket->_vat, 2); ?></span></dd>

        <dt>Total (inc):</dt>
            <dd class="bl_total">&pound;<span><?php echo number_format($objBasket->_total, 2); ?></span></dd>
    </dl>



    <p class="check_button">
        <a href="/?page=basket">View Basket</a> | 
        <a href="/?page=checkout">Checkout</a>
    </p>

</div>

basket.php

<?php
require_once('../inc/autoload.php');
if (isset($_POST['job']) && isset($_POST['id'])) {
$out = array();

$job = $_POST['job'];
$id = $_POST['id'];

$objCatalogue = new Catalogue();
$product = $objCatalogue->getProduct($id);

if (!empty($product)) {

    switch($job) {

        case 0:
        Session::removeItem($id);
        $out['job'] = 1;
        break;

        case 1:
        Session::setItem($id);
        $out['job'] = 0;
        break;

        }
        echo json_encode($out);

    }
}

Basket.php

<?php
 class Basket {
  public $_inst_catalogue;
  public $_empty_basket;
  public $_vat_rate;
  public $_number_of_items;
  public $_sub_total;
  public $_vat;
  public $_total;


   public function __construct() {

    $this->_inst_catalogue = new Catalogue();
    $this->_empty_basket = empty($_SESSION['basket']) ? true : false;

    $objBusiness = new Business();
    $this->_vat_rate = $objBusiness->getVatRate();

    $this->noItems();
    $this->subtotal();
    $this->vat();
    $this->total();

  }


  public function noItems() {
    $value = 0;
    if (!$this->_empty_basket) {
        foreach($_SESSION['basket'] as $key => $basket) {
            $value += $basket['qty'];
        }
    }
    $this->_number_of_items = $value;
  }



    public function subtotal() {
      $value = 0;
      if (!$this->_empty_basket) {
        foreach($_SESSION['basket'] as $key => $basket) {
            $product = $this->_inst_catalogue->getProduct($key);
            $value += ($basket['qty'] * $product['price']); 
          }
      }     
      $this->_sub_total = round($value, 2);
    }



    public function vat() {
      $value = 0;
      if (!$this->_empty_basket) {
        $value = ($this->_vat_rate * ($this->_sub_total / 100));
      }     
      $this->_vat = round($value, 2);   
    }



    public function total() {
      $this->_total = round(($this->_sub_total + $this->_vat), 2);  
    }
    public static function activeButton($sess_id) {
      if(isset($_SESSION['basket'][$sess_id])) {
        $id = 0;
        $label = "Remove From Basket";
      } else {
        $id = 1;
        $label = "Add To Basket";
      }
      $out = "<a href=\"#\" class=\"add_to_basket";
      $out .= $id == 0 ? " red" : null;
      $out .= "\" rel=\"";
      $out .= $sess_id."_".$id;
      $out .= "\">{$label}</a>";
      return $out; 
      }
  }

basket.js

 $(document).ready(function() {

    function refreshSmallBasket() {

        $.ajax({
        url: '/mod/basket_small_refresh.php', 
        dataType: 'json',
        success: function(data) {
            $.each(data, function(k, v) {
                $("#basket_left ." + k + " span").text(v);
            });
        },
        error: function(data) {
            alert("Error occured");
        }

        });

    }

    if ($(".add_to_basket").length > 0) {
        $(".add_to_basket").click(function() {

        var trigger = $(this);
        var param = trigger.attr("rel");
        var item = param.split("_");

        $.ajax({

            type: 'POST',
            url: '/mod/basket.php',
            dataType: 'json',
            data: ({ id : item[0], job : item[1] }),
            success: function(data) {
                var new_id = item[0] + '_' + data.job;
                if (data.job != item[1]) {
                    if (data.job == 0) {
                        trigger.attr("rel", new_id);
                        trigger.text("Remove From Basket");
                        trigger.addClass("red");
                    } else {
                        trigger.attr("rel", new_id);
                        trigger.text("Add To Basket");
                        trigger.removeClass("red");
                        }
                        refreshSmallBasket();
                    }

                },
                error: function(data) {
                    alert("Error occured"); 
                }

            });
            return false;

        });

    }

});

2 个答案:

答案 0 :(得分:0)

好的,所以您选择了*来自您的所有内容,并将表定义为$_table = "business"。你想选择&#39; vat_rate&#39; returnnd数组中可能不存在的列。在这里,您使用var_dump检查了您的阵列外观。如果您想知道如何从该数组中选择一个值,请查看文档:{​​{3}}或http://php.net/manual/en/language.types.array.php(更简单的解释)

答案 1 :(得分:0)

标记为固定!!!

我在MySQL业务数据库中缺少vat_rate。这导致了两个问题。感谢Nytrix和Grimbode的快速和步行支持,非常感谢! :)