Jump to content

functions not returning a value


Dakoach1

Recommended Posts

I am trying to write a custom shopping cart based on the code I have found in books and in web searches. Basically I have a form that returns between 2 and 9 items at once as hidden variables and url items. There are 2 items relating to printing costs that require additional calculation before I can add them to the cart. When these 2 variables are not present (PID4 and PID6) the cart works fine. When they are present it displays the show_cart table but the descriptions and prices are empty. It also seems to double the insert for these 2 items in the database. I am not a programmer so I need lots of help here.

here is the show_cart code.

<?php

@$new = $_GET["new"];

@$quote = $_GET["quote"];

 

if(isset($new)||isset($quote)){

if(!isset($_SESSION['cart'])) {

$_SESSION['cart'] = array();

$_SESSION['total_price'] = '0.00';

$_SESSION['shipping_wt'] = 0;

}

 

if(isset($quote)) {

$_SESSION['cart'][$quote] = 1;

$qty=1;$planno=$quote;

calculate_sw($planno,$qty);

}

 

if(isset($new)){

for($i=1; $i<9; $i++) {

if(isset($_POST['PID'.$i])) {

if(isset($_POST['PID4'])){

$no=$new;

$planno=$_POST['PID1'];

$qty=$_POST['PQT1'];

insert_custom_print($planno,$qty,$no);

$_SESSION['cart'][$code]=1;

}elseif(isset($_POST['PID6'])) {

$no=$new;

$planno=$_POST['PID1'];

$qty=$_POST['PQT2'];

insert_custom_print($planno,$qty,$no);

$_SESSION['cart'][$code]=1;

}else

$new=$_POST['PID'.$i];

if(isset($_SESSION['cart'][$new])) {

$_SESSION['cart'][$new] ++;

}else

$_SESSION['cart'][$new]=1;

}

}

}

if(isset($_POST['PID3'])) {

$_SESSION['shipping_wt'] += 1;

}

if(isset($_POST['PQT1'])) {

$planno=$_POST['PID1'];

$qty=$_POST['PQT1'];

calculate_sw($planno, $qty);

$_SESSION['shipping_wt'] += $sw;

}

if(isset($_post['PQT2'])) {

$planno=$_POST['PID1'];

$qty=$_POST['PQT2'];

$sw = 0;

$conn = db_connect();

calculate_sw($planno, $qty);

$_SESSION['shipping_wt'] += $sw;

}

$_SESSION['total_price'] = calculate_price($_SESSION['cart']);

}

if(($_SESSION['cart']) && (array_count_values($_SESSION['cart']))) {

display_cart($_SESSION['cart']);echo $_SESSION['shipping_wt'];

display_button("checkout.php", "GoToCheckout", "Go To Checkout");

display_button("archstyles.php", "ContinueShopping", "Continue Shopping");

display_button_empty("Empty.php?", "EmptyCart", "Empty Cart");

}else{

echo"There are no items in your cart.";

  display_button("archstyles.php", "ContinueShopping", "Continue Shopping");

}

?>

 

the linked (by way of include() ) functions are here:

function display_cart($cart) {

  // display items in shopping cart

 

  echo "<table border=\"0\" width=\"100%\" cellspacing=\"0\">

 

        <tr>

<th bgcolor=\"#cccccc\" align=\"left\" width=\"55%\">Item</th>

        <th bgcolor=\"#cccccc\" align=\"right\" width=\"15%\">Price</th>

        <th bgcolor=\"#cccccc\" align=\"right\" width=\"15%\">Quantity</th>

        <th bgcolor=\"#cccccc\" align=\"right\" width=\"15%\">Total</th>

        </tr>";

 

  //display each item as a table row

  foreach ($cart as $item => $qty)  {

    $plans = get_plan_details($item);

    echo "<tr>";

echo " ";

    echo "</td>";

    echo "<td align=\"left\" width=\"%55\">".$plans['description']."</td>

          <td align=\"right\" width=\"%15\">\$".number_format($plans['price'], 2)."</td>

          <td align=\"right\" width=\"%15\">".$qty."</td>

      <td align=\"right\" width=\"%15\">\$".number_format($plans['price']*$qty,2)."</td></tr>\n";

  }

  // display total row

  echo "<tr>

        <th colspan=\"".(2)."\" bgcolor=\"#cccccc\"> </td>

        <th align=\"right\" bgcolor=\"#cccccc\">Subtotal</th>

        <th align=\"right\" bgcolor=\"#cccccc\">

            \$".number_format($_SESSION['total_price'], 2)."

        </th>

        </tr>";

 

  echo "</form></table>";

}

 

function get_plan_details($item) {

  // query database for all details for each item

  if ((!$item) || ($item=='')) {

    return false;

  }

  $conn = db_connect();

  if (strpos($item, "CPQ") !== false) {

  $query = "select * from cpq where item='".$item."'";

  }else{ 

  $query = "select * from products where item='".$item."'";

  }

  $result = @$conn->query($query);

  if (!$result) {

    return false;

  }

  $result = @$result->fetch_assoc();

  return $result;

}

 

function calculate_price($cart) {

  // sum total price for all items in shopping cart

  $price = 0.0;

  if(is_array($cart)) {

    $conn = db_connect();

    foreach($cart as $item => $qty) {

// if (strpos($item, "CPQ") !== false) {

// $query = "select price from cpq where item='".$item."'";

// }else{

      $query = "select price from products where item='".$item."'";

// }

      $result = $conn->query($query);

      if ($result) {

        $item = $result->fetch_object();

        $item_price = $item->price;

        $price +=$item_price*$qty;

      }

    }

  }

  return $price;

}

 

function calculate_sw($planno, $qty) {

  // sum total weight for this items in shopping cart

$sw = 0.00;

$conn = db_connect();

$result = $conn->query("select sw from products where item='".$planno."'");

$row=$result->fetch_object();

$item_weight=$row->sw;

$sw =$item_weight*$qty;

  return $sw;

}

 

function insert_custom_print($planno, $qty, $no) {

$conn=db_connect();

$result = $conn->query("select * from products where item = '".$planno."'");

$row=$result->fetch_object();

$pps=$row->pps;

$price=$pps*$qty;

$desc=$qty." sets of prints.";

$con = mysql_connect("localhost", "webguest", "guest");

if (!$con)

{

die('Could not connect:' .mysql_error());

}

mysql_select_db("info", $con);

mysql_query ("INSERT INTO cpq(item, description, price) VALUES ('temp', '".$desc."', '".$price."')");

$qn=mysql_insert_id();

$code=$no."CPQ".$qn;

mysql_query("UPDATE cpq SET item = '".$code."' WHERE id = '".$qn."'");

mysql_close($con);

return $code;

}

 

Please help!! I've not got much hair left.

I have isolated each function on a separate file and they run as expected but the "calculate_sw" function doesn't return a value to show_cart and neither does the insert_custom_print() function unless I embed them in the show_cart page. 

Link to comment
https://forums.phpfreaks.com/topic/192577-functions-not-returning-a-value/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.