Jump to content

[SOLVED] Little php help please.


t31os

Recommended Posts

Hi all,

 

Have spent a couple of days attempting to learn some further PHP coding as my current learn of knowledge is some what lacking.

 

I know a little about case and switch, how to create an array, use includes and call content etc.....

 

Now i've got a site (just personal, hobby) which i'm running locally via a WAMP server which i'm having a few headaches with.

 

I'll explain the layout so you get the jist of where i need help.

 

index.php - of course the main page, simply consists of includes

header.php - the header of my document

footer.php - the footer

content.php - to be used to load varying content into an area of the page, i don't want to keep creating new pages and include the header/footer again and again.

 

Included in the header is a further include for a css tab menu (menu.php) that i'd like to have change dependant on the page loaded.

 

I have split the the active and non-active links of the header into arrays inside this file, like so...

 

<?php
// Tab menu array

// First array to be called when
// the page is current loading a none
// linked tab title
$menu_active = array(
                        'Home' => '<li><span>Home</span></li>'
                        ,'Projects' => '<li><span>Projects</span></li>'
                        ,'Guides' => '<li><span>Guides</span></li>'
                        ,'Other' => '<li><span>Other</span></li>'
                        );
// Second array is called for page
// not currently in view
$menu_page = array(
                        'Home' => '<li><a href="index.php">Home</a></li>'
                        ,'Projects' => '<li><a href="#">Projects</a></li>'
                        ,'Guides' => '<li><a href="#">Guides</a></li>'
                        ,'Other' => '<li><a href="#123">Other</a></li>'
                        ,'' => '<li><a href="#">None</a></li>'
                        );

?>

With the menu i have a <li> element containing <a href> will be a normal tab, and those containing a <span> element are the current/active tab.

 

In the header is the following....

<div id="tabs">
        <ul>
<?php
echo    
        $menu_active['Home'],
        $menu_page['Projects'],
        $menu_page['This'],
        $menu_page['That'],    
?>
        </ul>
</div> 

Aware the linsk mostly point nowhere yet... Want to make it work first...

 

Inside the content.php i have the following.... (early stage)

 

<?php

$page = $_GET['page'];

if ($page=="")
{ $page = "projects/index.php"; }


switch($page){
                case "1":
                include('projects/1.php');

                case "2":
                include('projects/2.php');
}


$blah = $_GET['page'];

if ($blah=="")
{ $blah = "blah/index.php"; }


switch($blah){
                case "1":
                include('blah/1.php');

                case "2":
                include('blah/2.php');
}

?> 

Can anyone explain how i can firstly have the content.php return content 'x' when no variable/case is present ($page or $blah), so basically when i'm on the index, $page and $blah will not be in the URL and as such i'd like something else displayed.

 

Secondly how do i then have the menu check to see if $page of $blah has been called.

 

As said, i know only little about php, and this is some of what i've been learning over the last few days.

 

I've found it hard to find a guide that isn't too basic or too advanced to help, though some have given me pointers, they don't really apply to what i'm wanting to do.

 

I'm wanting to create a site with PHP, HTML and CSS, no database for my personal projects and/or guides etc...

 

I can find info on using case/switch to change the content, but do not know how to check what the value or variable is to display different code into the menu.

 

I think i need something that simple says.....

 

If page is equal to index....

 

<?php
echo    
        $menu_active['Home'],
        $menu_page['Projects'],
        $menu_page['Guides'],
        $menu_page['Other'],    
?>

else if page is Projects (when the project case is equal to anything - index.php?project=1 <-- example)...

<?php
echo    
        $menu_page['Home'],
        $menu_active['Projects'],
        $menu_page['Guides'],
        $menu_page['Other'],    
?>

Sorry for the long winded post, trying to explain best i can....

 

If anyone has any pointers, links or suggestions they are all welcome, thanks...

Link to comment
https://forums.phpfreaks.com/topic/123831-solved-little-php-help-please/
Share on other sites

Ok refined my code after many hours of playing around and some help on another forum..

 

I've got the content area working just perfectly, so only need help on the menu related area now, here's where i'm stuck now.

 

I do know i need to check the url string instead but unsure exactly how to apply it, the code below was supplied elsewhere for use on seperate pages, which i won't be doing, i'll be using a switch as said before index.php?myswitch or index.php?myswitch=example.

 

My menu array.

$mylinks = array(
'Home' => "index.php",
'Projects' => 'index.php?project',
'Guides' => 'index.php?guides',
'Other' => 'index.php?other');

 

Where i call the menu and array....

$current = $_SERVER['SCRIPT_NAME'];

foreach($mylinks as $key => $link) 
{    
if($link == $current)
{ echo '<li><span>' . $key . '</span></li>'; } 
else 
{ echo '<li><a href="' . $link . '">' . $key . '</a></li>'; }
} 

 

Anyone able to suggest how i would have $current check if a switch is contained in my url?... ie. index.php?guides=page1 at the moment every page is $current because it contains index.php.

 

Perhaps some form of isset usage?

 

Any help is greatly appreciated at this stage.

Ok, nearly there....

 

$mylinks = array(
'Home' => "/index.php",
'Projects' => '/index.php?project',
'Guides' => '/index.php?guides',
'Other' => '/index.php?other');  

$current = ($_SERVER['SCRIPT_NAME']);
if(strlen($_SERVER['QUERY_STRING']) > 0) $current .= '?' . $_SERVER['QUERY_STRING'];  

foreach($mylinks as $key => $link) 
{    
if($link == $current)
{ echo '<li><span>' . $key . '</span></li>'; } 
else 
{ echo '<li><a href="' . $link . '">' . $key . '</a></li>'; }
} 
echo $current; // For my testing purposes.

 

Can anyone tell me what i need to do to have $current check if a switch has a case present, at the moment my tabs show as active when on the index.php or index.php?switch, but not when a case is supplied.. ie. index.php?switch=case

 

Thanks in advance for any help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.