bicho83 Posted February 23, 2008 Share Posted February 23, 2008 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 https://forums.phpfreaks.com/topic/92558-associative-arrays-getting-values-from-files-how/ Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Share Posted February 23, 2008 i believe you have to open the file for reading and parse it's contents first. you can do that using fread or file_get_contents Link to comment https://forums.phpfreaks.com/topic/92558-associative-arrays-getting-values-from-files-how/#findComment-474293 Share on other sites More sharing options...
bicho83 Posted February 23, 2008 Author Share Posted February 23, 2008 Oh, I didn't know I could use file_get_contents in there too. That worked, thanks. Now I need help fixing the array looping, because it's adding both array values in the same database field. Link to comment https://forums.phpfreaks.com/topic/92558-associative-arrays-getting-values-from-files-how/#findComment-474294 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Share Posted February 23, 2008 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 https://forums.phpfreaks.com/topic/92558-associative-arrays-getting-values-from-files-how/#findComment-474296 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Share Posted February 23, 2008 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 https://forums.phpfreaks.com/topic/92558-associative-arrays-getting-values-from-files-how/#findComment-474298 Share on other sites More sharing options...
bicho83 Posted February 23, 2008 Author Share Posted February 23, 2008 That worked man, thanks. Mind if I ask you why the '". in there? or the single quote, double quote, period. Link to comment https://forums.phpfreaks.com/topic/92558-associative-arrays-getting-values-from-files-how/#findComment-474301 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Share Posted February 23, 2008 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]')"; ?> Link to comment https://forums.phpfreaks.com/topic/92558-associative-arrays-getting-values-from-files-how/#findComment-474304 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.