Jump to content

Newb Questions...


spleinmuncher

Recommended Posts

Hi, I am new to this forum, though I have been struggling to learn PHP for some time now...I am currently in charge of a forum-based (though it is a very heavily modified forum...) nation simulation game, wherein players simulate real world politics by taking the role of a leader of a modern nation or organization.  I am attempting to add a new feature to the game: an automated Currency exchange system, which auto-updates exchange rates on its own (ambitious effort, I know...).  So, then, here is where I need your help: I am a horrible coder, and this is just not working!  I would like for you guys to tell me where I fubared (and trust me, its a lot of places).  So, here is how the auto-update works (for the record, I am using invision powerboard...):

I inserted an if clause into a commonly used file (in this case Usercp.php) that, if more than 28 hours have passed since the last update, the system is to run the file Ticker.php, which contains all of the code for the actual auto-update script.  Here is the portion of the file that calls it:

	$updates_array = array();
	$DB->query( "SELECT * FROM ibf_ticker" );
	while ( $i = $DB->fetch_row() )
	{
		$updates_array[] = $i['u_date'];
	}

	if ( time() - max($updates_array) >= 100800 )
	{
			include( ROOT_PATH."sources/Ticker.php" );
	}
	}

 

The problem is this: while the system DOES in fact open Ticker.php, it does not run any of the file's contents!  Which brings me to my next question: is this what I think it is?  I am pretty sure this does what I want it do, but if it does not, could you tell me?

<?php

//---------------------------------------------------
// Superpower
// Ticker Auto-Update
// Copyright 2007 Alex Zaslavsky
//---------------------------------------------------

class ticker {

function update_calc() 
{
	global $IN, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;

//------------------------------------------------------------------------------------------------------
// First, we select the currency in our database that is the primary currency
//------------------------------------------------------------------------------------------------------
	$primary_currency = array ();
	$DB->query( "SELECT * FROM ibf_currency WHERE c_main= 1" );
	while ( $u = $DB->fetch_row() )
	{
		$u[0] = $u['c_id'];
		$u[1] = $u['c_inflate'];
		$primary_currency[] = $u;
	}

//------------------------------------------------------------------------------------------------------
// Next, we select all the nations that are using that primary currency
//------------------------------------------------------------------------------------------------------

	$nation_array = array ();
	$DB->query( "SELECT * FROM ibf_nations WHERE n_primary_currencies=".$primary_currency[0] );
	while ( $s = $DB->fetch_row() )
	{
		$s[0] = $s['n_gdp'];
		$s[1] = $s['n_inflation'];
		$s[2] = $s['n_id'];
		$nation_array[] = $s;
	}

//------------------------------------------------------------------------------------------------------
// Now we select all the nations that are using that currency as a secondary currency
//------------------------------------------------------------------------------------------------------

	$countries_array = array ();
	$DB->query( "SELECT * FROM ibf_nations" );
	while ( $j = $DB->fetch_row() )
	{
		$option_currencies = explode( ",", $j['n_currencies']);
		if ( $option_currencies == $primary_currency[0])
		{
			$j[0] = $j['n_gdp'];
			$j[1] = $j['n_inflation'];
			$j[2] = $j['n_id'];
			$countries_array[] = $j;
		}
	}

//----------------------------------------------------------------------------------------------------------
// Then we take all our nations with the primary currency, and simply multiply their GDP by their Inflation
//----------------------------------------------------------------------------------------------------------

	$result = array ();
	foreach ( $nation_array as $r)
	{
		$result = $r[0] * $r[1];
	}

//------------------------------------------------------------------------------------------------------
// We do the same for all the nations with our currency as a secondary currency...
//------------------------------------------------------------------------------------------------------

	$answer = array ();
	foreach ( $nation_array as $p)
	{
		$answer = $p[0] * $p[1];
	}

//------------------------------------------------------------------------------------------------------
// Now, we make an array of all the nations in our database that were not in our first two groups
//------------------------------------------------------------------------------------------------------

	$other_nations_array = array ();
	$DB->query( "SELECT * FROM ibf_nations" );
	while ( $z = $DB->fetch_row() )
	{
		if ($z['n_id'] != $countries_array[2] && $z['n_id'] != $nation_array[2])
		{
			$z[0] = $z['n_gdp'];
			$z[1] = $z['n_inflation'];
			$z[2] = $z['n_id'];
			$other_nations_array[] = $j;
		}
	}

//------------------------------------------------------------------------------------------------------
// And we multiply their GDP by their inflation as well
//------------------------------------------------------------------------------------------------------

	$solution = array ();
	foreach ( $other_nation_array as $y)
	{
		$solution = $y[0] * $y[1];
	}
         }
}

?>

 

Basically, what I want the file to do is this:

-Make an array called $primary_currency, which will store all of the currencies which have 1 in the database slot "c_main"

-Make an array of all the nations which have the currency (there is only one primary currency) in the n_primary_currencies column in the database.  The ID of our currency is listed in that column if a nation has it

-Make an array of all nations who have a secondary currency (listed in the table) whose ID is the same as the one in our $primary_currency array.  Since a nation may have more than one secondary currency (but only one primary currency), the values in the database are separated by commas (e.g, currency_ID_1, currency_ID_2, etc...), so it is is necessary to explode that into an array...right?

-Next, we are going to take all the nations in $nations_array, and for each nation, multiply its GDP by its inflation.  Then, we will store all of our results in an array called $result (so basically, $result will be an array of the value of each nation's GDP multiplied by its inflation)

-We will do the exact same thing for our $countries_array, and name that array $answer

-Now, we will take all our nations that are NOT in either $nations_array or $countries_array, and make an array out of them as well, called $other_nations_array

-We will also multiply those nations' GDPs by their Inflations

-Thats it!

 

I know that is laden with mistakes...could you please help me though?

 

Thanks guys!

Link to comment
https://forums.phpfreaks.com/topic/59600-newb-questions/
Share on other sites

Well, I changed it to:

	$updates_array = array();
	$DB->query( "SELECT * FROM ibf_ticker" );
	while ( $i = $DB->fetch_row() )
	{
		$updates_array[] = $i['u_date'];
	}

	if ( time() - max($updates_array) >= 100800 )
	{
			include( ROOT_PATH."sources/Ticker.php" );
			$ticker = new ticker;

			$ticker->update_calc();
	}
	}

 

It still does not seem to be executing though.

Link to comment
https://forums.phpfreaks.com/topic/59600-newb-questions/#findComment-296175
Share on other sites

		$test_countries_array = array ("id"=> array(), "inflation" => array(), "gdp" => array() );
		$DB->query( "SELECT * FROM ibf_nations ORDER BY n_id" );
		while ( $d = $DB->fetch_row() )
		{	
			$test_option_currencies[] = explode( ",", $d['n_currencies']);
			foreach ($test_option_currencies as $test_key => $test_value)
			if ( $test_value == $test_currency['id'])
			{
				$test_countries_array['id'][] = $d['n_id'];
				$test_countries_array['inflation'][] = $d['n_inflation'];
				$test_countries_array['gdp'][] = $d['n_gdp'];
			}
		}

Now I have a slight problem: when the program runs upon a value where $test_value = $test_currency['id'], instead of only adding that single nation to $test_countries_array, it adds all of the nations with ID's greater than it as well!  Any idead?

Link to comment
https://forums.phpfreaks.com/topic/59600-newb-questions/#findComment-296748
Share on other sites

<?php
	foreach ($compare as $key => $value )
	{
		$test_currency = array ("id" => array(), "inflation" => array(), "rate" => array() );
		$DB->query( "SELECT * FROM ibf_currency WHERE c_id=".$value );
		while ( $w = $DB->fetch_row() )
		{
			$test_currency['id'][] = $w['c_id'];
			$test_currency['inflation'][] = $w['c_inflate'];
			$test_currency['rate'][] = $w['c_rate'];
		}

		foreach ($test_currency['id'] as $kk => $bb)
		{
			$ARR2 = $bb;
			$test_nation_array = array ("id"=> array(), "inflation" => array(), "gdp" => array() );
			$DB->query( "SELECT * FROM ibf_nations WHERE n_primary_currencies=".$ARR2 );
			while ( $g = $DB->fetch_row() )
			{
				$test_nation_array['id'][] = $g['n_id'];
				$test_nation_array['inflation'][] = $g['n_inflation'];
				$test_nation_array['gdp'][] = $g['n_gdp'];
			}
		}

		$test_countries_array = array ("id"=> array(), "inflation" => array(), "gdp" => array() );
		$DB->query( "SELECT * FROM ibf_nations WHERE n_currencies like '".$ARR2."' OR n_currencies like '%,".$ARR2."' OR n_currencies like '%,".$ARR2.",%' OR n_currencies like '".$ARR2.",%'" );
		while ( $j = $DB->fetch_row() )
		{	
			$test_countries_array['id'][] = $j['n_id'];
			$test_countries_array['inflation'][] = $j['n_inflation'];
			$test_countries_array['gdp'][] = $j['n_gdp'];
		}

		$test_result = array ();
		foreach ( $test_nation_array as $r)
		{
			$test_result[] = $r['gdp'] * $r['inflation'];
		}

		$test_answer = array ();
		foreach ( $test_countries_array as $p)
		{
			$test_answer[] = $p['gdp'] * $p['inflation'];
		}


		$test_total_gdp = implode( " + ", $test_nation_array['gdp'] );

		if (implode($test_nation_array['gdp']) == "")
		{
			$final_test = 0;
		}
		else
		{
			$final_test = (implode( " + ", $test_result )) / $test_total_gdp;
		}

		$test_secondary_gdp = implode( " + ", $test_countries_array['gdp'] );

		if (implode($test_countries_array['gdp']) == "")
		{
			$final_other_test = 0;
		}
		else
		{
			$final_other_test = (implode( " * ", $test_answer )) / $test_secondary_gdp;
		}

		if (implode($test_currency['inflation']) == "" || implode($test_currency['inflation']) == 0 )
		{
			$test_versus = 1;
		}
		else if (((.75 * $final_test) + (.25 * $final_other_test)) == 0)
		{
			$test_versus = 1;
		}
		else
		{
		$test_versus = ((.75 * $final_test) + (.25 * $final_other_test)) / implode($test_currency['inflation']);
		}

		$multiplier = (($test_versus / $versus) * .1) + .9;

		$new_rate = (implode($test_currency['rate'])) * $multiplier;

		foreach ($test_nation_array['id'] as $icue => $iresolve)
		{
		$prev_inflation = $test_nation_array['inflation'][$icue];
		$sub_inflation = ($prev_inflation * .9) + ($final_primary * .1);

		$DB->query( "UPDATE ibf_nations SET n_inflation=".$sub_inflation." WHERE n_id=".$iresolve );
		}

		$DB->query( "UPDATE ibf_currency SET c_rate='".$new_rate."', c_inflation='".$final_test."' WHERE c_id=".$ARR2 );

	}
?>

 

Guys, I really need help on this one, as it has me entirely stumped.  When my system runs this, it simply ignores the foreach that starts with this line:

foreach ($test_nation_array['id'] as $icue => $iresolve)
	$prev_inflation = $test_nation_array['inflation'][$icue];

 

But here is the weird thing: when I change the line which I just showed you to this, everything runs finse!:

 

foreach ($primary_array['id'] as $icue => $iresolve)
	$prev_inflation = $primary_array['inflation'][$icue];

 

The only difference between the two is that $primary_currency is an array which is defined outside of the original foreach (I did not copy $primary_currency's definition, and though I could, I have tried this with several other arrays defined outside my foreach, an it works very well).  Does anybody have a solution?  I really need help ASAP, as I have some people counting on me for this one...

 

Much thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/59600-newb-questions/#findComment-297244
Share on other sites

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.