Ricky55 Posted April 3, 2011 Share Posted April 3, 2011 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 More sharing options...
blacknight Posted April 3, 2011 Share Posted April 3, 2011 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... Link to comment https://forums.phpfreaks.com/topic/232594-php-basics-help-writing-an-array/#findComment-1196363 Share on other sites More sharing options...
spiderwell Posted April 3, 2011 Share Posted April 3, 2011 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** Link to comment https://forums.phpfreaks.com/topic/232594-php-basics-help-writing-an-array/#findComment-1196366 Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 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 Link to comment https://forums.phpfreaks.com/topic/232594-php-basics-help-writing-an-array/#findComment-1196369 Share on other sites More sharing options...
KevinM1 Posted April 3, 2011 Share Posted April 3, 2011 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. Link to comment https://forums.phpfreaks.com/topic/232594-php-basics-help-writing-an-array/#findComment-1196370 Share on other sites More sharing options...
Ricky55 Posted April 3, 2011 Author Share Posted April 3, 2011 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. Link to comment https://forums.phpfreaks.com/topic/232594-php-basics-help-writing-an-array/#findComment-1196381 Share on other sites More sharing options...
Ricky55 Posted April 4, 2011 Author Share Posted April 4, 2011 Just a short note to say thanks this worked great. Link to comment https://forums.phpfreaks.com/topic/232594-php-basics-help-writing-an-array/#findComment-1196570 Share on other sites More sharing options...
spiderwell Posted April 4, 2011 Share Posted April 4, 2011 echo "thanks for the code markup advice"; Link to comment https://forums.phpfreaks.com/topic/232594-php-basics-help-writing-an-array/#findComment-1196580 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.