Uning RAND() within a function?

Greetings,
I am in th eprocess of building a pre-cart, and need to have a random set of rows pulled as recommended products. No more than 8.

My problem is, I have no clue how to do it.
the code would go at the bottom of the function under “Recommendations”.
Any help would be great indeed.

Thank you all.

[php]function precartitm(){

	if(empty($_SESSION['cart']) && !empty($_SESSION['cartlines'])){

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

		forEach($_SESSION['cartlines'] as $chkoutLine){

			unset($chkoutLine['shipvia']);

			$chkoutLine['item'] = $chkoutLine['id'];

			unset($chkoutLine['id']);

			$_SESSION['cart'][] = $chkoutLine;

		}

	}

	

	// 1) cartInitiate();

	// 2) $cart = cartLoadItems();

	

	cartInitiate();

	

	//check for items to remove

	if(!empty($_GET["removeItem"])){

		if($_GET["removeItem"] == "all"){

			forEach($_SESSION["cart"]["itemList"] as $cartKey => $cartLine){

				unset($_SESSION["cart"]["itemList"][$cartKey]);

			}

		}

		$remId = simpleQueryString($_GET["removeItem"], "");

		if(!empty($remId)){

			forEach($_SESSION["cart"]["itemList"] as $cartKey => $cartLine){

				if($cartLine['item'] == $remId){

					unset($_SESSION["cart"]["itemList"][$cartKey]);

				}

			}

		}

	}

	

	$check = array_merge($_GET, $_POST);

	$goBackOnContinue = "false";

	forEach($check as $postKey => $postVal){

		//check for added items ~ $_POST['additem_100098'] = '300'

		if(stripos($postKey, "additem_") !== false){

			$itemId = intval(str_ireplace("additem_", "", $postKey));

			$qty = intval($postVal);

			$goBackOnContinue = "true";

			

			if(!empty($itemId) && !empty($qty)){

				//verify item is real

				$res = select("id from inv_items where id = '".intval($itemId)."'");

				if(count($res) >= 1){

					//check if item is already in session

					$hitBool = false;

					forEach($_SESSION["cart"]["itemList"] as &$cartLine){

						if($cartLine['item'] == $itemId){

							$hitBool = true;

							//if so, replace qty

							$cartLine['qty'] = intval($cartLine['qty']) + intval($qty);

						}

					}

					unset($cartLine);

					if($hitBool == false){

						//else, add new cart line

						$_SESSION["cart"]["itemList"][] = Array(

							"item" => $itemId,

							"qty" => $qty,

							"via" => "norm"

							);

					}

				}

			}

		}

		//check for added items ~ $_POST['addextitem_TRXID'] = 'qty'

		if(stripos($postKey, "addextitem_") !== false){

			$itemId = simpleQueryString(str_ireplace("addextitem_", "", $postKey), "");

			$qty = intval($postVal);

			$goBackOnContinue = "true";

			

			if(!empty($itemId) && !empty($qty)){

				//verify item is real

				//check if item is already in session

				$hitBool = false;

				forEach($_SESSION["cart"]["itemList"] as &$cartLine){

					if($cartLine['item'] == $itemId){

						$hitBool = true;

						//if so, replace qty

						$cartLine['qty'] = intval($cartLine['qty']) + intval($qty);

					}

				}

				unset($cartLine);

				if($hitBool == false){

					//else, add new cart line

					$_SESSION["cart"]["itemList"][] = Array(

						"item" => $itemId,

						"qty" => $qty,

						"via" => "ext"

						);

				}

			}

		}

	}

	

	//check for set items ~ $_POST['setitem_100098'] = '300'

	forEach($_POST as $postKey => $postVal){

		if(stripos($postKey, "setitem_") !== false){

			$itemId = simpleQueryString(str_ireplace("setitem_", "", $postKey), "");

			$qty = intval($postVal);

			//echo "set item: $itemId, $qty<hr />";

			if(!empty($itemId) && !empty($qty)){

				//verify item is real

				$res = select("id from inv_items where id = '".intval($itemId)."'");

			

				//check if item is already in session

				$hitBool = false;

				forEach($_SESSION["cart"]["itemList"] as &$cartLine){

					if($cartLine['item'] == $itemId){

						$hitBool = true;

						//if so, replace qty

						$cartLine['qty'] = $qty;

					}

				}

				unset($cartLine);

				if((count($res) == 1) && ($hitBool == false)){

					//else, add new cart line

					$_SESSION["cart"]["itemList"][] = Array(

						"item" => $itemId,

						"qty" => $qty,

						"via" => "norm"

						);

				}else{

					//Unknown item was set to a price without it already being in cart...

					//case \spoof packet\: do nothing

				}

			}

		}

	}

	

	//verify quantities for cart items (round up to nearest bundle)

	forEach($_SESSION["cart"]["itemList"] as $cartLineKey => $cartLineVal){

		if(intval($cartLineVal["qty"]) == 0){

			//check for zero qty items in session, remove them from session

			unset($_SESSION["cart"]["itemList"][$cartLineKey]);

			continue;

		}

		

		

		//print_r($cartLineVal);

		if($cartLineVal["via"] == "norm"){

			//echo "<br />".$_SESSION["cart"]["itemList"][$cartLineKey]["qty"]."<br />";

			$_SESSION["cart"]["itemList"][$cartLineKey]["qty"] = checkItemRoundingQty($cartLineVal["item"], $cartLineVal["qty"]);

			//echo "<br />".$_SESSION["cart"]["itemList"][$cartLineKey]["qty"]."<br />";

		}

	}

	

	

	

	//load item list and qty list from session

	$cartItemList = cartLoadItems();

	

	//display items, descriptions, qtys, and prices.

	$buffer = "

	<div id=\"quickNav\">

		<a href=\"/?action=view&amp;cat=1\">Home</a>

		

	</div>



<div id=\"groups\">

	<div>

	<table width=\"100%\">

	<tr>

	<td width=\"10%\">

		<span><b>Item Added</p></b></span>


		</td>

		<td width=\70%\"></td>

		<td width=\"10%\" align=\"right\">

		<span><b><a href=\"http://packitright.com/cart/atn_\">Checkout</a></b></span></td>

		</tr></table>

		<table class=\"roundTableFormat\">

			<thead>

				<tr>

					<th class=\"br ls al\" colspan=\"2\"></th>

					<th class=\"br al\"></th>

					<th class=\"br\"></th>

					<th class=\"br\"></th>

					<th class=\"\"></th>

					<th class=\"bl ds\"></th>

				</tr>

			</thead>

			<tbody id=\"cartLineItems\">";

	$orderSubTotal = 0;

	$checkoutCartStr = "";

	forEach($cartItemList as $cartItem){

		//print_r($cartItem);

		$cartItem['id'] = simpleQueryString($cartItem['id'], "");

		$cartItem['via'] = simpleQueryString($cartItem['via'], "");

		$checkoutCartStr .= "{$cartItem['id']}|{$cartItem['qty']}|{$cartItem['shipopt']}|{$cartItem['via']};";

		

		$orderSubTotal += floatval(str_replace(Array("$", ","), Array("", ""), $cartItem['totalprice']));

		

		if($cartItem['via'] == "norm"){

			$cartItemRes = select("* from inv_items where id = {$cartItem['id']}");

			

			if($cartItem['shipopt'] == "MEL"){

				$cartItem['shipopt'] = "LOC";

			}

			

			$cartItemRes = $cartItemRes[0];

			$itemShipOpt = "LOC";

			if(stristr($cartItemRes["ship_couriers"], "ups") !== false){

				$itemShipOpt .= ", UPS";

			}

			if(stristr($cartItemRes["ship_couriers"], "sef") !== false){

				$itemShipOpt .= ", FRE";

			}

			if(stristr($cartItemRes["ship_couriers"], "noc") !== false){

				$itemShipOpt .= ", NOC";

			}

			$buffer .= "

					<tr>

						<td></td>

						<td>{$cartItem['qty']}</td>

						<td>{$cartItem['desc_name']}</td>

						<td>Has been added to your cart</td>

						<td></td>

						<td><a href=\"/?action=cart&amp;removeItem={$cartItem['id']}\">Remove</a></td>

						<td>

							

						</td>
						

					</tr>";

		}else if($cartItem['via'] == "ext"){

			$itemShipOpt = "LOC";

			//$cartItemRes = select("* from inv_items where id = {$cartItem['id']}");

			$buffer .= "

					<tr class=\"lineitem light\">

						<td class=\"lineitem br ds al\">{$cartItem['id']}<input type=\"hidden\" value=\"{$cartItem['id']}\" /></td>

						<td class=\"lineitem br ds al\">{$cartItem['id']}</td>

						<td class=\"lineitem br ls al\">{$cartItem['desc_name']}</td>

						<td class=\"lineitem br\"><input type=\"text\" value=\"{$cartItem['qty']}\" class=\"inpAddToCartQty wide\" /></td>

						<td class=\"lineitem br\">{$cartItem['unitprice']}</td>

						<td class=\"lineitem\">{$cartItem['totalprice']}</td>

						<td class=\"lineitem bl ds\">

							<a href=\"/?action=cart&amp;removeItem={$cartItem['id']}\">Remove</a>

							<input type=\"hidden\" value=\"{$cartItem['unitprice']}\" />

							<input type=\"hidden\" value=\"$itemShipOpt\" />

							<input type=\"hidden\" value=\"{$cartItem['shipopt']}\" />

						</td>

					</tr>";

		}

	}

	$orderSubTotalT = "$".number_format($orderSubTotal, 2);

	if(empty($cartItemList)){

		$buffer .= "

				<tr class=\"lineitem light\">

					<td class=\"lineitem br bl\" colspan=\"7\">

						<br />

						Your cart is currently Empty.<br />

						<br />

					</td>

				</tr>";

	}else{

		$buffer .= "

				<tr class=\"lineitem light\">

					<td class=\"lineitem br ls\" style=\"text-align: right;padding: 0.5em;\" colspan=\"5\"></td>

					<td class=\"lineitem\" style=\"padding: 0.5em;\"></td>

					<td class=\"lineitem bl ds\" style=\"padding: 0.5em;\"><a href=\"/?action=cart\"></a></td>

				</tr>

				<tr class=\"lineitem light\" name=\"TRFreightQuote\">

					<td class=\"lineitem br ls\" style=\"text-align: right;padding: 0.5em;\" colspan=\"5\"></td>

					<td class=\"lineitem checkoutQuoteValField\" style=\"padding: 0.5em;\"></td>

					<td class=\"lineitem bl ds\" style=\"padding: 0.5em;\"></td>

				</tr>

				<tr class=\"lineitem light lgtxt\">

					<td class=\"lineitem br ls\" style=\"text-align: right;padding: 0.5em;\" colspan=\"5\"></td>

					<td class=\"lineitem checkoutOrdTotValField\" style=\"padding: 0.5em;\"></td>

					<td class=\"lineitem bl ds\" style=\"padding: 0.5em;\"><a href=\"/?action=cart\"></a></td>

				</tr>

				<tr class=\"lineitem light\">

					<td class=\"lineitem br ls\" style=\"text-align: right;padding: 0.5em;\" colspan=\"5\"></td>

					<td class=\"lineitem checkoutOrdTaxValField\" style=\"padding: 0.5em;\"></td>

					<td class=\"lineitem bl ds\" style=\"padding: 0.5em;\"><a href=\"/?action=cart\"></a></td>

				</tr>";

	}

	

	//available tools: remove item, calculate freight, checkout, continue shopping

	$buffer .= "

			</tbody>

		</table><br />

		<script type=\"text/javascript\">window.onload = checkGroupOverflow;</script>

	</div>

</div>

<div id=\"groups\">

Recommendations:

</div>";[/php]

I have the RAND working now, but will not stay within the table.

Any suggestions?
[php]

<p align=“left”>Recommendations:
";
			$result = mysql_query("SELECT * FROM inv_items ORDER BY RAND() LIMIT 0,5");

			while($row = mysql_fetch_array($result))
				 {
					echo $row['desc_picture'] . " " . $row['desc_name'] . " " . $row['id'];
					echo "<br />";
				 }

        }

	

	//available tools: remove item, calculate freight, checkout, continue shopping

	$buffer .= "
		</table>[/php]

What if you put all the productID’s in an array and rand() over the length of the array, using
[php]
$chosenId = $IdArray[rand(0,count($IdArray)-1)];
[/php]
to get a random productID?

O.

Sponsor our Newsletter | Privacy Policy | Terms of Service