Page 1 of 1

Weather[Resolved]

Posted: Sat Mar 05, 2011 1:47 pm
by Huezzer
Hey, I've been searching around on the forum, but can't find anything about this. The thing is that I've got some pictures that I've made in photoshop(Weather pictures) Like clouds etc., Is it possible to add these picture to a folder(with php code) that randomly set a weather when I log in.
One time I log in it's sunny, the other time Cloads on the picture, (Got both pictures). Hope you understand what I mean

Thanks, Huezzer.

Re: Weather

Posted: Sat Mar 05, 2011 2:21 pm
by alexander19
Maybe you could do a random thing,which checks everytime you log in:

Code: Select all

$chance = rand(0,100);
if($chance <=50)
//sunny weather
echo "<img src='images/weather/sunny_weather.jpg>";
elseif($chance <= 100)
//clouds
echo "<img src='images/weather/clouds_weather.jpg>";
.
.
. and so on
If you want to make sure that the weather image will be different than the previous time you logged in,you could create another tab in your players table and call it weather.
And then you could use something like this:

Code: Select all

$playerinfo = mysql_fetch_assoc(mysql_query("SELECT name,weather from players where name='$player'"));

$pl_weather = $playerinfo['weather'];

$chance = rand(0,100);

//now we are checking  if the last weather image the player got,wasn't sunny:

if($chance <=50 && $pl_weather!='sunny'){

//sunny weather

echo "<img src='images/weather/sunny_weather.jpg>";

$weather ='sunny';

mysqli_query("UPDATE players set weather ='$weather' where name='$player'");
}

elseif($chance <= 100 && $pl_weather!='clouds'){

//clouds

echo "<img src='images/weather/clouds_weather.jpg>";

$weather ='clouds';

mysqli_query("UPDATE players set weather ='$weather' where name='$player'");
}

Re: Weather

Posted: Sat Mar 05, 2011 9:10 pm
by Huezzer
Am I going to add this code:

Code: Select all

$chance = rand(0,100);
if($chance <=50)
//sunny weather
echo "<img src='images/weather/sunny_weather.jpg>";
elseif($chance <= 100)
//clouds
echo "<img src='images/weather/clouds_weather.jpg>";
In a new php document or where?, Login.php?

Thanks, Huezzer.

Re: Weather

Posted: Sat Mar 05, 2011 9:33 pm
by alexander19
You can add it on any php page you want to show the weather images.

Re: Weather

Posted: Sat Mar 05, 2011 9:40 pm
by Huezzer
Doesn't it update all the time then?, Or is it possible to make it same weather until I re-log in? :D


Thnaks, Huezzer.

Re: Weather

Posted: Sat Mar 05, 2011 9:52 pm
by alexander19
If you want it to be the same all the time and change only when you login,you must save it inside a cookie/session or db.
If you want to go with the db:
Use this on your authenticate.php script:

Code: Select all

$chance = rand(0,100);

if($chance <=50 ){

//sunny weather

mysqli_query("UPDATE players set weather ='sunny'  where name='$player'");
}

elseif($chance <= 100){

//clouds

mysqli_query("UPDATE players set weather ='clouds' where name='$player'");
}
And now,after you logged in you can get the weather from the db and echo the right image on every page:

Code: Select all

$playerinfo = mysql_fetch_assoc(mysql_query("SELECT name,weather from players where name='$player'"));

$pl_weather = $playerinfo['weather'];

if($pl_weather=='sunny')
//sunny weather
echo "<img src='images/weather/sunny_weather.jpg>";

elseif($pl_weather=='clouds')
//clouds
echo "<img src='images/weather/clouds_weather.jpg>";

But make sure you have a tab called "weather" inside your players table.

Re: Weather

Posted: Sat Mar 05, 2011 10:09 pm
by Huezzer
what is the "PhpMyAdmin" table information?, Like id?, or what do I have to add? and how do I link it, if I understand you right with the playerinfo update thing :P second code :)

Thanks, Huezzer.

Re: Weather

Posted: Sat Mar 05, 2011 10:14 pm
by Callan S.
If you just want it random each time the player comes to the page, you can just use a session variable. So you put session_start(); in at the start of your php, and then use something like

Code: Select all

if (!isset($_SESSION['currentweather'])) $_SESSION['currentweather']=rand(1,2);
Then, changing the rand values to the range you want and using if statements, check the session value on the page and have it echo out the appropriate image.

This way you don't have to mess around with the database as yet. But if you haven't watched the first five tutorial videos yet, you should as they teach you this stuff in a realy easy to absorb manner.

Re: Weather

Posted: Sun Mar 06, 2011 12:08 am
by Huezzer
Sorry, THe thing I want to do is to add the weather images in a square in the left corner that show the current weather in the game. And the thing is I want the code to make it randomly all the time,and it could be same weather twice like in the real world ^^, Also It will change every time you log in,
Two different players can have same weather or different weather, ^^^Hope you know what I mean.

Thanks, Huezzer

Re: Weather

Posted: Sun Mar 06, 2011 3:30 am
by Callan S.
Yep, I got you - that's why I'd just suggest using a $_SESSION variable as I noted. It's as easy as setting any normal variable (well, a bit more typing).