Jump to content

Dynamically creating 2000+ pages from DB


xtrafile

Recommended Posts

Yes I need to create 2000+ pages.

 

So I should instead use one page in a loop, loading the selected page from the DB?

 

How would I have each of them setup though to call the page from the database? I need something time-efficient so I still do not have to manually load the data from the DB.

 

Thanks

Database driven websites do not have a physical page for each viewable web page. If your not sure of the logic behind dynamic websites, you've probably a long way to go.

 

Theres a great link in my signiture that should get you started. Or, if you have a specific question, ask it.

A small example of some of the logic.

 

listarticles.php

<?php

  // connect to db.
  
  $sql = "SELECT id,title FROM articles ORDER BY stamp";
  // query the database for all articles.
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        // create a link to articles.php passing it the article id via the url.
        echo "<a href='articles.php?id={$row['id']}'>{$row['title']}</a><br />";
      }
    }
  }

?>

 

articles.php

<?php

  if (isset($_GET['id'])) {
    // get article id from the url.
    $id = mysql_real_escape_string($_get['id']);
  } else {
    // a default article id.
    $id = 1;
  }

  // connect to db.

  $sql = "SELECT title,data FROM articles WHERE id = '$id' LIMIT 1";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      echo "<h1>{$row['title']}</b1>";
      echo "<p>{$row['data']}</p>";
    }
  }

?>

 

These two simple scripts could display hundreds/thousands even millions of articles. Of course they would need some tweaking.

 

The first page simply lists all articles as links of titles sorted by date. When you click on a link you are taken to the second script which then displays the data for said article.

 

Hope this helps some.

There is no pagination in my scripts. Pagination is the process of splitting results accross multiple pages.

 

For instance, I stated that my script would need tweaking before they could serv thousands of articles. The reason is that listarticles.php will simply create links to all articles on one page. Thousands of articles would meen thousands of lines and pretty quickly the server would die, and the pages would not load. You might add pagination to the script in order to have it display say 20 links per page. You then have a next and previous link at the bottom to sort through more links.

 

That is pagination. The scripts I posted are just one example of some simple dynamic page generation logic.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.