Jump to content

Recommended Posts

Hello All,

 

I realise this query has a very simple solution, but i just do not know what it is.

 

I want to take File A an information file

 

Name

Reference

Colour

Shape

 

or whatever

 

and then add the answers to File 2 and file 3 and file 4, without ever needing to edit 2,3 or 4

 

Hello Welcome to (Name)

also known as (Reference) available in (Colour) and (Shape)

 

<?php include("file1.php"); ?>

is not good enough for the job because I want to add sections or snippets to File2.php, not the entire file.

 

I think I may need to use an array but I do not know how to call upon the sections of the Array or really how to setup the array in the first place.

 

There is no database involved, too advanced for me.

 

I just want / need to be able to amend file1.php if needed so that it changes File2.php and wherever featured.

 

I know the solution will be very simple but the more I read about php the less obvious the solution seems to be.

 

Thanks in Advance.

Link to comment
https://forums.phpfreaks.com/topic/55083-add-phrases-from-1-document-to-another/
Share on other sites

I've read your post about three times now and I *think* I know what you are wanting. Could have been much clearer. Let me paraphrase what I think you are really asking:

 

There is a text file with data fields in a specific format. There are also several template files with placeholders for the datafields in file1. I want to display the template files with the data.

 

Here is an example.

 

PHP File

<?php

$dataFile = "data.txt"; //Path to datafile
$templateFile = "template.txt"; //Path to template file

//Put dataFile into an array (by linebreaks)
$data = file($dataFile);

//Put template into a string variable
$template = file_get_contents($templateFile);

//Create array of the variable names to be replaced
//These must be in the same order as they are in the datafile
$dataVals = array ('{name}', '{address}', '{city}', '{state}', '{zip}');

//Replace var in template with values from data
$output = str_replace ($dataVals, $data, $template);

echo $output;

?>

 

Template file

<table>
  <tr>
    <td>Name:</td>
    <td>{name}</td>
  </tr>
  <tr>
    <td valign="top">Address:</td>
    <td>
      {address}<br>
      {city}, {state} {zip}
    </td>
  </tr>
</table>

 

Data File

John Smith
123 Main Street
Wichita
KS
67200

 

Output:

Name:    John Smith
Address: 123 main Street
         Wichita, KS 67200

I've read your post about three times now and I *think* I know what you are wanting. Could have been much clearer. Let me paraphrase what I think you are really asking:

 

There is a text file with data fields in a specific format. There are also several template files with placeholders for the datafields in file1. I want to display the template files with the data.

Hello mjdamato,

 

Thanks for your reply.

You were right with your paraphrase, thanks.

I have looked at lots of different php tutorials but the more I study the less I seem to understand and the correct terminology escapes me.

 

Thanks for your support.

I will try to implement your suggested solution and will report back as soon as completed.

 

BTW are there any online tutorials that you recommend? because I am keen to continue progressing.

Well, I hope it works for you.

 

As for your explanation, it is perfectly fine if you don't know the "correct" terminology. The important thing is to be clear and specific. If you notice, I did not use any technical terms within my paraphrase of your query. Just use plain english to state something if you don't know the technical terms.

 

Regarding tutorials, there are plenty out there and I don't have any recommendations per say. I went through a login tutorial utilizing PHP and MySQL when I first started in PHP. But, programming is programming. The language you choose only dictates the commands you will use. Picking up better programming techniques is something that you learn over time. I will sometimes pick up someone else's code and see how I can improve upon it or attempt to do something I have never done before to try and streatch myself.

Hi,

 

I tried to implement the script but I can't get it to work.

 

What do I do with the php file?? do I add it to the header or save it as a seperate document? what do I save it as?

 

 

 

Unrelated question.

Is it possible to have 1  file, index.php for example;

That has elements that are changed by the referring Url.

 

example 1

Welcome to MOVIE TITLE 1 player,

Click here to play MOVIE 1

 

example 2

Welcome to MOVIE TITLE 2 player,

Click here to play MOVIE 2

 

The word MOVIE 1 or MOVIE 2  added to the end of the referring Url as well as the specific code for MOVIE 1 or MOVIE 2 loaded into the PLAYER.

2 changeable pieces of information that is dependent on the Url.

 

The Url having the information added (most likely to the end) and then the index.php embeds the info when the page is viewed.

 

I want to do this because I am compiling a list of viewable media streams, at the moment I need to create a new page for every piece of media.

 

If it is possible to add the info to the URL I will only need 2 files, the URL List and the player which will make it much easier to to edit in future as well as add new titles.

 

Thanks,

 

 

To answer your unrelated question, yes it is completely possible. You do it by passing variables in the url string. The best way to explain is through example i think.

 

Say you have a link: http://www.yoursite.com/index.php?id=1

 

Then, in index.php:

 

<?php
$id = $_GET['id'];
?>

 

Say you need to pass two variables, you simply add an & sign in between : http://www.yoursite.com/index.php?id=1&foo=bar

 

 

As for your php file, yes - you save it and view the page. And it doesn't matter what you save it as. You will, of course, need to save all of the text files to get it to work too. You'll need to keep the names of those text files the same as the ones used in the php script, or modify the script.

Hi,

 

I tried to implement the script but I can't get it to work.

 

What do I do with the php file?? do I add it to the header or save it as a seperate document? what do I save it as?

 

First things first. I know that script works because I did test it before I posted it. Are you getting any errors? Well, as GingerRobot stated you save the first set of code as a PHP file (any name you want). The other two files are saved as TXT files with the names used in the script. You then just navigate to the PHP file to the address through your web server.

 

Now, the script can be easily modified to handle values passed on the query string. Ok, lets say you will pass two variables on the query string: movie and player. First off you should avoid using spaces or any special characters in the values you will use, and just use the name of the file without the extension. I would also put all movie files in a folder called "movies" and the player files (templates) in a folder called "players".

 

So, you would prepare my links as follows:

 

http://www.domain.com?movie=the_boat&player=wmp

 

The modified PHP script (with some error handling) could work something like this:

 

<?php

$movie = $_GET['movie']; //Path to movie data file
$player = $_GET['player']; //Path to player template file

//Put movie data file into an array (by linebreaks)
if(!($movie_data = file("movies/$movie.txt")) {

  $output = "Unable to retrieve movie data file.";

//Put player template into a string variable
} elseif (!($player_template = file_get_contents("players/$player.txt")) {

  $output = "Unable to retrieve player template file.";

} else {

  //Create array of the variable names to be replaced
  //These must be in the same order as they are in the movie data file
  $field_list = array ('{name}', '{address}', '{city}', '{state}', '{zip}');

  //Replace vars in player template file with values from movie data file
  $output = str_replace ($field_list, $movie_data, $player_template);

}

echo $output;

?>

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.