FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need Help in PHP Coding





k_s_baskar
Hi,
I am having MySQL Database with fields laptop name, price, review. This database consist of 1500+ records.

Now i want to create separte html files for each record where files names should be laptopmodel.html, laptopmodel2.html, laptopmodel3.html and so on..... I am new to php can somebody please help me create simple script to generate 1500+ files automatically.

thanks in advance
Peterssidan
why do you want to create one file for each record? Often it's best to just have one file and show different content depending on url parameters. You can even make it look like different files even if it's not using mod rewrite.
Fire Boar
You want .htaccess here.

Code:
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ routing.php?q=$1 [L,QSA]


Then in your routing.php script, or one of the files included by it:

Code:
function arg($n = NULL) {
  static $arguments;
  if (!isset($arguments)) {
    $arguments = explode('/', $_GET['q']);
  }
  if (!isset($n)) {
    return $arguments;
  }
  if (isset($arguments[$n])) {
    return $arguments[$n];
  }
}


You can now use the arg function in your php script to discover what the URL requested was. Example: www.yourdomain.com/path/to/file.html. arg(0) will return 'path', arg(1) will return 'to', arg(2) will return 'file.html'.

You can now use PHP scripts to figure out exactly what you want to do.
rvec
you should make one PHP file just like two other posters just said. make it something like laptopmodel.php?id=0, laptopmodel.php?id=1, laptopmodel.php?id=2
and instead of making HTML files just make one dynamic PHP file which fetches the rows out of the MySQL database at the user's request.
ProwerBot
One seperate html page? lol that would just get annoying eventualley.


It's a lot more convienent and much easier to just have one php file that grabs the info. from the database depending on the URL parameters.
k_s_baskar
ProwerBot wrote:
One seperate html page? lol that would just get annoying eventualley.


It's a lot more convienent and much easier to just have one php file that grabs the info. from the database depending on the URL parameters.


Accept your point. But for some seo purpose i need separate html file for each record. I found a useful software named webmerge which can be used to solve my purpose. But it is $99. I know this process can be completed with simple php coding by some experts and searching for them...
Fire Boar
Use my method, it fakes the presence of HTML files, which is just as good as having the physical file for SEO, and much much better for maintainability.
erlendhg
Fire Boar wrote:
Use my method, it fakes the presence of HTML files, which is just as good as having the physical file for SEO, and much much better for maintainability.


Agreed.
The search engines can't tell the difference, and with mod rewriting you won't have to regulary update 1500+ html files, since there actually just is one php script that retrieves data from the database on-the-fly Wink
The-Nisk
This is kind of funny, a guy asks for specific help and all the stubborn hardcode gurus are telling him that their way is better =D
I agree tho, PHP files are the way to go, I used to have .html files before i could use php, it was a nightmare trying to edit anything.
Fire Boar
The-Nisk wrote:
This is kind of funny, a guy asks for specific help and all the stubborn hardcode gurus are telling him that their way is better =D


Well, there would be a reason for that. In this case, the poster is asking for something specific because he is under the mistaken impression that what he is asking for is the only way to achieve what he wants. This is by no means true, and we (in my case specifically) are suggesting an alternative that will be a lot easier to maintain, on the grounds that actually answering the specific question would be a waste of time and just cause the OP more problems in the future than if he had gone with the suggestion.
k_s_baskar
Fire Boar wrote:

Well, there would be a reason for that. In this case, the poster is asking for something specific because he is under the mistaken impression that what he is asking for is the only way to achieve what he wants.


I know you people r doing great and suggesting good things to this community. But i want to distribute this content in cd to students who are not having internet connection. They want to view this content offline without installing mysql and php. So i decided to make separate html files which can be used in online and offline, please suggest me if you have some other idea better than this. Smile
Fire Boar
Oh, I see. In that case, why are you concerned about SEO? Content distributed on a CD is never indexed by search engines.

I'd suggest listing the records in javascript, then displaying one based on the contents of the query string (e.g. laptopmodel.html?m=1 for model 1, ?m=2 for model 2, and so on).

If you need an SEO version, use the PHP method, because SEO is only an issue when the content is actually distributed on the internet, where PHP is almost unanimously available. If you need an offline version, javascript is the way to go. If you need both... well, make both. That's still far preferable to the alternative, 1500 different HTML files.
rvec
1500 different HTML files is better than putting everything in one with a lot of javascript. Yes you can use PHP to make this with file_put_contents
http://www.php.net/manual/en/function.file-put-contents.php
sonam
It is lot of ways how to create the same thing. Here is my way. Note, before you run this script on your localhost or some other host create right folder and if you are on Apache give right chmod for writing.

Sonam

Code:
?php
session_start();
$_SESSION['all_nemes'] = array("laptop1", "laptop2", "laptop3"); // grab here names from your data base

$count = count($_SESSION['all_nemes']);
$folder = "cd_htmls"; // folder where you want to save files

for($i=0; $i<$count; $i++) {
   $name = $_SESSION['all_nemes'][$i] . ".html";
   
   // this part is very important. create here full html if you need, extract datas from MySql, etc.  I am just put names for examples
   $html_content = $_SESSION['all_nemes'][$i];
   
   
   $fillFile = $folder . "/" . $name;
   $newFile = fopen($fillFile, "wb");
   fwrite($newFile, $html_content);
   fclose($newFile);
}

echo "DONE!";
?


Because I cannot post this reply I must remove brackets < > on start and end of php code.
sonam
One more thing. You can create all 1500+ htmls in first step and then just part to part build html_content because script will automaticaly replace old files with new one.

Sonam
Related topics
Forum
Hi, I'm blue ^_^
Do you use dreamweaver for web design.
[HELP] PHP -> Scripting
Needing a Bit Og Help {PHP}!
need some help - php switch code
phpBB Forums Allowed Or Supported on Server 2?
PHP Problem
Need Help with this CSS/HTML Thing again.
PHP Form Question
[HELP] PHP (and some MyBB) Administration
Session directory permissions issue
PHP commands need help!
[Help] php script to post to twitter
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.