Page 1 of 1

Random items.

Posted: Fri Jun 12, 2009 3:50 pm
by musson
I got a full working script that I need to do some changes but I am not sure how to do it.
It actually take 3 ramdom items in the whole product list.
What I want to do is to take 3 random items of random subcategorie but from a specified categorie.
Thanks for the help!!!

Code: Select all

//get 3 random products
$q1 = "select devbg_products.*, devbg_categories.*, devbg_subcategories.* from devbg_products 
                            left join devbg_categories on devbg_products.ItemCategory = devbg_categories.CategoryID
                            left join devbg_subcategories on devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID 
 
                            order by rand() limit 0,4 ";
 
$r1 = mysql_query($q1) or die(mysql_error());
 
if(mysql_num_rows($r1) == '0')
{
    $NoItems = "<center>You have no items at the database!</center>";
}
else
{
 


Thanks
-Musson

Re: Random items.

Posted: Fri Jun 12, 2009 5:32 pm
by requinix
Write some code to get three random subcategory IDs. (And then post it.)

Re: Random items.

Posted: Fri Jun 12, 2009 8:43 pm
by musson
...???

Re: Random items.

Posted: Fri Jun 12, 2009 9:33 pm
by requinix
I'd be happy to help, but I'm not going to do all the work for you ;)
If you can write that little bit of code (or at least try to) then I'll show you how to use it to answer your question.

Or you can wait to see if somebody else here gives you the answer.

Re: Random items.

Posted: Sun Jun 21, 2009 12:08 am
by musson
//get 3 random products
$q1 = "select devbg_products.*, devbg_products.ItemCategory=26, devbg_subcategories.* from devbg_products
left join devbg_categories on devbg_products.ItemCategory = 26
left join devbg_subcategories on devbg_products.ItemSubcategory = devbg_subcategories.SubcategoryID

order by rand() limit 0,3 ";

$r1 = mysql_query($q1) or die(mysql_error());

if(mysql_num_rows($r1) == '0')
{
$NoItems = "<center>You have no items at the database!</center>";
}
else
{
while($a1 = mysql_fetch_array($r1))
{
$MyPrice = number_format($a1[ItemPrice], 2, ".", "");

if(!empty($a1[ItemImage]))
{
$xy = @getimagesize("items_images/$a1[ItemImage]");
$x = $xy[0];
$y = $xy[1];

$NewItemName = str_replace("'", "", $a1[ItemName]);

$DisplayImage = "<a href=\"javascript:ShowPic('items_images/$a1[ItemImage]', '$NewItemName', '$x', '$y');\"><img src=\"items_images/$a1[ItemImage]\" width=100 alt=\"$a1[ItemName]\" border=0 style=\"border-color:black\"></a>";
}
else
{
$DisplayImage = "&nbsp;";
}

$DisplayIndex .= "
<table align=center width=300 border=0 rules=rows cellspacing=0 bordercolor=black>
<tr>
<td valign=top width=140>$DisplayImage</td>

<td width=160 valign=top>
<a class=\"ItemTitle\" href=\"ViewItem.php?ItemID=$a1[ItemID]\">$a1[ItemName]</a>
<br><BR>
<font color=black face=verdana size=1><b>Category: </b></font><a class=\"CategoryClass\" href=\"ShowCategory.php?CategoryID=$a1[ItemCategory]\">$a1[CategoryName]</a>";

if(!empty($a1[SubcategoryID]))
{
$DisplayIndex .= ", <a class=\"SubcategoryClass\" href=\"ShowCategory.php?CategoryID=$a1[ItemCategory]&SubcategoryID=$a1[ItemSubcategory]\">$a1[SubcategoryName]</a>";
}

$DisplayIndex .= " <br>
<font color=red face=verdana size=2><b>Price: $$MyPrice</b></font>
<br><br>
<form method=post action=add.php style=\"margin:0\">
<input type=hidden name=ItemID value=\"$a1[ItemID]\">
<input type=hidden name=ItemName value=\"$a1[ItemName]\">
<input type=hidden name=ItemShipping value=\"$a1[ItemShipping]\">
<input type=hidden name=ItemPrice value=\"$MyPrice\">
<input type=submit name=s1 value=\"add to cart\" class=sub>
</form>
</td>
</tr>
</table><br><hr size=1 color=E3E3E3>";
}

}

if(isset($NoItems))
{
echo $NoItems;
}






Im not able to do it so the item are only took from the categorie id 26.

Re: Random items.

Posted: Sun Jun 21, 2009 12:42 am
by requinix
But that is basically what you want, right? It's pulling random items from that one category.

So instead of using 26 use a PHP variable.

Code: Select all

"...left join devbg_categories on devbg_products.ItemCategory = $category..."
Then figure out the $category. Like, if you got it from the URL:

Code: Select all

$category = intval($_GET["category"]);
Do not forget to use intval!