/    Sign up×
Community /Pin to ProfileBookmark

Random generated values from an array of possible values

Hi i created the following function for a previous e-commerce site.

[code]$sql=”select MAX(productID) as prodID from Products”;
$result=mysql_query($sql) or die (“ERROR!! Unable to perform query!”);
$row=mysql_fetch_array($result);
$maxProdID=$row[‘prodID’];
$maxProdID–;

$id=(rand()%$maxProdID);
$id++;
[/code]

What this is used for is to display a random image each time a user enters/refreshes the site. The field productID is an auto increment and a product was never deleted from the database which meant that an image was always displayed. However im creating a new site now which asks that when an item is sold its deleted from the products table which means a random number which is generated may not have a corresponding entry in the products table. So my question is is there a way to generate a random number from an array of possible entries i.e. get all the productID from the tables and generate a random numbers from just those values. Hope i made myself clear.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderFeb 14.2007 — Populate an array with data then shuffle it.

[code=php]<?php
$productA = array("Product Name" => "Washing Machine", "Price" => "300.00");
$productB = array("Product Name" => "Toy Car", "Price" => "10.00");

$products = array();

$products[] = $productA;
$products[] = $productB;

// Dump before shuffle
var_dump($products);

shuffle($products);

// Dump aftershuffle
var_dump($products);
?>[/code]


Or maybe there is some way of doing ORDER BY RAND() in mysql?
Copy linkTweet thisAlerts:
@cs3mwauthorFeb 14.2007 — no ive done some rework on its and done the following:

$sql="select productID from Products";
$result=mysql_query($sql) or die ("ERROR!! Unable to perform query!");
$row=mysql_fetch_array($result);

$id=(array_rand($row,1));
echo"$id";


i found the new function array_rand which is supposed to generate a random value from the mentioned array in this case array called $row. in order to see whether the function is generating a random number I inserted the echo"$id"; but this is just displaying the values 0 and productID has anyone ant idea whats causing this?
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — The easiest would be:[code=php]$sql="SELECT productID FROM Products ORDER BY rand()";[/code]In your example, your array consits of only one row with only one element. It cannot return anything other than 0. For array_rand() to work as you wish, you'll need to feed it something larger.
Copy linkTweet thisAlerts:
@cs3mwauthorFeb 14.2007 — I done the following

$sql="select productID from Products order by rand()";
$result=mysql_query($sql) or die ("ERROR!! Unable to perform query!");
$id=mysql_fetch_array($result);
echo"$id";


which just gives the value array all the time
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — [code=php]$sql="select productID from Products order by rand()";
$result=mysql_query($sql) or die ("ERROR!! Unable to perform query!");
$row=mysql_fetch_array($result);
echo $row['productID'];[/code]
Copy linkTweet thisAlerts:
@cs3mwauthorFeb 14.2007 — thats done the trick thanks very much!!
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — You're welcome!
×

Success!

Help @cs3mw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...