Jump to content

Placeholders and automated pages


mattjones

Recommended Posts

Hello everyone i am new here and quite new to PHP. I have a couple of questions and would be grateful if anyone could answer them.

 

I am creating a niche directory site and i have made php create a new page from some form data using the following.

 

file_get_contents

str_replace

fwrite

 

and a few things.  I have used place holders to create these pages and also to create a link to these new pages.

 

On part where a new link is created i make the script write a new placeholder ready for the next new entry.

 

Hope you are still with me!!  :-\ anyway my questions are..

 

1. Is using placeholders the best way to go about this or is there another way?

 

2. What is the best way to hide these placeholders on a page that people will be viewing as they are unsightly.  I was thinking of making the text the same colour as the background but they will still be visible if highlighted and i have also heard i would be penalised by google for hiding text and i want my page to rank well and not annoy the search engines.

 

Thank you very much for any help in advance!!

 

Matt

Link to comment
Share on other sites

One of php's main uses is for creating dynamic content. The idea of actually writting to a file (excepting maybe as a stoarge solution) kinda goes against this entire concept.

 

Do you think when you post a new thread on this forum php creates a new file especially to hold your thread? Then with each reply a placeholder is replaced with a users posted data?

 

This is not the case.

Link to comment
Share on other sites

There is no simple functions that are going to magically do this for you. Its an entire concept and the main one which php centres around.

 

For instance (and this is a very simplified example), say you wanted every user of your site to get there own page which simply displays there name and email address. Using your method, you would make each individual file (you may as well be using html). Even if your using php to generate these files its still not exactly dynamic. Using php to generate the impression of each user having there own file is however.

 

The script might look something like:

 

<?php

  if (isset($_GET['user'])) {
    include 'db.connect.php';
    $user = mysql_real_escape_string($_GET['user']);
    $sql = "SELECT email FROM users WHERE uname = '$user' LIMIT 1";
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
        echo "<p>$user - {$row['email']}</p>";
      }
    }
  }

?>

 

Thats it! No more saving and manipulating files all over the server. Given that you store your usernames and email addresses within a database, this is the only code (obviously its a simplified example) you would need to create the impression of each user having there own page.

Link to comment
Share on other sites

ok that is very useful thank you very much.

 

I will use that for my users pages.

 

I still have a HTML page that is already created where i wish the links to be added in certain places.  I need this page to stay as HTML so it can be indexed by the search engines as this is the way i will get new members.

Is using placeholders the best way to do this.

 

Thanks again you are being very helpful.

 

Matt

Link to comment
Share on other sites

I need this page to stay as HTML so it can be indexed by the search engines

 

PHP pages are indexed by search engines. Besides that, its a simple process to make a php page appear to be served with the html extension (using mod_rewrite).

 

I still think your missing a big part of what php is and can do.

Link to comment
Share on other sites

Lets make a simple example. Take this html file with your placeholder in it.

 

links.html

<html>
  <head>
    <title>links</title>
  </head>
  <body>
    <p>
      <!-- {LINKS_PLACEHOLDER} -->
    </p>
  </body>
</html>

 

Now, at the moment you might have a script something like....

 

generate_links_html.php

<?php

  $links = array(
    '<a href="foo.com">foo.com</a>',
    '<a href="bar.com">bar.com</a>',
    '<a href="bob.com">bob.com</a>'
  );

  $file = file_get_contents('links.html');
  $file = str_replace('<!-- {LINKS_PLACEHOLDER} -->', implode("<br />\n", $links) . "<!-- {LINKS_PLACEHOLDER} -->\n", $file);
  file_put_contents('file.html', $file);

?>

 

While this would work, its messy and not very dynamic. Using a database to store your links you could simply have one script...

 

links.php

<html>
  <head>
    <title>links</title>
  </head>
  <body>
    <p>
<?php

  include 'dbconnect.php';
  $sql = "SELECT link, linkname FROM links"
  if ($result = mysql_query($sql)) {
    if ($mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        echo '      <a href="' . $row['link'] . '">' . $row['linkname'] . "</a>\n";
      }
    }
  }

?>
    </p>
  </body>
</html>

 

Which would achieve the same goal. It might look like more work but the major benifit is that you would then simply have another script which would allow you to add new links via a form and they would automatically appear.

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.