473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

array from query then random gen. of array

I'm trying to make an array from a query and then randomly select 10 values from the array, but the error i keep getting is that it's not reading as an array. Thanks in advance for anyone who can help.

$query = "SELECT Id FROM January WHERE Username='0' AND Day='1'";
$result = mysql_query($query);

while($row=mysql_fetch_assoc($result))
{
echo $row['Id'];
echo "<br>";
}

for($i=1; $i<=10; $i++){
$random = array_rand($row);
echo $random;
}
?>
Mar 17 '08 #1
10 1957
Markus
6,050 Expert 4TB
I'm trying to make an array from a query and then randomly select 10 values from the array, but the error i keep getting is that it's not reading as an array. Thanks in advance for anyone who can help.

$query = "SELECT Id FROM January WHERE Username='0' AND Day='1'";
$result = mysql_query($query);

while($row=mysql_fetch_assoc($result))
{
echo $row['Id'];
echo "<br>";
}

for($i=1; $i<=10; $i++){
$random = array_rand($row);
echo $random;
}
?>
Try:
[php]
<?php
$_query = mysql_query("SELECT * FROM `Members_Info`");

function rand_from_array($_data)
{
for($_i = 0; $_i < 10; ++$_i)
{
echo array_rand($_data);
}
}

while($_row=mysql_fetch_assoc($_query))
{
echo rand_from_array($_row) . "<Br />";
}
?>
[/php]
You'll have to adapt it (slight modifications)
Also untested!
Let me know if there's a problem!
Mar 17 '08 #2
When i tried that, all i get was 'Id' listed way too many times.
Mar 17 '08 #3
Markus
6,050 Expert 4TB
When i tried that, all i get was 'Id' listed way too many times.
Show me what you got.





...
:|
Mar 17 '08 #4
$_query = mysql_query("SELECT Ad_Id FROM January_Ad1 WHERE Username='0' AND Day='1'");

function rand_from_array($_data)
{
for($_i = 0; $_i < 10; ++$_i)
{
echo array_rand($_data);
}
}

while($_row=mysql_fetch_assoc($_query))
{
echo rand_from_array($_row) . "<Br />";
}
Mar 17 '08 #5
TheServant
1,168 Expert 1GB
Look at shuffle() and array_rand() functions here.

Sorry this didn't post, so only posted it a few replies later so it's out of date!
Mar 17 '08 #6
TheServant
1,168 Expert 1GB
Actually, look at that link. array_rand() takes another input which lets you choose how many elements you want to return, so that might be what you're after!
Mar 17 '08 #7
that definitely helped and put things in a more logical perspective. thanks. however, i remain with the same original problem. i feel as though this problem is because the array which i am trying to carry over from the while loop is not reading in the for loop. any further suggestions?
Mar 17 '08 #8
works properly now. finished code below.

$query = "SELECT Id FROM January WHERE Username='0' AND Day='1'";
$result = mysql_query($query);

$data = array();
while($row=mysql_fetch_assoc($result))
{
$data[] = $row['Id']; // store in array
}

$random = array_rand($data, 10); // get 10 random keys

foreach ($random as $key)
{
echo $data[$key];
echo "<br>";
}
Mar 18 '08 #9
TheServant
1,168 Expert 1GB
that definitely helped and put things in a more logical perspective. thanks. however, i remain with the same original problem. i feel as though this problem is because the array which i am trying to carry over from the while loop is not reading in the for loop. any further suggestions?
Why don't you use [PHP]array_rand($data, 10)[/PHP] instead of your for loop? Don't they do the same thing?


** ALSO: Start using appropriate code tags around any posted code!
Mar 18 '08 #10
TheServant
1,168 Expert 1GB
lol, I'm a bit slow today. Glad we could help ;)
Mar 18 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Greg Strong | last post by:
Hello All, Is it possible to create multiple random numbers in a query where there are numerous records? I've created a custom function. When I use it in a query it creates the same random...
4
by: James | last post by:
Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. Thanks
6
by: Paul van Brenk | last post by:
When you run the Shuffle method often enough it will throw exception. And I can't figure out why. Anybody? Paul van Brenk the code: static void Shuffle(){ int ints = { 1, 2, 3, 4, 5, 6,...
6
by: Les Juby | last post by:
I need to extract records from a database subject to conditions and only thereafter give the users the choice of which fields to sort the results on. In this situation I can't write back to a...
5
by: jar13861 | last post by:
I'm confused on how to write a random array that will only generate 9 different numbers from 1-9. Here is what I have, but its only writing one number.... holder = new Array ( 9 ); var flag =...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
6
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and...
9
by: Tuxedo | last post by:
I'd like to reorganize the third, fourth, fifth and sixth, as well as any elements thereafter in an array in random order: var a = new...
6
by: lukasso | last post by:
Hi, this is my code that should produce something like a timetable for a few days with each day divided into 30 minute pieces. It makes query from MySQL and then creates a 2d $array which then is to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.