Forum Moderators: coopster
I'm building a CMS and would like to use an OO approach with classes and functiosn but I'm not too sure what I'm doing. I'm reading Sams PHP & MySQL Web Development (Welling and Thomson) and it is giivng me ideas but not answering specific questions.
I have some code to determine which category the website is in
$title = 'Page name here - tagline here';
$nav_link = '<a href="/link_destination/">link text</a>';
$body = '<body id="www-example-co-uk" class="no-subnav">';
if (isset($_GET['category'])){
$home_link = '<a href="/">home</a>';
$body = '<body id="www-example-co-uk" class="no-sidebar">';
switch($_GET['category']){
case "cat_1":
$title ="Page title - category name";$nav_link = 'link text'; // remove link to current page
$body = '<body id="cat-1-page" class="sidebar">';
break;
etc;
Then in the page I've included the file the code is in and done this..
<?php echo $title;?> in the relevant palces with relevant variable names. I''d like to do this with a class or a function so I don't have to include the switch statement everytime I need to add comlex feature sto an individual page (e.g. some pages may have a different content structure or whatever) or include bigger code blocks inside each case e.g.
case = cat 1;
$sql= "select data from table where cat = 1"
I hope that made sense? any ideas? Even pointers to good books or good web tutorials would be great as I haven't found too many yet.
[edited by: jatar_k at 6:19 am (utc) on Feb. 9, 2005]
The switch statement is there finidng out what catgeory the page is on and modifying certain attributes. It doesn't render an entire page with each case in the switch, instead a html page sits there and displays various echo statements e.g. $title.
However, I would like functions to sit in some of the cases (in the switch statement) that grab the content from the database but, the problem I'm having is how to show that content on the page in the place I want it to be.
switch{$_GET['category']}
case ="cat_1";
$content = get cat_1_content();
and then the function would be
function get cat_1_content(){
<<mysql get query here>>
echo $row_get_cat_1_content['body'];
return;
}
and then on the page it would be a simple
<?php echo $content;?> in the relevant place but, this code shows the $content above the doctype and everything else but not in the specifed location. I should dump the echo statement in the fucntion but then I have no idea how to get the info into the variable $content in the switch statement.
Even pointers to good books or good web tutorials would be great as I haven't found too many yet.
[webmasterworld.com...]
[webmasterworld.com...] (see msg #7)
e.g.
function generate_content($category){
global $title, $description;
if!(preg_match('#^[A-Za-z\s0-9]+$', $category)) die('bad input'); // check user input of $_GET
$title = '<title>mysite.com - '.$category.'</title>';
$description = 'Brilliant, authoritative article on the subject of '.$category;
$somethingelse = 'Definitely your favorite for '.$category;
}
<?php
generate_content($_GET['category']);
echo '<dtd blah blah HTML blah>';
echo '<head>'.$title.'</head>';
/* a lot more code in between */
echo $description;
echo $somethingelse;
The var set as $somethingelse here won't show since it wasn't globalized, but all the rest will show up properly, even though the variables were generated right at the beginning.
Another thing you can do is set a variable (if you need lots of info, an array) and have your function return this.
Learn a lot about functions before trying to figure out OO, functions are a lot more 'basic'.