473,471 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

random selection of CSV's

Hi all!

Is there a function in PHP-MySQL to randomly select comma-seperated
variables in a text field?

For example if you have a text field containing the values
"1,1.2,4,5.6," is there a way of randomly selecting one of the numbers?

Any ideas would be greatly appreciated!

Feb 23 '06 #1
8 1689
is array_rand the best bet?

Feb 23 '06 #2
monomaniac21 wrote:
Hi all!

Is there a function in PHP-MySQL to randomly select comma-seperated
variables in a text field?

For example if you have a text field containing the values
"1,1.2,4,5.6," is there a way of randomly selecting one of the numbers?

Any ideas would be greatly appreciated!


$selected = $arrayvar[ rand(0,array_count_values($record)-1) ]
get a random element within the array

not tested - YMMV.
Feb 23 '06 #3
monomaniac21 wrote:
is array_rand the best bet?


yes, that too... :)
Feb 23 '06 #4
Thanks noone, how can you convert a text field into an array? or how
can you create an array field in mysql?

Feb 23 '06 #5
monomaniac21 wrote:
Thanks noone, how can you convert a text field into an array? or how
can you create an array field in mysql?


I'd use the explode() function

<?php

$str = "1,2,3,4";
$arr = explode(",",$str);
print_r($arr);

?>
Feb 23 '06 #6
monomaniac21 wrote:
Thanks noone, how can you convert a text field into an array? or how
can you create an array field in mysql?

go to the docs and lookup arrays, explode

Two sites that will help you immeasurably!!
www.mysql.com
and http://us3.php.net/manual/en/funcref.php
then use the search feature
From the docs: http://us3.php.net/manual/en/function.explode.php
// Example 1
$pizza = "piece1,piece2,piece3,piece4,piece5,piece6";

// or you can get the field from a database - coding left to
// the OP - you learn better that way...

$pieces = explode(",", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
Feb 23 '06 #7
$string = $_POST['something'];

$chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);

list($pone,$ptwo,$pthree,$pfour,$pfive,$psix,$psev en) = $chars;

you can split a string into an array this way too...

Feb 23 '06 #8
monomaniac21 wrote:
Thanks noone, how can you convert a text field into an array? or how
can you create an array field in mysql?


You don't create an array field in MySQL. Rather, you create a second
table with a one-to-multiple linkage.

In this case you would have two columns - id and numval (or a more
appropriate name for whatever you're using it for). id would match the
id in the first table. numval would hold one value in each row. So,
you might end up with something like:

id numval
4 1
4 1.2
4 4
4 5.6
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Keith Griffiths | last post by:
I'm trying to do a search under a set criteria followed by a selection of random entries meeting this criteria. But I don't seem to be able to achieve this. The idea being to search on say...
21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
1
by: Micak | last post by:
I'm using radnom selection in ms access to select certain number of questions for the exam. i have two versions of the report: - one without marked answers - one with marked answers the problem...
1
by: David Barger | last post by:
Greetings, It appears that an Append Query I run in Access XP is randomly failing to append a field. I have payroll data being entered into a payroll database. This data is exported daily to...
8
by: Kari Lavikka | last post by:
Hi! I have to select a random row from a table where primary key isn't continuous (some rows have been deleted). Postgres just seems to do something strange with my method. -- -- Use the...
5
by: jordi | last post by:
I need the random.sample functionality where the population grows up to long int items. Do you know how could I get this same functionality in another way? thanks in advance. Jordi
5
by: k.i.n.g. | last post by:
Hi, I have a csv file which in taken as the input file for adding users in my linux mail server with the format userid,fullname,passwword,dateofbith Now I have to write a script to generate...
19
by: Boris Borcic | last post by:
does x.sort(cmp = lambda x,y : cmp(random.random(),0.5)) pick a random shuffle of x with uniform distribution ? Intuitively, assuming list.sort() does a minimal number of comparisons to ...
13
by: Bruza | last post by:
I need to implement a "random selection" algorithm which takes a list of as input. Each of the (obj, prob) represents how likely an object, "obj", should be selected based on its probability of...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.