Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Change $num value based on page_title

    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 Code:
    <?php

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


    PHP Code:
    function page title() 
    $if page title ="value" then $num =3
    Thanks in advance

  • #2
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,559
    Thanks
    76
    Thanked 105 Times in 104 Posts
    Will it be stored in the database, or does it need to be calculated in the script?
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Calculated in the script if possible please

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Have a look a switch():
    PHP Code:
    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;


  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you for replying kbluhm - I'll give that a go.

  • #6
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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!

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    switch ( $page_title )
    {
        case 
    'Nottingham':
            
    $num 1;
            break;
        case 
    'Derby':
            
    $num 2;
            break;
        default:
            
    $num 0;

    Still not what you need?

  • #8
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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.

    PHP Code:
    <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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •