Jump to content


Write to file advanced question


6 replies to this topic

#1 Penaf

    Young Padawan

  • Members
  • Pip
  • 85 posts
  • Location:Portugal

Posted 29 August 2006 - 02:45 PM

Hi all

After having followed an huge ammount of tutorials here on p2l i managed to do a neat system for my father's company but now i came across a problem regarding writing to files!
I would be extremely grateful if anyone could help me getting this issue of mine solved!

So basically is this:

I have 2 files for a store in a certain city, Oporto-comunicate.php and Oporto-see.php, and I have an admin panel where i can add another city (reads the name from a form) and this would create another 2 files this time with the new city name

Example given: the new city is Lisbon then it would create Lisbon-comunicate.php and Lisbon-see.php

The problem is that I want to write PHP in the new files...
So let's imagine I have a template like

<?php
<--- CODE --->
$nameOfTheNewCity
echo "$example";
<--- CODE --->
?>

$nameOfTheNewCity should be seen on the new file as Lisbon and
echo "#example"; should be as is on the new file

Can anyone help me ? Or was this a bit confusing ?

Thank you in advance :)

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 29 August 2006 - 04:08 PM

Why use files? A database would be more secure and easier to deal with.
Just browse the MySQL section of the PHP tutorials here and you'll find all you need with mysql_query() and the $_GET superglobal. ;)

#3 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 29 August 2006 - 04:32 PM

What if he just wants them in files? Nothing wrong with that.

Just check out these PHP functions, they will be able to help you.

http://php.net/fopen
http://php.net/fread
http://php.net/fwrite
http://php.net/fclose

#4 Penaf

    Young Padawan

  • Members
  • Pip
  • 85 posts
  • Location:Portugal

Posted 29 August 2006 - 06:50 PM

I need to be files cause i'm creating a new page that will be used by the new store, and the login system makes it so that other existing stores can only access their respective page.

Thanks for the try Chaos King but I have already previously checked php.net manuals for help and haven't found any :|

#5 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 29 August 2006 - 07:07 PM

LOL.

I don't know what you are smoking but you are going to need to use those functions to solve your problem ;) I don't know where you were checking. :P

#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 30 August 2006 - 03:09 PM

Well, he could've found those functions, but when he tried to write the text to the file, he got some error.

What i'd do, is i would create a plain .txt file with what will be in the new files. Then, load the file, parse a few things in it, then write it to the new files.
$template = file_get_contents('template.txt');
$template = str_replace('{CITY_NAME}', $city_name, $template); // {CITY_NAME} won't change, it's a typical place holder that you will place in the txt file where the city name will go.  Then $city_name will contain the name of the city.
$fp = fopen('new_file.php', 'w');
fwrite($fp, $template);
fclose($fp);
That should give you a rough idea. I didn't test it, so i'm sure you'll have to fix something.

gl

#7 Penaf

    Young Padawan

  • Members
  • Pip
  • 85 posts
  • Location:Portugal

Posted 30 August 2006 - 10:43 PM

thanks rc69 ... that was exactly the kind of solution i was looking for :)

I'll see if that works or not but thanks already :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users