Ty Jacobs Ty Jacobs - 23 days ago 7
Android Question

Retrieve Data From 2 Tables PHP, MySql, Android

Does anyone know of a way that I can gat data from 2 tables in a MySql DB using PHP? My android app allows users to place items in a shopping cart(table in MySql). When the user clicks on the shopping cart button on in the app it loads the "Shopping Cart Activity" and loads a listview with everything they have added to their cart based on a "Session ID" or "Customer ID" if they are logged in. What I do right now is when the item is added to the cart I capture the Product ID, Product Name, and Image URL, Quantity, and Price. It works great and quickly. What I am wanting to do is since the Product Name, image url, and price are already in the "Products Table" I want to do the same thing but when the user adds an item to their cart only capture the Product ID and session ID. When the customer goes to the Shopping Cart Activity I want my PHP Script to grab the Product ID in the Shopping_Cart table then Search the Products Tables for that Product ID and return the Price, Image URL, Name, etc.

Here is my PHP Script:

if($The_Function=="LOAD_CART"){
$response = array();

require_once __DIR__ . '/db_connect.php';

$con = new DB_CONNECT();

$user_session = $_GET['session'];
$user_item_status = $_GET['status'];
$order_total = 0.00;
$result = mysql_query("SELECT * FROM shopping_cart WHERE SessionID LIKE '$user_session' AND Status LIKE '$user_item_status'");

if(!empty($result)){
if (mysql_num_rows($result) > 0) {
$response["cart_contents"] = array();
//$result = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
$myItems = array();

$myItems["product_id"] = $row["PID"];
$myItems["product_name"] = $row["Item"];
$myItems["product_price"] = $row["Price"];
$myItems["product_qty"] = $row["Qty"];
$myItems["image_url"] = $row["URL"];
$myItems["line_id"] = $row["ItemCount"];
$line_total = $row["Price"] * $row["Qty"];
$order_total = $order_total + $line_total;
array_push($response["cart_contents"], $myItems);
}
// success
$response["success"] = 1;
$response["order_total"] = $order_total;
// user node
// $response["products"] = array();
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";

// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";

// echo no users JSON
echo json_encode($response);
}
}


Here is what I am trying to do. This code does not work.

if($The_Function=="LOAD_CART2"){
$response = array();

require_once __DIR__ . '/db_connect.php';

$con = new DB_CONNECT();

$user_session = $_GET['session'];
$user_item_status = $_GET['status'];
$order_total = 0.00;
$result = mysql_query("SELECT * FROM shopping_cart WHERE SessionID LIKE '$user_session' AND Status LIKE '$user_item_status'");

if(!empty($result)){
if (mysql_num_rows($result) > 0) {
$response["cart_contents"] = array();
//$result = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
$line_total = $row["Price"] * $row["Qty"];
$line_ID = $row["ItemCount"];
$Quantity = $row["Qty"];
$Prod_ID = $row["PID"];


$result2 = mysql_query("SELECT * FROM products WHERE pid LIKE '$Prod_ID'");
$myItems = array();
$theItem=$row["name"];
$thePrice=$row["price"];
$theImage=$row["prod_image"];

$myItems["line_id"] = $line_ID;
$myItems["product_qty"] = $Quantity;
$myItems["product_id"] = $Prod_ID;
$myItems["product_name"] = $theItem;
$myItems["product_price"] = $thePrice;
$myItems["image_url"] = $theImage;

$order_total = $order_total + $line_total;
array_push($response["cart_contents"], $myItems);
}




}
// success
$response["success"] = 1;
$response["order_total"] = $order_total;
// user node
// $response["products"] = array();
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";

// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";

// echo no users JSON
echo json_encode($response);
}


THIS IS WHAT I JUST TRIED:

if($The_Function=="LOAD_CART2"){
$response = array();

require_once __DIR__ . '/db_connect.php';

$con = new DB_CONNECT();

$user_session = $_GET['session'];
$user_item_status = $_GET['status'];
$order_total = 0.00;

$result = mysql_query("SELECT s.*, p.* FROM shopping_cart AS s LEFT JOIN products AS p ON p.pid = s.PID WHERE s.SessionID ='$user_session' AND s.Status = '$user_item_status'");

if(!empty($result)){
if (mysql_num_rows($result) > 0) {
$response["cart_contents"] = array();
//$result = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
$myItems = array();
//FROM shopping_cart Table
$line_total = $row["Price"] * $row["Qty"];
$myItems["line_id"] = $row["ItemCount"];
$myItems["product_qty"] = $row["Qty"];
$myItems["product_id"] = $row["PID"];

//FROM product TABLE
$myItems["product_name"]=$row["p.name"];
$myItems["product_price"]=$row["p.price"];
$myItems["image_url"]=$row["p.prod_image"];


}

$order_total = $order_total + $line_total;
array_push($response["cart_contents"], $myItems);


}
// success
$response["success"] = 1;
$response["order_total"] = $order_total;
// user node
// $response["products"] = array();
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";

// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";

// echo no users JSON
echo json_encode($response);
}


My Scheme:

products TABLE

pid|name|price|created_at|prod_image|description|catagory



shopping_cart TABLE

FirstName|LastName|OrderNumber|CustomerID|Email|Price|Qty|Status|URL|PID|SessionID|CustomerType|ItemCount

Answer

You should use a LEFT JOIN instead of doing a separate query for each item.

Something like: "SELECT s.*, p.* FROM shopping_cart AS s LEFT JOIN products AS p ON p.pid = s.PID WHERE s.SessionID ='$user_session' AND s.Status = '$user_item_status'

Assuming lowercase pid is in the products table, and capital PID is in the shopping_card table.