SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: calling link variables - a better way?

  1. #1
    SitePoint Wizard
    Join Date
    May 2002
    Posts
    1,343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    calling link variables - a better way?

    The following works as it is, hyphenating with lower case.
    There may or may not be a state variable on the page.

    PHP Code:
    <?php $city="Cerro Gordo"
    $state="IL"?>
     
    <a href="location-<? echo strtolower(str_replace(" ""-"$city)); if ($state !=''){echo strtolower("-".$state);} ?>.php">CityState</a>
    gives:
    location-cerro-gordo-il.php

    But something tells me there is a better way of forming this, rather than the seperate second condition. (these links are being placed on many areas and pages throughout the site)

  2. #2
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    California
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, I would avoid the ugliness of doing this in HTML. Second, I tried sticking the intended formatted value into a temporary variable. This seemed a lot cleaner.

    PHP Code:
    <?php
    $city
    ="Cerro Gordo"
    $state="IL";
    $formatted_loc strtolower(str_replace(" ""-"$city));
    if (
    $state !='')
    {
        
    $formatted_loc .= strtolower("-".$state);
    }
    echo 
    '<a href="location-' $formatted_loc '.php">CityState</a>';
    ?>
    The Banana Stand - an Arrested Development fansite
    LC-3 Help - tutorials on the LC-3 educational assembly language

  3. #3
    SitePoint Wizard
    Join Date
    May 2002
    Posts
    1,343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I can back up for a moment (only this snippet, please)

    While this works on a testing page by itself:
    PHP Code:
    <a href="location-<? echo strtolower(str_replace(" ""-"$city)); if ($state !=''){echo strtolower("-".$state);} ?>.php">CityState</a>
    When I insert as equal to a variable from another page (includes file)this is already working, to which I am trying to add the hyphenated state:

    PHP Code:
    <a href="location-'.strtolower(str_replace(" ", "-", $city)).'.php">CityState</a
    But when I append the hyphenated state, it doesn't go, why? Tried with and without the echos,displacing the ending colon, etc

    PHP Code:
    <a href="location-'.strtolower(str_replace(" ", "-", $city))  if($state !=''){strtolower("-".$state);}.'.php">Citystate</a
    NOTE: I'm trying to check for the existence of the state variable before appending it.
    Last edited by datadriven; May 4, 2007 at 19:37.

  4. #4
    SitePoint Wizard
    Join Date
    May 2002
    Posts
    1,343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    note that isset not seeming to work:

    <a href="location-''.strtolower(str_replace(" ", "-", $city)).''.if (isset($state)){strtolower("-".$state);}.'.php">Citystate</a>

  5. #5
    SitePoint Addict
    Join Date
    Dec 2004
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by datadriven View Post
    note that isset not seeming to work:

    <a href="location-''.strtolower(str_replace(" ", "-", $city)).''.if (isset($state)){strtolower("-".$state);}.'.php">Citystate</a>
    It does not work because you forgot "<?php" and "?>" round your PHP code. I think you meant something like this (not checked):
    PHP Code:
    <a href="location-<?php echo strtolower(str_replace(" ""-"$city)); if(isset($state)){ echo strtolower("-".$state);} echo '.php';?>">Citystate</a>

  6. #6
    SitePoint Wizard
    Join Date
    May 2002
    Posts
    1,343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that doesn't do it. with or without the php tags and echos

    this does work,
    PHP Code:
     <a href="location-'.strtolower(str_replace(" ", "-", $city)).''.(strtolower("-".$state)).'.php">Citystate</a
    the problem arises when the condition of checking on $state is added
    (remember the above is set as a variable and being called on another page)

    is there another way to eliminate the trailing hyphen when $state does not exist?

    Only other thing I can think of is to save a seperate $state values as ''-IL''
    I am using this to seperate values of states having the same city names
    There are less -- and these are minor exceptions -- so I'll do it this way.

    Much appreciate the help.
    Last edited by datadriven; May 5, 2007 at 09:20.

Bookmarks

Posting Permissions

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