Jump to content

[SOLVED] Randomiseing and such likes


burtybob

Recommended Posts

I have a code that should update a table named drugs after 2 hours however when i try the code i get the data from the database but it then goes into minus plus can some one look at my code and tell me why it goes minus instead?

 

<?php
$con = mysql_connect("sql address","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

$londonpdp=mt_rand(99,999);
$oslopdp=mt_rand(98,999);
$cairopdp=mt_rand(99,999);
$shanghaipdp=mt_rand(99,999);
$parispdp=mt_rand(99,999);
$dublinpdp=mt_rand(99,999);
$riopdp=mt_rand(99,999);
$stockholmpdp=mt_rand(99,999);
$athenspdp=mt_rand(99,999);
$montrealpdp=mt_rand(99,999);
$washingtonpdp==mt_rand(99,999);
    $time1=mysql_query("SELECT pricetime FROM drugs");
    $outtime=time()+(24*60*60); //24hours
    $time4= $time1-time();
if ($time4<=0) {
mysql_query("UPDATE drugs SET londonprice = '$londonpdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET osloprice = '$oslopdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET cairoprice = '$cairopdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET shanghaiprice = '$shanghaipdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET washingtonprice = '$washingtonpdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET dublinprice = '$dublinpdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET parisprice = '$parispdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET rioprice = '$riopdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET stockholmprice = '$stockholmpdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET athensprice = '$athenspdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET montrealprice = '$montrealpdp' WHERE drugname = 'pot'");
mysql_query("UPDATE drugs SET pricetime = '$outtime' WHERE drugname = 'pot'");
}
mysql_close($con);

echo "$outtime <br>";
echo "Time 4: $time4 <br>";
?>
d

Link to comment
https://forums.phpfreaks.com/topic/68087-solved-randomiseing-and-such-likes/
Share on other sites

Try this

<?php
$con = mysql_connect("sql address","username","password") or die('Could not connect: '.mysql_error());
mysql_select_db("database", $con);

$prices = array();
foreach(array('london', 'oslo', 'cairo', 'shanghai', 'paris', 'dublin', 'rio', 'stockholm', 'athens', 'montreal', 'washington') as $city)
{
$prices[$city] = mt_rand(99, 999);
}

list($time1) = mysql_fetch_array(mysql_query("SELECT pricetime FROM drugs"));
$outtime = time()+(24*60*60); //24hours
$time4= $time1-time();
if($time4<=0)
{
$sets = array();
foreach($prices as $city => $price)
{
	$sets[] = "{$city}price='{$price}'";
}
mysql_query("UPDATE drugs SET ".join(',',$sets)." WHERE drugname = 'pot'");
}
mysql_close($con);

echo "$outtime <br>";
echo "Time 4: $time4 <br>";
?>

 

You might want to reconsider your database layout. It's better to store each city as an entry for itself rather than having a lot of fields.

What goes into "minus"? The prices?

No the prices are fine but the time goes into minus when i go on the page the time from the DB - Current time for some reason seem to go into minuse.

1189102314 <---- Time now

Time 4: -1189015805 <------ Time in DB - Time now

 

So because it never hits 0 it never automatically updates the DB.

Thanks one of my mates that helps to code, has just realised that the time in the db that it is getting is the first one on the list which is why it is totally messing so thank you for your help, even though it idnt work it has tidied my code up a lot.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.