Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-16-2012, 02:11 PM help me in vars view
Junior Talker

Posts: 2
Trades: 0
hi frenids
i have 2 var in database names
oil ,money
i want Increased them every second by these calculation:-
$oil=$oil+1;
and display them by id in html page that the visitor can view the vars Increase in the page like as timer
please how can i do these php code ??
mustafa93 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-16-2012, 08:34 PM Re: help me in vars view
Physicsguy's Avatar
404 - Title not found

Posts: 1,061
Name: Scott Kaye
Location: Ontario
Trades: 0
...Ok then!

You want an auto-incrementing variable to show up on the page? Except the database values are updating every second? Seems like an awful system to me; incredibly heavyweight on the server.

To make it easier on your poor server, grab the value from the database with PHP, and use that in a JS loop that increments 1/sec. No need to make an AJAX call every single second while the DB updates, it's redundant.

I assume you know how to get data from a database? If not, this is all you need to know. To do what I said in Javascript, load the variable with PHP into a JS variable. From then on, use setInterval() to update the value in a box every second (1000 ms).

I hope I could help, if you need anything else, please ask

Side note: You can just use $oil++; instead of $oil=$oil+1;

EDIT: Heck with it, I'll write something

Not tested, but SHOULD work. <<-- emphasis on the 'Not tested'!

Code:
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT oil,money FROM mytable");
$result=mysql_fetch_array($result);
list($oil,$money)=$result;
?>
<script type="text/javascript">
var oil = <?php echo $oil;?>;
var money = <?php echo $money;?>;
function addone(){
	document.getElementById("money").innerHTML = parseInt(document.getElementById("money"))++;
	document.getElementById("oil").innerHTML = parseInt(document.getElementById("oil"))++;
}
setInterval("addone()",1000);
</script>
<div id="money"><?php echo $money;?></div>
<div id="oil"><?php echo $oil;?></div>
Just swap out the PHP values for your MySQL information / table name / database name and you should be good. I'm not 100% on that parseInt part, as I've never used parseInt. (Is it math.parseInt?)
__________________

Please login or register to view this content. Registration is FREE

Last edited by Physicsguy; 01-16-2012 at 08:42 PM..
Physicsguy is offline
Reply With Quote
View Public Profile Visit Physicsguy's homepage!
 
Old 01-17-2012, 05:10 AM Re: help me in vars view
chrishirst's Avatar
Defies a Status

Posts: 43,970
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Just to note that it couldn't be done with PHP anyway, as PHP runs ONCE, when the page is requested.
When the page is served to the user agent PHP execution is over and done with.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-18-2012, 04:17 PM Re: help me in vars view
Junior Talker

Posts: 2
Trades: 0
thank you so much Mr.Physicsguy
you only the one give me the answer after one month searching about the best way to make it !
i test your code in fact not work but you helped me to edit the code and finally work !!
these it the new code
PHP Code:
<?php
//these vars for example but in real code i will take it from mysql database
$oil=1000;
$money=2000;
?>
<script type="text/javascript">
var oil = <?php echo $oil;?>;
var money = <?php echo $money;?>;
function addone(){
    document.getElementById("money").innerHTML = money++;
    document.getElementById("oil").innerHTML = oil++;
}
setInterval("addone()",1000);
</script>
<div id="money"><?php echo $money;?></div>
<div id="oil"><?php echo $oil;?></div>
these vars for example but in real code i will take it from mysql database
i know how to get data from a database

i am sorry but i will ask you Several questions about php because i am currently I try to develop game for internet browser like travian.com
if you do not mind please answer me


Mr.Physicsguy my second question that how to save values in db after updating every second?
So as not to lose the visitor his data after the update in the case of exit from the site without logging off or close
what the best way to make it?
mustafa93 is offline
Reply With Quote
View Public Profile
 
Old 01-18-2012, 05:22 PM Re: help me in vars view
Junior Talker

Posts: 1
Trades: 0
Instead of doing it by PHP code, add a "last_update" field ond the db, so it's like this:
Code:
primary_id, oil, money, last_update
1,0,100,1334355
last_update contains the unix time of the last update, to update all use this query:
PHP Code:
<?php
$units_per_second 
1
mysql_query
("UPDATE `table` SET `last_update` = ".time().", `oil` = (".time()." -`last_update`) * ".$units_per_second);
Matt
IlMattoDel93 is offline
Reply With Quote
View Public Profile
 
Old 01-19-2012, 02:00 PM Re: help me in vars view
Ariblue's Avatar
Skilled Talker

Posts: 75
Name: Arifeen
Location: Pakistan, Quetta.
Trades: 0
I am new to this field but from what I have learned so far it can be done through JavaScript and not PHP.

I hope my post was helpful.

Regards
Arifeen
Ariblue is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to help me in vars view
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26370 seconds with 11 queries