Jump to content

Auto-generate pages from MySQL data


happyhappy

Recommended Posts

Firstly. Great forum - I already sorted one problem just by searching for it :) However - I just can't find any tutorials that can help me do the following (although I'm sure it's a very common task): Can anyone point me in the right direction?

 

Automatically generate web pages from data in a mysql database?

 

It needs to:

 

1 - auto-create a webpage (based on a template page)

2 - assign a url from a database field

3 - populate the page with information from the mysql table

 

Any help is much appreciated.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/119132-auto-generate-pages-from-mysql-data/
Share on other sites

I'm quite new to MySQL & PHP. I do get the basics > i.e I can output a list of items from a MySQL table, and have worked out how to paginate the results.

 

I now need to learn how to generate pages from that table data so that my users can browse through the listing and click to be taken to a page which has the full details of the items.

 

e.g:

 

Listing 1

Some information about listing 1 goes here

For more info - click here (the info on the page is autogenerated)

 

I'm sure I can figure it out if I can find a tutorial . . . I just can't find one lol

 

Help much appreciated :)

What ure asking involves basic php and mysql knowledge. I highly suggest reading any books on php & mysql as its not rocket science, but it needs some literature.

 

A basic example:

<?php
if(isset($_GET['lid'])){ //check if the get variable 'lid' is set which is the id of a listing
     $lid = mysql_real_escape_string($_GET['lid']); //clean input
     $results = @mysql_query("SELECT title, content FROM listings WHERE id=$lid") or die(); //make the damn query
     if(mysql_num_rows($results) == 1){ //check if the query returns smth
          $values = mysql_fetch_array($results); //fetch the returned data in an array
          echo $values['title'] . '<br />' . $results['values']; //print the listing
     } else{
          echo 'The listing you are requesting does not exist.'; //if it didnt return, show this message
     }
} else{
     $results = @mysql_query("SELECT id, title FROM listings ORDER BY title") or die(); //make another damn query
     while($values = mysql_fetch_array($results)){ //fetch the data in an array and loop through them
          echo $values['title'] . '<br />'; //print the title
          echo "<a href='mypage.php?lid=" . $values['id'] . "'>Read more</a>"; //print a raedmore link which passes the get variable
     }
}
?>

 

That should get u in the business.

Working with templates is another issue, though, and an important one.

Many coders do not separate HTML code from PHP. But when you want to separate function and layout, which is usually a good idea, I would advise to call a template script from PHP and call the data from there.

 

Modify the code above as follows and name it "example.php":

<?php
function getData () {
// quote: code taken from example above
if(isset($_GET['lid'])){ //check if the get variable 'lid' is set which is the id of a listing
     $lid = mysql_real_escape_string($_GET['lid']); //clean input
     $results = @mysql_query("SELECT title, content FROM listings WHERE id=$lid") or die(); //make the damn query
     if(mysql_num_rows($results) == 1){ //check if the query returns smth
          $values = mysql_fetch_array($results); //fetch the returned data in an array
          echo $values['title'] . '<br />' . $results['values']; //print the listing
     } else{
          echo 'The listing you are requesting does not exist.'; //if it didnt return, show this message
     }
} else{
     $results = @mysql_query("SELECT id, title FROM listings ORDER BY title") or die(); //make another damn query
     while($values = mysql_fetch_array($results)){ //fetch the data in an array and loop through them
          echo $values['title'] . '<br />'; //print the title
          echo "<a href='mypage.php?lid=" . $values['id'] . "'>Read more</a>"; //print a raedmore link which passes the get variable
     }
}
// end quote
}
require_once 'your_template.tpl.php';
?>

 

And this is your template:

// your_template.tpl.php
<html>
<body>
<p>this is my data:</p>
<? getData() ?>
</body>
</html>

 

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.