Jump to content

Associative Arrays - Getting Values From Files - How?


bicho83

Recommended Posts

Is it possible to make associative arrays to grab the values of a .txt file instead of actually grabbing the String of the patch specified?

 

$players = array(
			array(
				"nickname" => "Somedude",
				"global_location" => "somedude_points.txt"
			),
			array(

				"nickname" => "Someguy",
				"global_location" => "someguy_points.txt"
			)
		);

 

I am trying to put that (the value of the file) in a database but it treats it as string (I guess the double quotes don't help either).

I am referencing the filename because that filename changes overtime, I can't just reference a certain value.

 

2nd Problem.

	foreach ($players as $c) {
	while (list(, $v) = each ($c)) {
		echo "$v <br />";

		$sql = "INSERT INTO players(nickname, global_points) VALUES('$v', '$v')";
		$res = mysqli_query($mysqli, $sql);

	}

}

 

When I check the database the tables are not populated as I want it. It adds both the nickname and the actual path to both table fields that I have created (nickname and global_points) in the table players. I want the nickname value to just go to the nickname field, and the values from the the files that changes to the global_points field.

 

When I echo $v, it shows the following:

Somedude

somedude_points.txt

Someguy

someguy_points.txt

 

Thanks, I hope someone can help me out!!  :'(

Link to comment
Share on other sites

i think changing this line....

 

$sql = "INSERT INTO players(nickname, global_points) VALUES('$v', '$v')";

 

to this will work...

 

$sql = "INSERT INTO players(nickname, global_points) VALUES('".$c["nickname"]."', '".file_get_contents($c["global_location"])."')";

Link to comment
Share on other sites

i dont think you need that 'while' statement either....

 

<?
$players = array(
			array(
				"nickname" => "Somedude",
				"global_location" => "somedude_points.txt"
			),
			array(

				"nickname" => "Someguy",
				"global_location" => "someguy_points.txt"
			)
		);

foreach ($players as $c) {
$sql = "INSERT INTO players(nickname, global_points) VALUES('".$c["nickname"]."', '".file_get_contents($c["global_location"])."')";
$res = mysqli_query($mysqli, $sql);
}
?>

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.