Author Topic: Uning RAND() within a function?  (Read 267 times)

twcast

  • New Member
  • *
  • Posts: 9
  • Karma: 0
    • View Profile
Uning RAND() within a function?
« on: May 14, 2012, 10:17:53 AM »
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 Code: [Select]
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($orderSubTotal2);

	
	
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\">



<span>Recommendations:</span>


	
	


	
	


	
	


	
</div>"
;

twcast

  • New Member
  • *
  • Posts: 9
  • Karma: 0
    • View Profile
Re: Using RAND() within a function?
« Reply #1 on: May 14, 2012, 04:04:32 PM »
I have the RAND working now, but will not stay within the table.

Any suggestions?
PHP Code: [Select]
<td><b><p align=\"left\">Recommendations:</b></p><br/>";

	
	
	
	


	
	
	
	
$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 .= "

</td></tr>

	
	
	
</table>

Ojoshiro

  • Technocentror
  • Senior Member
  • ****
  • Posts: 150
  • Karma: 4
  • That is not dead which can eternal loop...
    • View Profile
Re: Uning RAND() within a function?
« Reply #2 on: May 15, 2012, 08:04:53 AM »
What if you put all the productID's in an array and rand() over the length of the array, using
PHP Code: [Select]

$chosenId 
$IdArray[rand(0,count($IdArray)-1)];

to get a random productID?

O.
T.A.N.S.T.A.A.F.L.
_______________________________/