php-db | 2002091

[PHP-DB] heeellp.. radnom display working but not quite what I want.. From: Aaron Wolski (aaronjw <email protected>)
Date: 09/13/02

Basically what I was trying to do is... (keep in mind this is all static
<http://forums.devshed.com/images/smilies/frown.gif> )

In my shopping cart.. if a user HAD select a particular product.. then
display a maximum of 3 "Suggested Products" to them to add to their
cart.

Now.. all worked fine until I went to check to see if one of the
"suggested products" was already in the cart to not display that
suggested product again.

Here is the code I am using:

PHP:

  _____

$cartQuery = db_query("SELECT product_index FROM CartTable WHERE
cart_id='$cart_id'");

    while ($cartResult = db_fetch($cartQuery)) {

$rows = array();

$rows[] = "$product4";

$rows[] = "$product5";

$rows[] = "$product6";

$rows[] = "$product7";

$rows[] = "$product8";

srand( ( float ) microtime() * 10000000 );

$keys = array_rand( $rows, 3 );

if (!($cartResult["product_index"] == 4)) { echo $rows[$keys[0]].'<BR>';
}

if (!($cartResult["product_index"] == 5)) { echo $rows[$keys[1]].'<BR>';
}

if (!($cartResult["product_index"] == 6)) { echo $rows[$keys[2]].'<BR>';
}

if (!($cartResult["product_index"] == 7)) { echo $rows[$keys[3]].'<BR>';
}

if (!($cartResult["product_index"] == 8)) { echo $rows[$keys[4]].'<BR>';
}

  _____

Note: the $product variables reference HTML code for the "add product to
cart function". All very basic.

Now.. from my understanding.. shouldn't what I have work? I AM cycling
through all product_index in the CartTable. I am checking to see if they
don't exist then to display the corresponding $product variable through
$row[]

Oh.. what is IS doing is displaying 3 random $product variables
regardless of whether or not they are already selected in the shopping
cart.

I just don't get it.

Can someone show me the light here?