...

View Full Version : Change $num value based on page_title



mulder
03-01-2012, 12:29 PM
Hey all, I'm after a piece of code that changes the $num value based on a page title (or file name if page title cannot be done)



This is the piece of code that I have at present. I need to change the number value based on what page title or filename

<?php

$num = 1;
?>

I have to admit, apart from guessing that a $get_ function would be called, or an $if option e.g.



function page title()
$if page title ="value" then $num =3;

Thanks in advance

myfayt
03-01-2012, 01:14 PM
Will it be stored in the database, or does it need to be calculated in the script?

mulder
03-01-2012, 01:35 PM
Calculated in the script if possible please

kbluhm
03-01-2012, 01:42 PM
Have a look a switch (http://php.net/switch)():


switch ( $title )
{
case 'value 1':
$num = 3;
break;
case 'value 2':
$num = 6;
break;
case 'value 3':
case 'similar to value 3':
$num = 2;
break;
default:
$num = 1;
}

mulder
03-01-2012, 07:26 PM
Thank you for replying kbluhm - I'll give that a go.

mulder
03-02-2012, 01:09 PM
OK, maybe the switch isn't for me. Hopefully I can be clearer in what I would like.


The page title needs to remain the same.

I need to change the value of $num based on an element of the page title. So for example.

If page_title = Nottingham

then $num= 1

If page_title = Derby

then $num= 2

Thanks for your help so far, it is appreciated!

kbluhm
03-02-2012, 01:21 PM
switch ( $page_title )
{
case 'Nottingham':
$num = 1;
break;
case 'Derby':
$num = 2;
break;
default:
$num = 0;
}

Still not what you need?

mulder
03-02-2012, 01:36 PM
Cant seem to get it to work. Here's the background info. - Have a select box which will highlight an option based on $num value. On pages that I can write the page title this is fine as I also just use $num=3, which tells the select box to highlight option 3.

However, I have a forum, and on the board index and topic pages, the page title is written once the user posts; therefore I cannot decide the page title. Upon implementing your code above the select box does not highlight the desired option. rather it is using the default 0.


<select size=1 id="s1" onchange="window.open(this.options[this.selectedIndex].value,'_top')">
<option value="/index.php" <?php echo ($num == 1)?"selected":""; ?>>Home</option>

<option value="/content/index.php#c2" <?php echo ($num == 2)?"selected":""; ?>>Nottinghamshire</option>
</select>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum