Jump to content

Number problem


Worqy

Recommended Posts

Hi.

I have a script that adds a value to a MySQL value every second.

Now I made another script that shows this value to the user.

The problem is that the value in MySQL can be 200,945646 , and I just want to show the 200 .

 

Anyone that knows how do I get this?

 

Btw: The number is in a array

Link to comment
Share on other sites

$x='200,945646';

$num=explode(",",$x);

echo $num[0];

 

HTH

Teamatomic

Thank you, works great.

Now I have a question that I need a answer on, because some people say its not possible and some people say I can do it.

I'm retrieving some data from MySQL. I have made a script that adds a value to MySQL every second (refresh with META). Now I retrieve the info to main.php, but I need to use META to refresh the page so I can see the new value, that the other script adds. It looks a bit weird when the page updates every second (main.php). Is there some other way?

Link to comment
Share on other sites

use jquery for this.

 

I believe you are lucky since i was working on this earlier on about two weeks ago.

 

I will give you my code to make it "refresh"

 

The proper term would be reload.

 

<script src="http://code.jquery.com/jquery-latest.js"></script>              
<script type="text/javascript">
// When page has completed loading. Then LOAD the part of the page you need to be loaded
$(document).ready(function() {
      // Once document has loaded the fadeOut content (If any) and then load the php code. Fade it in slowly.
      $("#divname").fadeOut('slow').load('loadthis.php').fadeIn('slow');
   var refreshId = setInterval(function() {
      // After the amount of seconds below this code will keep loading over and over.
      $("#divname").fadeOut('slow').load('loadthis.php').fadeIn('slow');
   }, 30000);
});

Link to comment
Share on other sites

<script src="http://code.jquery.com/jquery-latest.js"></script>              
<script type="text/javascript">
// When page has completed loading. Then LOAD the part of the page you need to be loaded
$(document).ready(function() {
      // Once document has loaded the fadeOut content (If any) and then load the php code. Fade it in slowly.
      $("#divname").fadeOut('slow').load('loadthis.php').fadeIn('slow');
   var refreshId = setInterval(function() {
      // After the amount of seconds below this code will keep loading over and over.
      $("#divname").fadeOut('slow').load('loadthis.php').fadeIn('slow');
   }, 30000);
});

$("#divname") = You replace "divname" with your div id. As said above, if you have a piece of code which displays information every 1 second, stick it in a div, and use the JQuery (Thats if you wanted to)

30000 = 30 seconds. 1000 = 1 second. So just adjust that to when you want to reload the div. (eg: each second)

Link to comment
Share on other sites

you can change loadthis.php to something like:

 

includes.php #divcontainer

 

which will load that file and take the contents of #(div) you have selected :)

 

Thank you for your reply.

I didn't get it to work, maby I did something wrong?

auto-update.php (Should I set this script in main.php?)

<script src="http://code.jquery.com/jquery-latest.js"></script>             
<script type="text/javascript">
// When page has completed loading. Then LOAD the part of the page you need to be loaded
$(document).ready(function() {
      // Once document has loaded the fadeOut content (If any) and then load the php code. Fade it in slowly.
      $("#ResTable").fadeOut('slow').load('main.php').fadeIn('slow');
   var refreshId = setInterval(function() {
      // After the amount of seconds below this code will keep loading over and over.
      $("#Restable").fadeOut('slow').load('main.php').fadeIn('slow');
   }, 1000);
});

 

main.php (Only a part of the code)

?>
<html>
<div id="ResTable">
</html>
<?php
//Gold
$numGold= $info['gold'];
$gold=explode(".",$numGold);
echo $gold[0];
echo " ";
//Three
$numThree= $info['three'];
$three=explode(".",$numThree);
echo $three[0];
echo " ";
//Clay
$numClay= $info['clay'];
$clay=explode(".",$numClay);
echo $clay[0];
echo " ";
//Iron
$numIron= $info['iron'];
$iron=explode(".",$numIron);
echo $iron[0];
echo " ";
//Wheat
$numWheat= $info['wheat'];
$wheat=explode(".",$numWheat);
echo $wheat[0];
echo " ";
/**
echo ' Three: ';
echo $info['three'];
echo ' Clay: ';
echo $info['clay'];
echo ' Iron: ';
echo $info['iron'];
echo ' Wheat: ';
echo $info['wheat'];
**/
?>
<html>
</div>
</html>

 

I started up the script tha adds the value to MySQL, and the new script.

But the only thing that happends is that I see the old values in main.php.

Something wrong?

Link to comment
Share on other sites

Yes, it should go between the <head></head> tags on main.php

You can also use <html> tags and have PHP between them too. Instead of using them twice or more.

 

Put the PHP code that needs to be refreshed into another file. eg: myfile.php

Then in your jquery script, replace "main.php" with myfile.php

 

Also, in the jquery script, after the line "});" (lastl ine) You'll need to put in "</script>" It wasn't in the origional by mistake.

Link to comment
Share on other sites

Ok, I'll do that, but why this:

"Put the PHP code that needs to be refreshed into another file. eg: myfile.php"

 

??

 

Because it is this part that needs to be updated:

main.php

//Gold
$numGold= $info['gold'];
$gold=explode(".",$numGold);
echo $gold[0];
echo " ";
//Three
$numThree= $info['three'];
$three=explode(".",$numThree);
echo $three[0];
echo " ";
//Clay
$numClay= $info['clay'];
$clay=explode(".",$numClay);
echo $clay[0];
echo " ";
//Iron
$numIron= $info['iron'];
$iron=explode(".",$numIron);
echo $iron[0];
echo " ";
//Wheat
$numWheat= $info['wheat'];
$wheat=explode(".",$numWheat);
echo $wheat[0];
echo " ";

Link to comment
Share on other sites

Now, I did as you said:

main.php

<html>
<head>
<title>Access denied!</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>             
<script type="text/javascript">
// When page has completed loading. Then LOAD the part of the page you need to be loaded
$(document).ready(function() {
      // Once document has loaded the fadeOut content (If any) and then load the php code. Fade it in slowly.
      $("#ResTable").fadeOut('slow').load('test.php').fadeIn('slow');
var refreshId = setInterval(function() {
      // After the amount of seconds below this code will keep loading over and over.
      $("#Restable").fadeOut('slow').load('test.php').fadeIn('slow');
}, 1000);
});
</script>
</head>

 

and

 

test.php

<html>
<div id="ResTable">
<?php
//Gold
$numGold= $info['gold'];
$gold=explode(".",$numGold);
echo $gold[0];
echo " ";
//Three
$numThree= $info['three'];
$three=explode(".",$numThree);
echo $three[0];
echo " ";
//Clay
$numClay= $info['clay'];
$clay=explode(".",$numClay);
echo $clay[0];
echo " ";
//Iron
$numIron= $info['iron'];
$iron=explode(".",$numIron);
echo $iron[0];
echo " ";
//Wheat
$numWheat= $info['wheat'];
$wheat=explode(".",$numWheat);
echo $wheat[0];
echo " ";
?>
</div>
</html>

But still it does not work

Link to comment
Share on other sites

Keep everything between the php tags in test.php

Move the

<div id="ResTable">

and the close div back into main.php.

So main.php should look like..

<html>
<head>
<title>Access denied!</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>             
<script type="text/javascript">
// When page has completed loading. Then LOAD the part of the page you need to be loaded
$(document).ready(function() {
      // Once document has loaded the fadeOut content (If any) and then load the php code. Fade it in slowly.
      $("#ResTable").fadeOut('slow').load('test.php').fadeIn('slow');
var refreshId = setInterval(function() {
      // After the amount of seconds below this code will keep loading over and over.
      $("#ResTable").fadeOut('slow').load('test.php').fadeIn('slow');
}, 1000);
});
</script>
</head>

<body>
<div id="ResTable">
</div>
</body>
</html>

 

And all test.php has is the everything between the php tags (WITH THE PHP TAGS) no other tags eg: html etc

Link to comment
Share on other sites

Thank you, now it works.

But when I putted in this code in test.php

<?php
//Include
include "connect.config.php";

// Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select Database");

// Collect data from MySQL, table resources
$data = mysql_query("SELECT * FROM resources") or die(mysql_error());
while($info = mysql_fetch_array($data))
//Gold
$numGold= $info['gold'];
$gold=explode(".",$numGold);
echo " Gold: ";
echo $gold[0];
//Three
$numThree= $info['three'];
$three=explode(".",$numThree);
echo " Three: ";
echo $three[0];
//Clay
$numClay= $info['clay'];
$clay=explode(".",$numClay);
echo " Clay: ";
echo $clay[0];
//Iron
$numIron= $info['iron'];
$iron=explode(".",$numIron);
echo " Iron: ";
echo $iron[0];
//Wheat
$numWheat= $info['wheat'];
$wheat=explode(".",$numWheat);
echo " Wheat: ";
echo $wheat[0];
?>

 

It only shows:

Gold: 1500 Three: Clay: Iron: Wheat:

 

If I runt the same code in main.php it get this answer:

Gold:1500 Three:5000 Clay:1750 Iron:3248 Wheat:2250

 

Strange?

Link to comment
Share on other sites

I don't know sorry.

Although if it works in main.php .. then thats alright.. isn't it?

 

hmm no? Because it is the content in test.php that shall be refreshed and shown on the page. ANd while looking today, I saw that your script isnt even refreshing, or it isnt showing the new value  :confused:

Link to comment
Share on other sites

I'm not sure about it not refreshing. However.. replace your while with this.

while($info = mysql_fetch_array($data)) {
//Gold
$numGold= $info['gold'];
$gold=explode(".",$numGold);
echo " Gold: ";
echo $gold[0];
//Three
$numThree= $info['three'];
$three=explode(".",$numThree);
echo " Three: ";
echo $three[0];
//Clay
$numClay= $info['clay'];
$clay=explode(".",$numClay);
echo " Clay: ";
echo $clay[0];
//Iron
$numIron= $info['iron'];
$iron=explode(".",$numIron);
echo " Iron: ";
echo $iron[0];
//Wheat
$numWheat= $info['wheat'];
$wheat=explode(".",$numWheat);
echo " Wheat: ";
echo $wheat[0];
}

 

You didn't have brackets in, and basically the while function only recognised that it only had to do "gold"

If that wasn't the problem, I still prefer to use brackets. :D

Link to comment
Share on other sites

Nooo. The while function. You didn't have {  }

I put them in the code I supplied. Just copy and paste it :P

I'll fix that echo array problem later, I just need your script to work:

main.php

<script src="http://code.jquery.com/jquery-latest.js"></script>             
<script type="text/javascript">
// When page has completed loading. Then LOAD the part of the page you need to be loaded
$(document).ready(function() {
      // Once document has loaded the fadeOut content (If any) and then load the php code. Fade it in slowly.
      $("#ResTable").fadeOut('slow').load('test.php').fadeIn('slow');
var refreshId = setInterval(function() {
      // After the amount of seconds below this code will keep loading over and over.
      $("#Restable").fadeOut('slow').load('test.php').fadeIn('slow');
}, 1000);
});
</script>

test.php:

<?php
//Include
include "overall.config.php";

// Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select Database");

// Collect data from MySQL, table resources
$data = mysql_query("SELECT * FROM resources") or die(mysql_error());
while($info = mysql_fetch_array($data))
{
//Gold
$numGold= $info['gold'];
$gold=explode(".",$numGold);
echo " Gold: ";
echo $gold[0];
//Three
$numThree= $info['three'];
$three=explode(".",$numThree);
echo " Three: ";
echo $three[0];
//Clay
$numClay= $info['clay'];
$clay=explode(".",$numClay);
echo " Clay: ";
echo $clay[0];
//Iron
$numIron= $info['iron'];
$iron=explode(".",$numIron);
echo " Iron: ";
echo $iron[0];
//Wheat
$numWheat= $info['wheat'];
$wheat=explode(".",$numWheat);
echo " Wheat: ";
echo $wheat[0];

}

?>

Your function is not refreshing the main.php page (or the #ResTable part to be exact)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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