herghost Posted October 19, 2009 Share Posted October 19, 2009 Hi all, Can someone explain and give me a quick example of how I would go about this? I have 100 values in a database with colum headings of t_val and t_name. If I wanted to update all 100 of these with a rand number how would I do this? Would I do something like this? t_val1 = rand(1,10); What I want to do is have different variances in the rand as well. So ten would be rand(1,10) and the next ten would be rand(5,20) for example? Many Thanks Quote Link to comment Share on other sites More sharing options...
herghost Posted October 19, 2009 Author Share Posted October 19, 2009 Just re-read that and I am not sure how much sense it made so I will try again. I have a table in my database that looks like this: stock_id stock_name stock_desc stock_code t_val y_val Now there is 100 rows in this table, with stock_id running from 5 to 104. What I am trying to achieve is to update all these with a random value for t_val, while leaving the rest of the data intact. I cant seem to get my head around how I would do this! Quote Link to comment Share on other sites More sharing options...
severndigital Posted October 19, 2009 Share Posted October 19, 2009 I would pull the data first, then push the random number this is the short version, since I don't know your mysql interface. $array = "SELECT id FROM table"; for($i = 0; $i < count($array); $i++){ $update = "UPDATE table SET t_val='" . rand(1,10) . "' WHERE id='" . $array[$i] ."'"; } **EDIT ** this will NOT ensure that each entry gets a different random number, if that is needed. Quote Link to comment Share on other sites More sharing options...
herghost Posted October 19, 2009 Author Share Posted October 19, 2009 Hi, thanks for the reply. Unfortunately they will all need a different random number :~( Do you mind explaining what all the i++ business is as well? Not something I have come across before! Many thanks Quote Link to comment Share on other sites More sharing options...
jonsjava Posted October 19, 2009 Share Posted October 19, 2009 increment by 1 (lets say $i=0 ; 0+1 = 1; 1+1=2;) For each increment through the loop, it adds one, until it meets the max (count($array)) Quote Link to comment Share on other sites More sharing options...
herghost Posted October 19, 2009 Author Share Posted October 19, 2009 ahhh thanks, that clears that one up Any ideas on my original problem jonsjava? Thanks to both of you for your help sp far Quote Link to comment Share on other sites More sharing options...
herghost Posted October 19, 2009 Author Share Posted October 19, 2009 Would it be possible to do something like this? Grab rows 5 to 15 enter into array value of t_val for these rows count number of values generate rand(1,10) for each value counted replace values with rand output update rows 5 to 15 Hmm, logically this makes sense to me, however its not within my skills in php even to know if its possible! Quote Link to comment Share on other sites More sharing options...
jonsjava Posted October 19, 2009 Share Posted October 19, 2009 I've actually been working on an incrementing system for you. Hopefully, something like this will do the job: <?php $array = "SELECT id FROM table"; $res = mysql_query($array); for($i = 0; $i < count($res); $i++){ if (is_int($i/10) || $i == 0){ $rand_low = rand(1,5); $rand_high = rand(10,20); } $num = rand($rand_low,$rand_high); $update = "UPDATE table SET t_val=$num WHERE id='" . $res[$i] ."'"; } Quote Link to comment Share on other sites More sharing options...
herghost Posted October 19, 2009 Author Share Posted October 19, 2009 Many many thanks for your efforts include('../common/dbconnect.php'); $array = "SELECT stock_id FROM stocks"; $res = mysql_query($array); for($i = 0; $i < count($res); $i++){ if (is_int($i/10) || $i == 0){ $rand_low = rand(1,5); $rand_high = rand(10,20); } $num = rand($rand_low,$rand_high); $update = "UPDATE stocks SET t_val=$num WHERE stock_id='" . $res[$i] ."'"; } This is what I am running in my browser to match up with my database details, however nothing happens! No errors and no update in the database? Am I missing something obvious here? This is far more advanced than the stuff I have been doing! Quote Link to comment Share on other sites More sharing options...
jonsjava Posted October 19, 2009 Share Posted October 19, 2009 forgot to add this: <?php $array = "SELECT id FROM table"; $res = mysql_query($array); for($i = 0; $i < count($res); $i++){ if (is_int($i/10) || $i == 0){ $rand_low = rand(1,5); $rand_high = rand(10,20); } $num = rand($rand_low,$rand_high); $update = "UPDATE table SET t_val=$num WHERE id='" . $array[$i] ."'"; if (mysql_query($update)){ echo "Updated $i record(s)<br />\n"; } } Quote Link to comment Share on other sites More sharing options...
herghost Posted October 19, 2009 Author Share Posted October 19, 2009 thanks, get the message updated 0 records?! However, i have a row with an id of 0 which is meant to stay at a constant value of 2, however it is updating this but not rows 5+ etc! Quote Link to comment Share on other sites More sharing options...
jonsjava Posted October 19, 2009 Share Posted October 19, 2009 Try this method: <?php $array = "SELECT id FROM table"; $res = mysql_query($array); $count = 0; while ($row = mysql_fetch_assoc($res)){ if (is_int($count / 10) || $count == 0){ $rand_low = rand(1,5); $rand_high = rand(10,20); } $num = rand($rand_low,$rand_high); $update = "UPDATE table SET t_val=$num WHERE id='" . $row['id'] ."'"; if (mysql_query($update)){ echo "Updated $i record(s)<br />\n"; } } Change the querries to suit your schema. Quote Link to comment Share on other sites More sharing options...
herghost Posted October 19, 2009 Author Share Posted October 19, 2009 Thanks, that updates them once I had removed the reference to $i, Ill have a look at this and see if I can think of anything else to ask you! Many thanks for all your help Quote Link to comment Share on other sites More sharing options...
jonsjava Posted October 19, 2009 Share Posted October 19, 2009 that was broken code I gave you. Sorry. Here's a fix: <?php $array = "SELECT id FROM table"; $res = mysql_query($array); $count = 0; while ($row = mysql_fetch_assoc($res)){ if (is_int($count / 10) || $count == 0){ $rand_low = rand(1,5); $rand_high = rand(10,20); } $num = rand($rand_low,$rand_high); $update = "UPDATE table SET t_val=$num WHERE id='" . $row['id'] ."'"; $count++; if (mysql_query($update)){ echo "Updated $count record(s)<br />\n"; } } Quote Link to comment 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.