brown2005 Posted April 12, 2006 Share Posted April 12, 2006 Hi, can someone please help...right what i have is one tabletable 1 and the field in this is numberthen i have 32 records 1,2,3,4, etc... 30,31,32now i havetable2 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... Quote Link to comment https://forums.phpfreaks.com/topic/7199-help-please/ Share on other sites More sharing options...
craygo Posted April 12, 2006 Share Posted April 12, 2006 Not that I do not want to help you out but why would you want to do this?? You could use php to randomize the output of the data rather than have another table.Ray Quote Link to comment https://forums.phpfreaks.com/topic/7199-help-please/#findComment-26229 Share on other sites More sharing options...
wisewood Posted April 12, 2006 Share Posted April 12, 2006 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 table2for($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 table2for($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. Quote Link to comment https://forums.phpfreaks.com/topic/7199-help-please/#findComment-26250 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.