Generating new html pages using php and MySQL

Discussion in 'PHP' started by chickenhouse, Jun 3, 2008.

  1. #1
    Hi,

    Does anyone know if it's possible to use php and a list of keywords stored in a MySQL database to generate a number of new html pages. Each page would be a permanent html page called keyword.html

    Thanks,
    Travis
     
    chickenhouse, Jun 3, 2008 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Nothing's impossible :)
    Try explaining a bit harder, cause i'm not sure what you want to accomplish.
     
    xlcho, Jun 3, 2008 IP
  3. chickenhouse

    chickenhouse Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    OK I'll give it a go.

    Imagine I have the following keywords

    cars
    trucks
    vehicles

    stored in a MySQL database

    I'd like to generate the html files

    cars.html
    trucks.html
    and vehicles.html

    in each html file you would have something like this

    <h1>cars</h1>

    <p>cars and really great to drive blah blah blah</p>

    and

    <h1>trucks</h1>

    <p>trucks and really great to drive blah blah blah</p>

    So the keywords in the MySQL database are variables that can be used in the name of a html file, in the title, in the keywords tag or anywhere.

    I'm OK using variables from MySQL databases within the html page. The hard part for me is working out how to generate these html files without manually programming each one.

    Thanks,
    Travis
     
    chickenhouse, Jun 3, 2008 IP
  4. phpPig

    phpPig Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This first thing is that your host must allow mod rewrite. If they do, you can do this by defining values in php code to query database and place the information on the page in the order you wish. You will also define a string that would in essence replace your url http://yoursite.com/index.php?task=whatever to http://yoursite.com/yourkeyword.html.

    If you have a page defining functions, you will need to define a function. If not, simply place it in your config file(or whatever it is named with database information).

    //////Function to change php to html//////
    function ($html)
    {
    $html = html_entity_decode($html, ENT_QUOTES);
    $html = ereg_replace("[^a-zA-Z0-9 ]", "", $html);
    $html = ereg_replace(" +", " ", $html);
    $html = str_replace(" ", "_", $html);
    return $html;
    }


    [this is just an idea on how to make it work inside your site.

    <?php
    $query = mysql_query("SELECT * FROM `sql_table_with_keywords_and_info`");
    while ($row = mysql_fetch_assoc($query))
    {
    $name = ($row['name']);
    if ($name != '')
    {

    { $url = ' . $row['id'] . '-' . $name . '.html'; }
    echo '<li><a href="' . $siteurl . '/' . $url . '">' . $row['name'] . '</a></li>';
    }
    }
    ?>]

    You would do the same as above to define site categories if you wish.

    You also need to write a file, .htaccess to turn rewrite engines on and define the url names.

    The htaccess file would look something like this.

    Options -Indexes
    RewriteEngine on
    RewriteRule ^([0-9]+)-([a-zA-Z0-9?-]+) index.php?task=view&id=$1&name=$2 [L]
    RewriteRule ^categories/([0-9]+).html$ index.php?task=viewcat&cat_id=$1 [L]
    RewriteBase /

    I hope that this helps.
     
    phpPig, Jun 3, 2008 IP
  5. chickenhouse

    chickenhouse Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Wow thanks for your quick reply.

    I don't quite understand it all yet but hopefully once I look into it more it will become clear.

    Just one question.

    Where would I place the code to generate the other html files? Would it all sit in index.php?
     
    chickenhouse, Jun 3, 2008 IP
  6. phpPig

    phpPig Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You would need your site to define the pages as index.php?task=view or something to that effect. If that is in place, then all of the html urls will automatically be written by your server. The string $name above would go in your site navigation and index page to show your links. You can mod that query also to show most recent, or randomly change the links as well.

    The function string would go in a php file that is used on every page such as your config or index if that is where your database information is. That string along with the .htaccess file placed on your server would immediately translate the php url to html. And the only thing that the $name string does is changes the url for site navigation.

    Also, your site may give 404 errors if the RewriteBase statement isn't include in the .htaccess file. You would also define the folder of your site behind the RewriteBase if it isn't the root folder on your site. Example: If my site is guitar.com and the folder for this php script is /les-paul then I would define that as RewriteBase /les-paul.....but if it is the root it would only be RewriteBase /
     
    phpPig, Jun 4, 2008 IP
  7. chickenhouse

    chickenhouse Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks I'll see how I go
     
    chickenhouse, Jun 4, 2008 IP
  8. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Erm could do something simple like this:

    
    <?php
    
    $query = mysql_query("SELECT * FROM keywords");
    while($arr = mysql_fetch_array($query)){
    	
    	//Create html content from keywords in database
    	$content='
    	<html>
    		<head>
    			<title>'.$arr[title].'</title>
    		</head>
    		<body>
    			<h1>'.$arr[header].'</h1>
    			<p>'.$arr[paragraph].'</p>
    		</body>
    	</html>
    	';
    	
    	$file = $arr[title];
    	
    	//Save html content in a file
    	file_put_contents($file, $content);
    }
    
    ?>
    
    PHP:
     
    lui2603, Jun 4, 2008 IP
  9. neuromante

    neuromante Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    you could make the script and pre-generate all of those pages. you would save cpu instead of having apache executing php for every request on the fly
     
    neuromante, Jun 4, 2008 IP
  10. chickenhouse

    chickenhouse Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Simple options are always the best. :)

    So if I created the php script as you suggested would it create the html pages on the server?

    I only have php on my server at the moment, I don't have a local copy.

    Then on the server I would need to restrict access to the php page to just myself and run it whenever I update the database.
     
    chickenhouse, Jun 4, 2008 IP
  11. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #11
    This makes an HTML file:
    
    $content='
    <html>
    <head></head>
    <body></body>
    </html>
    ';
    $name = 'page.html';
    file_put_contents($name, $content);
    
    PHP:
    You can follow what I put in my first post to get keywords from the database
     
    lui2603, Jun 5, 2008 IP
  12. surekhaweb

    surekhaweb Banned

    Messages:
    504
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    can any one translate this code in Xml Database and asp.net
     
    surekhaweb, Jun 5, 2008 IP
  13. chickenhouse

    chickenhouse Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hi,

    I've got the information from the database and everything works well up until I create the page.

    If I just echo $content; it shows what I want.

    But file_put_contents($name, $content); isn't working.

    I'm sure it's just an access problem. I can get into my server and change the settings just not sure what to save it too. Do I need to make the folder where I want the pages to go to be read/write by everyone?

    Is there someway of allowing just that php code to write to the server?
     
    chickenhouse, Jun 5, 2008 IP
  14. chickenhouse

    chickenhouse Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Ignore that last question.

    Just changed the access to write for others and it works. Just need to change it back once I'm done. :)

    Thanks very much.
     
    chickenhouse, Jun 5, 2008 IP
  15. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Ah yeh file_put_contents needs write access to create files.
    Glad it helped ;)
     
    lui2603, Jun 5, 2008 IP
  16. chickenhouse

    chickenhouse Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Hi,

    It all worked fine but now I have a new problem.

    I'm trying to dynamically create a folder and files within the folder.

    If I use mkdir($titlefolder, 0777);

    It doesn't create the folders as I expect. They have rwx r-x r-x access But I need to have at least rwx r-x rwx access as I did before to create the files.

    Because the user is set to apache I can't change the access manually.

    What can I do to get the access I need? Is there some way to sign in to my domain so it will allow me to do what I want?

    Thanks,
    Travis
     
    chickenhouse, Jun 9, 2008 IP