Page 1 of 1

PHP switch/case

Posted: Fri Jul 23, 2010 1:22 pm
by lstables
Hi All,

I have a page called: "right_side.php"

which includes the following:

Code: Select all

<?php
	switch($_GET['page']) {
		case "name-of-the-page" :
		echo 'Echos out the content placed here
		';
		
		break;
>php
But I have an htaccess file the enables friendly urls for seo and the "right_side.php" file will not display even though i have included it in my template.

htaccess file

Code: Select all

SetEnv DEFAULT_PHP_VERSION 5

Options +FollowSymLinks
  RewriteEngine on
   
  # 301 Redirect all requests that don't contain a dot or trailing slash to
  # include a trailing slash
  # except for form POSTS
  RewriteCond %{REQUEST_URI} !/$
  RewriteCond %{REQUEST_URI} !\.
  RewriteCond %{REQUEST_METHOD} !POST$
  RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
 
  # Rewrites urls in the form of /parent/child/
  # but only rewrites if the requested URL is not a file or directory
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?page=$1 [QSA]
Can anyone advise how to do this to get the right_side.php file to display on the relevant page in the switch case.

The SWITCH/CASE works when mod_rewrite is disabled, but need to enable it with friendly URL's....

Thanks in advance.
Lee

Re: PHP switch/case

Posted: Fri Jul 23, 2010 1:40 pm
by NaN
Where do you include the file?
Maybe you can access the current page by the content object or the global gCms object.

Re: PHP switch/case

Posted: Fri Jul 23, 2010 1:43 pm
by lstables
I included the right_side.php file in the root of the directory

How does global gCms object work?

What do I need to do in order to make it work

Re: PHP switch/case

Posted: Fri Jul 23, 2010 1:53 pm
by NaN
Okay, so so far, the file is placed in the root of your CMS directory.
But i mean where in your page is it included?
How do you include the script?

Why don't you create a plugin or UDT instead?
It is much easier to write php scripts if they are in context of the cms by default than to create external stuff that needs info from the cms.

The gCms object contains almost everything you need ;)
Most plugins or tags are just tiny wrappers to get data of that object because it is much easier to write e.g. just {content} to get the page content instead of accessing it by e.g. {$content_obj->mProperties->mPropertyValues.content_en}.

But first it would be better to know how you include the script.
Maybe there is an even much easier way doing this with smarty in your template or content.

Re: PHP switch/case

Posted: Fri Jul 23, 2010 1:59 pm
by lstables
in my template i have included the file like so

Code: Select all

<div id="right">
				{php}include('right_side.php');{/php}
			</div>
Like I said this worked without friendly urls but since friendly urls have been enabled this doesnt seem to work

Re: PHP switch/case

Posted: Fri Jul 23, 2010 2:02 pm
by lstables
in right_side.php i have this too, this may help

Code: Select all

$result = mysql_query("SELECT * FROM cms_module_news ORDER BY news_id LIMIT 2");
		while($row = mysql_fetch_array($result)) {
			echo '<div class="news_title"><a href="index.php?mact=News,cntnt01,detail,0&cntnt01articleid='.$row['news_id'].'&cntnt01returnid=67">'.$row['news_title'].'</a></div>';
			echo '<div class="news_title">'.$row['news_date'].'</div>';
			
			echo '<br /><hr><br />';
		}
I'm trying to acheive that on different pages different content appears on the right hand side, I know I could use Global Content Blocks, but I would have to do 20 or templates too. whereas a switch case works fine.

Re: PHP switch/case

Posted: Fri Jul 23, 2010 2:28 pm
by NaN
I would suggest to create a userdefined tag.
So you can use the CMS api functions to query the database etc.
It is much more safe and ... yeah, pretty easy.

If it is all about related news articles (i just believe that because of your snippet) i believe you could just use news categories to show page related news.
Add an additional content block to your template like this:

{content block="news_category" oneline=true label="Enter category name here" assign="news_category"}
{news category=$news_category}

This will add an additional text input field to the edit area in backend where you just can enter the related news category of the page.
In frontend the entered value is not just printed out but assigned to a var named "news_category" that you can use this way {$news_category}.
This var is now used as value of the param category of the news module.
So the news module will display only news articles of that category you entered in the backend when editing the page.

If it is about different content in general and not just different news categories you could try two things.
1.: use such content blocks to enter the whole module, plugin or whatever smarty can handle.
2. try AdvancedContent module. There you can add different input conrtols to your editarea like dropdowns etc.
I created a template with different content blocks using AdvancedContent.
The editor in the backend can select up to 8 modules from a dropdown to be displayed in a sidebar and define some params to each module. And this for each page individually.

So no php script, global content or whatever is needed.
If this sounds like a solution to you i will post some examples here.

Re: PHP switch/case

Posted: Fri Jul 23, 2010 2:32 pm
by lstables
It's not really news articles it's just all html placed in php case tags to show when that page is requested from HTTP

Re: PHP switch/case

Posted: Fri Jul 23, 2010 2:56 pm
by NaN
Hardcoded HTML?
Wouldn't it be better to manage that content also with the CMS?

Re: PHP switch/case

Posted: Fri Jul 23, 2010 2:59 pm
by lstables
i think you have mis-understud what I'm trying to acheive.

When I had www.domain.com/index.php?page=my-custom-page

I have a template with {content} in for all content on the left hand side and used {php}include('right_side.php');{/php} for the right hand side which worked.

Then when I enable freindly urls this stopped working so am guessing the php page needs adjusting slightly and also the htaccess file needs amending.

It's easier to the php include has I have about 10 differnet cases the go on the right side the page on 10 different pages.

Re: PHP switch/case

Posted: Fri Jul 23, 2010 3:06 pm
by NaN
Just some thoughts.
Okay.

Try this in your php script to get the page instead of the $_GET array:

Code: Select all


global $gCms;
$pageinfo =& $gCms->variables['pageinfo'];
$page_alias = $pageinfo->content_alias;
$page_id = $pageinfo->content_id;


Re: PHP switch/case

Posted: Fri Jul 23, 2010 3:12 pm
by lstables
So just top clarify I would do:

global $gCms;
$pageinfo =& $gCms->variables['pageinfo'];
$page_alias = $pageinfo->content_alias;
$page_id = $pageinfo->content_id; {
case "page-name" :
echo ' echo content ';

break;

Re: PHP switch/case

Posted: Fri Jul 23, 2010 3:48 pm
by NaN
This is ... just wrong.
Where is your switch now?
It is just to get the current page info.
You now can do switch($page_alias) {...} or switch($page_id) instead of switch($_GET['page']):

Code: Select all


global $gCms;
$pageinfo =& $gCms->variables['pageinfo'];
$page_alias = $pageinfo->content_alias;

switch($page_alias) {
    case 'home': 
        doSomething();
        break;
    ...
}


Re: PHP switch/case

Posted: Fri Jul 23, 2010 3:59 pm
by lstables
ok tried that and it takeds away the doctype ans stylesheet and desnt even display the right side content displyed in the echo.

Re: PHP switch/case

Posted: Fri Jul 23, 2010 4:02 pm
by lstables
Sorry, my mistake it does work, thanks dude your a life saver....

I put 2 ss on switch like a wally lol

Thanks again dude.