Jump to content

Recommended Posts

I've been using PHP includes for a while now but I've just started to try to get more advanced. (not much more)

 

I'm using this code to both generate my page titles and give my li tags a class of active so I can style them with css as being in a down state. All is working but I just need a more efficient way of doing this I think.

 

I currently have

 

<?php
$title = basename($_SERVER['SCRIPT_NAME'], '.php');
if ($title == 'index') {
$title = 'home';
}
$title = ucfirst($title);
$title = str_replace('-', ' ', $title);
$title = ucwords($title);
?>

 

Then in my unordered list I have

 

<?php if ($title == 'Product Selector'||$title == 'Kingston Range') {echo 'class="active"';} ?>

 

I could continue to say if title = a or b or c or d etc but could I use an array to store all of these titles and check to see if the title is in the array? or is there a better way to do this?

 

Thanks in advance

 

 

Link to comment
https://forums.phpfreaks.com/topic/232594-php-basics-help-writing-an-array/
Share on other sites

still not quite understanding .. but heres how to make multidementional arrays...

 

define a var

$title = array();

define a key

$title['index']=array();

value of a key

$title['index']['home'] = 'texthere';

 

to call

echo $title['index']['home'];// returns texthere to the screen hope this helps...

 

well rather than rework your code, I will show you how I solved the same issue using a tabbed css menu

 

$navarray['View Exhibitions'] = 'exhibitions.php';

$navarray['My Sales'] = 'mysales.php';

//etc

$outputstr = "<ul>";

foreach ($navarray as $key => $value)

    {

$outputstr .= "<li";

 

if (strpos($_SERVER['PHP_SELF'], $value)) $outputstr .= " class='selected'";

$outputstr .= ">\r";

$outputstr .= "<a href='" . $value  . "'>";

$outputstr .= $key;

$outputstr .= "</a>\r";

$outputstr .= "</li>\r";

}

$outputstr .= "</ul>";

echo $outputstr;

 

it might be of some use to you

also how do i make it look like code in the forum post? ( only joined today)

 

**once again my slow typing means someone else showed the same idea** :)

Just in case you don't want to redo your code:

 

$activetitles = array('Product Selector', 'Kingston Range');
if (in_array($title, $activetitles)) {echo 'class="active"';}

 

Just gotta populate the array with all the others.

 

also how do i make it look like code in the forum post? ( only joined today)

Use BB code tags like [ php ][/ php ] or [ code ][/ code ], without spaces

Just in case you don't want to redo your code:

 

$activetitles = array('Product Selector', 'Kingston Range');
if (in_array($title, $activetitles)) {echo 'class="active"';}

 

Just gotta populate the array with all the others.

 

Was just going to post something like this myself.  Definitely the way to go.

Cheers to everyone. I love an active forum. Some really useful info here I think the if in array is what I was looking for I just wasn't sure how to write it.

 

Btw to get your code to look like code you use code tags

 code here 

same on most forums.

 

Once again thanks. Really starting to love php, more of a front guy but it's just too powerful to not know more.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.