Help writing data to file.

Hello…

Here is what i have so far.

I create a user, and then i click on a button to create a dir (mkdir) and it makes a folder named what ever the get_var tells it.

It then takes me to a page where i can click a button and it copys a set of files into this new folder.

All these files it copies will never change, except 1.

There is a config file that tells the other files in the folder (index.php, help.php) what to do. It can only get that information if it querys the database and finds all of the required info for that ID number.

The id number is always going to be different in each config file, but the rest of the information will remain there.

example

$id = 55;

I need to write the result of this into a php file i have in the folder.

In the php file its formatted like so…

<?php $id = 5; ?>

My question being, how can i write that value to my config file.

I tried using this …

[code]<?php
include “admin/conn.inc.php”;

$query = mysql_query(“SELECT id FROM user_info WHERE city = '”.$_POST[“city”]."’");
$result = mysql_fetch_array($query);
$id = $result[“id”];

?>

<?php $File = "config.php"; $Handle = fopen($File, 'a'); $Data = " $city_id = $id; "; fwrite($Handle, $Data); print "Data Written"; fclose($Handle); ?>

[/code]

That didnt work because first off it didnt like the $$ and second when it saw "; it thought to end the Data string.

I changed the $ to the ascii for it but then it wouldnt bring in my ID number from the query.

So to sum it all up, i need to write the value to a php file in php format…

If you can be of any help, please let me know :)!

Assuming your MySQL part is correct, I think your problem is the difference between double quotes and single quotes when making a string.

Single quotes create a string out of the given characters ‘as-is’, no questions asked. Double quotes however, try to evaluate (or parse) any PHP-possible code that’s between them. So if you have the following code:

[php]$data = “$city_id = $id;”;[/php]

The resulting string in the $data variable will look like this:

 = 55;

(where 55 is the value of $id, and $city_id is uninitialized, as per your example code).

What you need to do is either switch to single quotes, or escape any characters PHP might recognize, using a backslash:

[php]$Data = “$city_id = $id;”;[/php]

In order to not confuze parsed and unparsed variables in such strings, try to keep the parsed variables outside the quotes:

[php]$Data = “$city_id = “.$id.”;”;[/php]

It increases readability and should fix your problem altogether :slight_smile:

Great information…

I still have one problem.

It adds this to the file perfectly $city_id = ;

but it doesnt add the id that was called in to that area via $id

Any ideas?

problmes solved!

The include wasnt pointing to the correct folder with the db connections

Ok, i have a little problem here.

After i finnished my project and started testing it, i ran into an issue.

In my config file i have a query that reds info from that database according to the city ID #.

Well in my Write file (that writes the data to the config file) its putting the ID number at the bottom so the query isnt catching it.

I some how need to write all this data to my config file.

One of the lines im having trouble writing to the config file is the query.

If you could just show me how that one would look. as well as a resut one i could do the rest.

[php]

<?php $query = mysql_query("SELECT * FROM user_info WHERE id = '".$city_id."'"); $result = mysql_fetch_array($query); $city = $result["city"]; $ckm_first = $result["first"]; $ckm_last = $result["last"]; $ckm_number = $result["phone"]; $ckm_email = $result["email"]; $state = $result["state"]; $ckm_weather = $result["weather"]; $class_id = $result["classifieds"]; $cloud = $result["cloud"]; $portal = $result["portal"]; $cityid1 = $result["id"]; $contact = "Click Here"; $contact2 = "email"; $cplink = "$city City Portal"; $class = "$city Classifieds"; $ckm_name = "$ckm_first $ckm_last"; [/php] I need to write this info to a config file as is.. Thanks in advance. my attempt [php] <?php $File = "$path$folder/id.php"; $Handle = fopen($File, 'a'); $Data = "$city_id = ".$id." $query = mysql_query("SELECT * FROM user_info WHERE id = '".$city_id."'"); "; fwrite($Handle, $Data); print "Data Written to Config File "; print "
"; print "
"; print "
"; print "
"; print "
"; print "Click here to View City"; fclose($Handle); ?>

[/php]

I know its way off, but im learning by my mistakes and by the tips i get from you guys

I have been doing some searching and i think its possible to write to the top of a file, i will look into this more after work,

If you want to write stuff to the beginning of the file, rather than the end, there’s no such filepointer in PHP. A rather simple alternative would be:

  1. read the contents of the file into a variable
  2. truncate the contents of the file
  3. write the stuff that’s supposed to be at the top to the file
  4. write the contents previously read from the file, to the file

It’s a workaround, but should work cleanly.

All i did instead of using append (a) i used r+ and it wrote to the top of the file.

Doesn’t ‘r+’ overwrite the original contents?

Sponsor our Newsletter | Privacy Policy | Terms of Service