Uncle Zed 272 Posted March 19, 2013 Does anyone have something that will display my server's average life expectancy on my website? Share this post Link to post Share on other sites
dayz247 59 Posted March 19, 2013 (edited) If you have access to the database for your server, this would easily be done with a php script.pull your "profile" table down into an array (select survival_attempts,total_survivor_time from profile) .... loop through each row and add up all the survival attempts and survival time ..... then divide the two.This should work (untested):<?php// edit these$dbIp = Ip.to.your.database;$dbPort = port_for_your_sql;$dbUname = YourSqlUserName;$dbPassword = duh;$dbName = Your_Database_Name;// stop editing here, unless you know what your doing$con=mysqli_connect($dbIp . ":" . $dbPort, $dbUname, $dbPassword, $dbName);if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error();}$q1 = mysqli_query($con,"Select survival_attempts,total_survival_time from profile");$attempts = 0;$time = 0;while ($r1 = mysqli_fetch_array($q1)) { $attempts += $r1[0]; $time += $r1[1];}// average time in minutes$avgTimeMin = $time / $attempts;// edit here for output formattingecho "Average survival time in minutes: " . $avgTimeMin;mysql_close($con);?> Edited March 19, 2013 by dayz247 1 Share this post Link to post Share on other sites
Uncle Zed 272 Posted March 19, 2013 Thanks for the reply. My forum has a portal page on which I can place blocks of custom html: http://killinzedz.netHow could I make it so this information is displayed in an html block on my portal? Do I save the php file someplace and call it with html? Any insight is appreciated! Share this post Link to post Share on other sites
dayz247 59 Posted March 19, 2013 It really depends on how your host handles php within html...... you should be able to simply copy that block into your html block and go with it.Try copying the following into a html block:<?php// if you can read me on your website you have problems, ignore the next statement.echo "if this is all you can see, your set..... remove this code and copy that block, replacing the top portion with your database information";?> Share this post Link to post Share on other sites
Uncle Zed 272 Posted March 19, 2013 It really depends on how your host handles php within html...... you should be able to simply copy that block into your html block and go with it.Try copying the following into a html block:<?php// if you can read me on your website you have problems, ignore the next statement.echo "if this is all you can see, your set..... remove this code and copy that block, replacing the top portion with your database information";?>Putting that in a block just causes the php code to be displayed. I was thinking that there should be a way to save the code to a php file on my webserver, then call it with html in a block on my portal page? Share this post Link to post Share on other sites
dayz247 59 Posted March 19, 2013 (edited) Do you have access to a file named".htaccess"on your server?You need to enable php parsing of html files.You can store the code as a php file, and use the "include" command to put the file into html, but that in itself requires php parsin of the html file in order to work.... that command would be:<?phpinclude("yourfile.php");?>but if the snippet I gave you to test shows on the page, then the above code will as well. Edited March 19, 2013 by dayz247 Share this post Link to post Share on other sites
Uncle Zed 272 Posted March 19, 2013 Do you have access to a file named".htaccess"on your server?You need to enable php parsing of html files.You can store the code as a php file, and use the "include" command to put the file into html, but that in itself requires php parsin of the html file in order to work.... that command would be:<?phpinclude("yourfile.php");?>but if the snippet I gave you to test shows on the page, then the above code will as well.Actually I made an error in setting up the block on my portal. I had BBCode selected instead of html. Now it just shows nothing instead of the php code. Share this post Link to post Share on other sites
dayz247 59 Posted March 19, 2013 (edited) Ok, this is going to get ugly, lolSave the following as AvgLife.php :<?php// edit these$dbIp = Ip.to.your.database;$dbPort = port_for_your_sql;$dbUname = YourSqlUserName;$dbPassword = duh;$dbName = Your_Database_Name;// stop editing here, unless you know what your doing$con=mysqli_connect($dbIp . ":" . $dbPort, $dbUname, $dbPassword, $dbName);if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error();}$q1 = mysqli_query($con,"Select survival_attempts,total_survival_time from profile");$attempts = 0;$time = 0;while ($r1 = mysqli_fetch_array($q1)) { $attempts += $r1[0]; $time += $r1[1];}// average time in minutes$avgTimeMin = $time / $attempts;// edit here for output formattingecho "<div id=AvgLife>\n";echo "document.getElementById('AvgLife').innerHTML = 'Average survival time in minutes: " . $avgTimeMin . "'; \n";echo "</div>";mysql_close($con);?>put this on your webpage:<script type="text/javascript" src="http://www.yourpage.com/AvgLife.php"></script>That should get you something, lol Edited March 19, 2013 by dayz247 Share this post Link to post Share on other sites
Uncle Zed 272 Posted March 19, 2013 (edited) Still not showing up in the block, but trying it with just the path to the .php file in the browser shows errors: http://killinzedz.net/AvgLife.phpWith the original php code (Untested, your first reply), it works fine in the browser: http://killinzedz.net/ale.php but still no love in the portal block. Edited March 19, 2013 by BetterDeadThanZed Share this post Link to post Share on other sites
dayz247 59 Posted March 19, 2013 (edited) When calling the php file via javascript you must include some javascript in the php in order for it to work (this is why I had the div and the getelementbyid in the second code).For the error you are receiving, make sure you have your information entered correctly at the top. It is saying you have an error on line 3... did you do something wonky when putting your IP address in there?Try putting quotes around the variables... like so:$ip = "127.0.0.1";$port = "2302";$user = "blah";etc etcalso edit this line:echo "document.getElementById('AvgLife').innerHTML = 'Average survival time in minutes: " . $avgTimeMin . "'; \n";to this:echo "document.getElementById('AvgLife').innerHTML = 'Average survival time in minutes: " . $avgTimeMin . "' \n";Shouldn't make a difference (the extra ; should have just shown up on your page, which would have been aggrevating, and you would have wanted to remove it lol). Edited March 19, 2013 by dayz247 Share this post Link to post Share on other sites
Uncle Zed 272 Posted March 19, 2013 I have a feeling this is more of a restriction in the forum software I'm using rather than any problem with the code. I've got a message posted on the forum for the portal so maybe they can explain. Share this post Link to post Share on other sites
dayz247 59 Posted March 23, 2013 You ever get it working for your site? Share this post Link to post Share on other sites
Uncle Zed 272 Posted March 23, 2013 You ever get it working for your site?Nope. Share this post Link to post Share on other sites