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!!  :'(

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"])."')";

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);
}
?>

i guess, i left double quotes in there, so i escaped the string to concatenate the variables, it can also be done like this...

 

<?php 
$sql = "INSERT INTO players(nickname, global_points) VALUES('$c[nickname]', '$c[global_location]')";
?>

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.