Jump to content

fwrite- Writing variables into an external file


ryld

Recommended Posts

Note: New to PHP, scripting, programming, web design, and everything else.

Okay.  So this sounds pretty simple.  I know you could just use fopen to open a file and then fwrite to write a variable to a text file very easily.  But this isn't what I want to do.  I want to store the actual variable itself, not just the string that it contains.

To explain further, here is exactly what I'm trying to do:

I have a website that uses a database to store all of its information across all pages.  I have a PHP script that reads the information from the database and stores the information in variables.  Then the information contained in the variable is displayed.

The problem I ran into using this method is that there are 1 of 2 ways to do this.  I could have multiple instances of the script and tailor them for each page (clunky), or I could have one file that reads all of the information and stores all of the information, even if its not relevant to the page (which means there's a lot of unused data which is a waste of resources).

So I had the idea to use fopen(), fwrite(), and fclose() to open the file, write all the variables to the file, and close the file.  Then I would manually run the database script once every 24 hours, and it would output all of the variables to a .php file using the aforementioned functions.  Then each page would use the include() function to include the .php file that holds the variables and then pick and choose which variables to use.

To me, this would greatly reduce the amount of data transferred and the strain on the server.

However, I ran into the problem of not being able to write the actual variable, but only the string that the variable contains.

So my question is how would I go about writing the actual variable to a .php file?  Or is this even possible?

Thanks!
Link to comment
Share on other sites

When I initially gather the values for the variables, they come from a MySQL database.  PHP script runs to connect to the DB etc...

What I want to happen is that when the PHP script runs and gathers the values, I want it to write the VARIABLES (not just the values) to a different file (ex var.php), and then use include('var.php'); so that I can use some or all of the stored variables.

With this method, I would only have to run the script that connects to the database and gathers the values once per day.
Link to comment
Share on other sites

Sorry, still a bit fuzzy on this. Ok, so you're running a MySQL query in order to populate the variables...correct? And what you want to do is write the actual variable name and value to a text file? Like:

$thisismyvariable = "value";

So you want that to be written to the text file, correct?
Link to comment
Share on other sites

Ok, I think I get it now. Not sure why you want to save it to a text file but hey... :)

The text file can store anything. It's just text. So, you'd have to write your code in a way that the variable name is part of the variable value you're writing to the file. Sorta like this (not sure if this is syntax perfect but let's pretend it's logical)

[code]<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$yourVariable = "\$yourVariable = Bobby Bopper\n";
fwrite($fh, $yourVariable);
$yourVariable2 = "\$yourVariable2 = Tracy Tanner\n";
fwrite($fh, $yourVariable2);
fclose($fh);
?>[/code]

You have to escape the dollar sign so it doesn't act like a variable signifier.
Link to comment
Share on other sites

Okay.  So doing this would enable me use the include() function to include the text file into a php document and then use the variables stored in the text file, correct?

And as for the reason, it is because I only want the content of the pages updated once per day, and I only want to use one script.  Since all of the information for all of the pages is in the database, it makes sense (to me).  If you have a suggestion for another way to implement the idea, I'm all ears.
Link to comment
Share on other sites

Hm...doesn't seem to work.  :(

I get the following error:

[tt]Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\database testing\scripts\maxitems.php on line 35[/tt]

Line 35:
[code]$date = "\$date = $row['date']"; [/code]
Link to comment
Share on other sites

[code]
$date = "\$date = {$row['date']}";
[/code]

That should work.

I'm sure your script is working, but I would suggest populating an array with the DB values that way you could loop through a foreach and automate the process of creating each variable to write... That way if you ever wanted to modify the code you could just add another array key and value instead of hand editing code and copy and pasting... Either way works though :p
Link to comment
Share on other sites

Well, like I said, I'm brand spankin' new to PHP.  So most of my learning has been done by grabbing some scripts, using them, and looking at the result and the script to see what does what and learning that way...I find this to be the most effective way to learn it...mostly because that's how it used to be done!  So since I'm new to programming/scripting, I don't know how to do what you said to do.  :-P

Basically, to be honest, the script I'm using is not mine, though it has been heavily modified to suit my needs and I've learned a good bit from it, such as passing queries, connecting to DBs, variables, a LITTLE about arrays, and if/else statements.  I've learned most of this within the span of about 2 or 3 days.  So...not really sure exactly how to implement your suggestion.  :(
Link to comment
Share on other sites

Ah!  Sorry.  I forgot to post the updated problem.  The error message is the same, except applicable now to line 37.

Line 37:
[code]$news = "\$news = {htmlentities ($row['newstext'])}";[/code]

I also tried moving the curly bracket to before the normal bracket with no success.  I also tried the second syntax you suggested to no avail.
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.