Skip to main content
Participating Frequently
December 8, 2010
Answered

Filter one recordset using another.

  • December 8, 2010
  • 1 reply
  • 1380 views

I have two tables called shopping_cart and products which have the following columns:

products - product_id, name, size, price, comments, stock

shopping_cart - cart_id, product_id, quantity, session_id

I want to be able to display the contents of my shopping cart on screen, using only the items in the table shopping_cart filtered by session_id but with all of the product information which is held in the products table.

I have created a record set to filter the shopping cart by session_id without a problem, but don't know how to pull the information from the products table for each entry?

Using PHP/MySQL - have very limited knowledge I'm afraid.

Thanks

Mark

This topic has been closed for replies.
Correct answer David_Powers

Hi David  thanks for the reply, it's your fault I'm in this state after doing your tutorial, I thought I could change the world....

I am so very nearly there.

If I specify a username in my query it is working for me, but if I try to get the session variable to sit in it's place in the qhery it doesn't.  Here is the code generated in both instances:

Specified - username 'marcobraynio'

mysql_select_db($database_boutique_wines, $boutique_wines);
$query_getCart = "SELECT cart.product_id, cart.quantity, cart.session_id, product.price, product.winery, product.colour, product.grape, product.year FROM cart, product WHERE cart.session_id = 'marcobraynio' AND cart.product_id = product.product_id ";
$getCart = mysql_query($query_getCart, $boutique_wines) or die(mysql_error());
$row_getCart = mysql_fetch_assoc($getCart);
$totalRows_getCart = mysql_num_rows($getCart);?>

Pulling in the Session Variable in the place of 'marcobraynio' I get this which doesn't work.  I have started the session at the top of the page.

$colname_getCart = "-1";
if (isset($_SESSION['session_id'])) {
  $colname_getCart = $_SESSION['session_id'];
}
mysql_select_db($database_boutique_wines, $boutique_wines);
$query_getCart = sprintf("SELECT cart.product_id, cart.quantity, cart.session_id, product.price, product.winery, product.colour, product.grape, product.year FROM cart, product WHERE cart.session_id = %s AND cart.product_id = product.product_id ", GetSQLValueString($colname_getCart, "text"));
$getCart = mysql_query($query_getCart, $boutique_wines) or die(mysql_error());
$row_getCart = mysql_fetch_assoc($getCart);
$totalRows_getCart = mysql_num_rows($getCart);
?>

I don't understand the code generated here - DW has placed a %s where I thought $_SESSION['session_id'] would go.

Cheers

Mark


marcobraynio wrote:

I don't understand the code generated here - DW has placed a %s where I thought $_SESSION['session_id'] would go.

The code that Dreamweaver has generated is correct. Dreamweaver uses the sprintf() function to insert the variable into the SQL query. Getting your head around sprintf() can be difficult to begin with. The function takes a minimum of two arguments. The first argument is the string that you want to insert variables into. Instead of inserting the variables directly in the string, you use placeholders that always begin with %. In this case %s represents a string.

The remaining argument(s) are the values you want to insert in place of the placeholders. If you look at the script generated by Dreamweaver, the second argument to sprintf() is GetSQLValueString($colname_getCart, "text"). GetSQLValueString() is a custom function created by Dreamweaver that checks the value of a variable, and makes it safe to insert in a SQL query. In your case, $colname_getCart is the equivalent of $_SESSION['session_id'].

There shouldn't be any problem using $_SESSION['session_id'] because PHP uses a different value to refer to the session ID (the internal one that identifies the session, not your variable). However, to avoid confusion, it would be better to use a different name, such as $_SESSION['user_id'].

If your query is working with a hard-coded name, but not with the session variable, it means that the session variable isn't being recognized on the page. Use echo to check whether the variable is being passed to the page.

if (isset($_SESSION['user_id'])) {

  echo $_SESSION['user_id'];

} else {

  echo 'Session variable does not exist';

}

1 reply

Participating Frequently
December 8, 2010

Unless I am misunderstanding you, you don't need two recordsets for this. Just use a single recordset with SQL that joins the two tables.

Participating Frequently
December 8, 2010

Thanks for the reply.  Could you tell me how to do that?  I really have no idea.

David_Powers
Inspiring
December 8, 2010

Do a Google search for MySQL join tutorial. You'll find lots of resources.