Niallll Posted February 8, 2010 Share Posted February 8, 2010 Hello there, Apologies if this is the wrong forum or this question has been asked before, I'm finding my feet in php, and not entirely sure the name of what I want to do. I just would like some advice on whether I'm going down the right route or not. All I'm trying to build is a very simple dynamic website, to save me writing out the same html on each page. The way I'm going is to include one php file at the beginning of the document, defined by a query in the address at the moment. (index.php?main=dogs for example) In the included php file, I define a list of variables, which get referenced within my index file. Here's what's in index.php: (heavily trimmed to make it easier to read, everything is wrapped in divs and styled, and not about dogs ) In my index.php, I have: <head> <?php include "pages/$_GET[main].php"?> <title><?php print $title?></title> </head> <body> <?php print $maintext?> <img src= "<?php print $pic?>"> </body> In dogs.php I have <?php $title="Dogs" ?> <?php $maintext="Lovely Dogs" ?> <?php $pic="dog.jpg"?> So cats.php is <?php $title="Cats" ?> <?php $maintext="Horrible Cats" ?> <?php $pic="cat.jpg"?> I'm just wondering if this is accepted practice? Seems like there would be a better way. I'm trying to avoid storing this information in a database, because I wouldn't even know where to start with that. But eventually I would like the information to be updated via some sort of cms, and my idea was I could just edit cats.php to read <?php $title="database path of cats" ?> Any help? Link to comment https://forums.phpfreaks.com/topic/191352-advice-on-dynamic-pages/ Share on other sites More sharing options...
jl5501 Posted February 8, 2010 Share Posted February 8, 2010 You are on the right track Do not forget, you do not need your php code and the html to be in different files. It is quite common for someone doing what you are doing, to have a single php file (perhaps index.php) which contains all the php and html. If you are going to have other similar html pages with slightly different layout but with common header, footer and navigation, then it is good to have those areas handled by include files. so on your master php page, you extract the information required from the database, and fit it into the html as required. Here is a simplified example <?php error_reporting(E_ALL); ini_set('display_errors',1); here you have code to get your database info lets say you end up with an array of info called $data <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <?php include("header.php");?> <div class="wrap"> <div class="header"> </div> <div class="pagemain" style="border-left:1px solid #ccccff;"> <?php if(!$prodinfo->img_path) $prodinfo->img_path = 'nophoto.gif'; $pdesc = str_replace('<','<',$prodinfo->product_desc); //$pdesc = html_entity_decode($pdesc); $pdesc = str_replace('>','>',$pdesc); $pdesc = str_replace('"','',$pdesc); //$pdesc = str_replace('""','"',$pdesc); $pdesc = str_replace('&','&',$pdesc); $pdesc = str_replace(' ','',$pdesc); $pdesc = str_replace('<br />','',$pdesc); ?> <div style="text-align:center;margin-top:4px;float:left;width:160px"> <div class="catheading"><?php echo $prodinfo->name;?></div> <img src="<?php echo $prodinfo->img_path;?>" border="0" style="margin-top:50px;" /> </div> <div style="margin-top:4px;margin-left:10px;float:left;width:510px;background:<?php echo $config->prod_back_col;?>;text-align:left"> <div style="width:490px;margin:10px;"> <?php echo stripslashes(stripslashes(stripslashes($pdesc))); echo '<br />'; if($prodinfo->pdf) echo '<a href="http://www.xx/pdfs/'.$prodinfo->pdf.'">Download PDF</a>'; ?> </div> </div> </div> <?php include("footer.php");?> </div> </body> </html> I have taken a page from a real site of mine and chopped out some stuff, but you should be able to get the idea Link to comment https://forums.phpfreaks.com/topic/191352-advice-on-dynamic-pages/#findComment-1008864 Share on other sites More sharing options...
jl5501 Posted February 8, 2010 Share Posted February 8, 2010 I missed a ?> php close tag in my last post which should be before <!DOCTYPE so that area should be ..... here you have code to get your database info lets say you end up with an array of info called $data ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> Link to comment https://forums.phpfreaks.com/topic/191352-advice-on-dynamic-pages/#findComment-1008865 Share on other sites More sharing options...
Niallll Posted February 8, 2010 Author Share Posted February 8, 2010 Thanks for your swift reply, jl5501. I have to confess that I'm slightly confused by your code, but I'm glad I'm on the right track. (means I'm understanding correctly.) What I posted in my example is pretty much all the php I know, so I had to do a bit of searching to find out what was a variable and what was a function in your example, am I right in assuming $prodinfo is defined by the user, similar to how I have used $main in my example? I suppose I could put it all in one .php file without calling anything else, but in reality, my $maintext is a few paragraphs long, and I wouldn't want to bloat the index file more than needed, unless I'm misunderstanding you? I'm re-reading your code a few times to fully understand what's going on, so I may end up getting back to you with a few more questions. But thanks a lot your help. Link to comment https://forums.phpfreaks.com/topic/191352-advice-on-dynamic-pages/#findComment-1008940 Share on other sites More sharing options...
jl5501 Posted February 8, 2010 Share Posted February 8, 2010 It would be intended, that the content (your few paragraphs) would be stored in a database and managed by a page with an editor to change that content easily. It could also be in a text file instead of a database if no database was available Link to comment https://forums.phpfreaks.com/topic/191352-advice-on-dynamic-pages/#findComment-1008968 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.