Jump to content

[SOLVED] Arrays


dink87522

Recommended Posts

<?php // FUNCTIONS
function stat_01($stat_01_handle, $answer_result){
	global $stat_01_handle, $answer_result; 
	echo("<br /> stat_01_handle: $stat_01_handle");
	echo("<br /> answer_result: $answer_result <p>");
	$stat_01_file = "statistics01.txt"; 
	$stat_01 = fopen($stat_01_file, "r"); 
	$stat_01_data = fread($stat_01, filesize($stat_01_file));
	fclose($stat_01);
	$stat_01 = fopen($stat_01_file, "w"); 
	$stat_01_raw = explode("h", $stat_01_data);
	[b]print_r($stat_01_data);[/b]
	echo ("<p>Data: ".$stat_01_raw[$stat_01_handle]);
	$stat_01_temp = $stat_01_raw[$stat_01_handle];
	$stat_01_pieces = explode(",", $stat_01_temp);
	echo("<p> Exploded pieces: ".$stat_01_pieces[0]); // $stat_01_pieces[1] = question/array handle
	echo("<p> Exploded pieces: ".$stat_01_pieces[1]); // $stat_01_pieces[1] = number of times correct
	echo("<p> Exploded pieces: ".$stat_01_pieces[2]);  // $stat_01_pieces[2] = number of times incorrect
		if ($answer_result == 1){ // User answered the question correctly
			$stat_01_pieces[1] = ($stat_01_pieces[1] + 1); // Adds 1 to the correct tally statistic for that question
		}else{
			$stat_01_pieces[2] = ($stat_01_pieces[2] + 1); // Adds 1 to the incorrect tally statistic for that question
		}
	//$a = "h".$stat_01_pieces[0].",".$stat_01_pieces[1].",".$stat_01_pieces[2];
	//$stat_01_data[$stat_01_handle] = $a;
	[b]print_r($stat_01_data);[/b]
	echo("A: ".$a);
	echo("SH: ".$stat_01_raw[$stat_01_handle]);
	for 
	fwrite($stat_01, $stat_01_raw);
	fclose($stat_01);
}
?>

 

The data is in the first array when I print it. However by the end of the function it is no longer in that array (I need it be be, while I have changed on of the contents of one of the array values). When I try to print the array at the end the data is not in an array, why??? How can I achieve what I mentioned above (change the contents of one of the array values).  Thanks

Link to comment
Share on other sites

If you are getting the $variables by

global $stat_01_handle, $answer_result;

 

Why are sending them again to the function?  :

function stat_01($stat_01_handle, $answer_result)

 

 

remove the function parameters:

function stat_01()

 

But still use the globals:

function stat_01() {

global $stat_01_handle, $answer_result;

 

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.