Jump to content

help please...


brown2005

Recommended Posts

Hi, can someone please help...

right what i have is one table

table 1 and the field in this is number

then i have 32 records 1,2,3,4, etc... 30,31,32

now i have

table2 and a field in this called randomnumber...

and what i want to do is update 32 records in table2 with the records from table1 but randomly and not in the order of 1,2,3, etc...
Link to comment
Share on other sites

I dont know the name of the field you have your numbers 1-32 in, so i've called it ID.
I have assumed there is an auto-increment field on table2 called 'id'.
[code]
<?php // SELECT 32 ENTRIES FROM table1 in random order
$strSQL = "SELECT * FROM table1 ORDER BY rand() LIMIT 32";
$result = mysql_query($strSQL) or die ("Invalid query");
$num = mysql_num_rows($result);

$count = 1;
// FOR EACH RESULT, INSERT THE NUMBER INTO THE randomnumber FIELD OF table2
for($i=0;$i<$num;$i++)
{
$number= mysql_result($result,$i,"number");
$insert_qry = "UPDATE table2 SET randomnumber=$number WHERE id=$count";
$insert_rslt = mysql_query($insert_qry);
$count++;
}
?>
[/code]

It might not do exactly what you want, and might not be 100% coded right as i've not tested it, but it should point you along the right path.

The important thing is to SELECT FROM table1 ORDER BY rand() and then to update one entry in table2 for each of the 32 results.

If you wanted to update 32 random results in table2, and table2 contains a lot more than 32 entries...

[code]
<?php // SELECT 32 ENTRIES FROM table2 in random order
$strSQL = "SELECT * FROM table2 ORDER BY rand() LIMIT 32";
$result = mysql_query($strSQL) or die ("Invalid query");
$num = mysql_num_rows($result);
$count = 1;

// FOR EACH RANDOM RESULT, INSERT $count NUMBER INTO THE randomnumber FIELD OF table2
for($i=0;$i<$num;$i++)
{
$id= mysql_result($result,$i,"id");
$insert_qry = "UPDATE table2 SET randomnumber=$count WHERE id=$id";
$insert_rslt = mysql_query($insert_qry);
$count++;
}
?>
[/code]

Hope this is of some use, or i've wasted my time lol.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.