2013-04-16 2 views
-4

나는이정의되지 않은 변수 : 총

$total += $rows['price'] * $qty; 

공지 사항에 문제가있어 : 정의되지 않은 변수 : D 총 : \ XAMPP \ htdocs를 \ WBPL-MusicLightDev \ INC \ functions.inc.php을 라인 (42)

function wbpl_showCart() { 
    global $db; 
    $cart = $_SESSION['cart']; 
    if ($cart) { 
     $items = explode(',', $cart); 
     //$contents = array(); 
     foreach ($items as $item) { 
      $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; 
     } 
     echo '<form action="index.php?page=cart&action=update" method="post" id="cart">'; 
     echo '<table border=0 align="center" class="table table-bordered">'; 


     foreach ($contents as $id => $qty) { 
      $sql = "SELECT * from wbpl_product WHERE kd_product = '$id'"; 
      $result = mysql_query($sql) or die(mysql_error()); 
      $rows=mysql_fetch_array($result); 
      //extract($row); 

      echo '<tr> 
         <td>Brand</td> 
         <td colspan="4">'. $rows['kd_product'] .'</td> 
        <tr> 
        <tr> 
         <td>Brand</td> 
         <td colspan="4">'. $rows['nama_brand'] .'</td> 
        <tr> 
        <tr> 
          <td>Instrument Type</td> 
          <td colspan="4">'. $rows['nama_instype'] .'</td> 
        </tr> 
        <tr"> 
        <td rowspan="2">Price</td> 
        <td rowspan="2">Rp. ' . $rows['price'] . '</td> 
        <td rowspan="2"><input type="text" name="qty' . $id . '" value="' . $qty . '" size="2" maxlength="3" /></td> 

        <td rowspan="2">Rp. ' . ($rows['price'] * $qty) . '</td> 

        <td><a href="index.php?page=cart&action=delete&id=' . $id . '" class="btn btn-danger">Hapus</a></td> 
        </tr> 
        <tr><td><br></td></tr>'; 
      $total += $rows['price'] * $qty; 
     } 
     echo '</table>'; 
     $qty = getQty(); 


     echo '<p>Sub Total: <strong> Rp. ' . $total . '</strong></p>'; 



     //session_register('totalbayar'); 
     $_SESSION['totalbayar'] = $total; 
     echo '<div><button type="submit" class="btn btn-primary">Update cart</button></div>'; 
     echo '</form>'; 
    } else { 
     echo '<p>Keranjang belanja masih kosong.</p>'; 
    } 
    //return join('', $output); 
} 
+0

질문이 있으십니까? –

답변

2

존재하지 않는 값에 무언가를 추가 할 수 없습니다. 첫 번째 호출은 다음과 같습니다. null + = $ rows [ 'price'] * $ qty; 이것은 불가능하므로 추가하십시오.

$ total = 0;

foreach 루프 전에!

function wbpl_showCart() { 
    global $db; 
    $cart = $_SESSION['cart']; 
    if ($cart) { 
     $items = explode(',', $cart); 
     //$contents = array(); 
     foreach ($items as $item) { 
      $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; 
     } 
     echo '<form action="index.php?page=cart&action=update" method="post" id="cart">'; 
     echo '<table border=0 align="center" class="table table-bordered">'; 

     $total = 0; 
     foreach ($contents as $id => $qty) { 
      $sql = "SELECT * from wbpl_product WHERE kd_product = '$id'"; 
      $result = mysql_query($sql) or die(mysql_error()); 
      $rows=mysql_fetch_array($result); 
      //extract($row); 

      echo '<tr> 
         <td>Brand</td> 
         <td colspan="4">'. $rows['kd_product'] .'</td> 
        <tr> 
        <tr> 
         <td>Brand</td> 
         <td colspan="4">'. $rows['nama_brand'] .'</td> 
        <tr> 
        <tr> 
          <td>Instrument Type</td> 
          <td colspan="4">'. $rows['nama_instype'] .'</td> 
        </tr> 
        <tr"> 
        <td rowspan="2">Price</td> 
        <td rowspan="2">Rp. ' . $rows['price'] . '</td> 
        <td rowspan="2"><input type="text" name="qty' . $id . '" value="' . $qty . '" size="2" maxlength="3" /></td> 

        <td rowspan="2">Rp. ' . ($rows['price'] * $qty) . '</td> 

        <td><a href="index.php?page=cart&action=delete&id=' . $id . '" class="btn btn-danger">Hapus</a></td> 
        </tr> 
        <tr><td><br></td></tr>'; 
      $total += $rows['price'] * $qty; 
     } 
     echo '</table>'; 
     $qty = getQty(); 


     echo '<p>Sub Total: <strong> Rp. ' . $total . '</strong></p>'; 



     //session_register('totalbayar'); 
     $_SESSION['totalbayar'] = $total; 
     echo '<div><button type="submit" class="btn btn-primary">Update cart</button></div>'; 
     echo '</form>'; 
    } else { 
     echo '<p>Keranjang belanja masih kosong.</p>'; 
    } 
    //return join('', $output); 
} 
0
$total = 0; 

먼저 변수를 읽는 것을 의미 문제

0

$x += y를 해결할 수 $ 총을 사용하기 전에이 설정 그 다음 무언가를 추가 한 다음 결과를 (다시) 지정하십시오. foreach 루프의 첫 번째 반복 중에 += 연산자가 실행될 때 읽을 변수가 없으므로 경고가 표시됩니다.
루프 앞에 변수를 초기화하십시오.

$total = 0; 
foreach ($contents as $id => $qty) { 
    [...] 
    $total += something; 
1

$total 변수 선언

$total += ...; 수단 $total 값으로 ... 추가하지만 $total ISN : foreach는 루프로

문제 전에

$total = 0; 

것은 루프를 통해 첫 번째 반복에서 통지의 원인을 정의했습니다.

0

+= 연산자를 사용하려면 왼쪽 변수를 이미 정의해야합니다. 당신은 $total += ... 사용하기 전에 $total를 초기화하여이 문제를 해결할 수 : $total += $x이 내부적으로 $total = $total + $x로 변환되기 때문에

$total = 0; 
foreach (...) 
    $total += ... 

이, 당신이 정의되지 않은에서 $total를 사용하므로 처음이다.