SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    str_replace doesn't work on this query

    I'm using the MySQL table query below to display a list of nations, in a form format...

    PHP Code:
    $result mysql_query('select count(*) from cia_ids');
    if ((
    $result) && (mysql_result ($result 0) > 0)) {
    } else {
    die(
    'Invalid query: ' mysql_error());
    }
    {
    $List mysql_query ("SELECT CI.ID_Name, CI.ID_CIA, GWG.IDArea, GWG.Name GWName, CN.ID_Name, CN.Name 
     FROM cia_ids CI
     LEFT JOIN gw_geog GWG ON GWG.IDArea = CI.ID_Name
     LEFT JOIN cia_names CN ON CN.ID_Name = CI.ID_Name"
    ) or die (mysql_error());

    $GWName str_replace('_'' '$List["GWName"]);
    {
    $rowcounter=0;
    while (
    $row mysql_fetch_array ($List)) {
    echo <<<EOD
    <option value="{$row["GWName"]}">{$List["GWName"]}</option>\r
    EOD;
    }
    }

    It works, but I don't want to include underscores in the names that are visible. In other words, I want to display "United States," not "United_States."

    Simple enough, I just rigged the following:

    PHP Code:
    $GWName str_replace('_'' '$List["GWName"]); 
    But it doesn't work, whether I use $List, $res, $result or $row. That is, the links work, but there's no visible text. Can someone explain what I'm doing wrong?

    Thanks.

    On Edit: Yes, I'm replacing...

    PHP Code:
    <option value="{$row["GWName"]}">{$List["GWName"]}</option>\
    with...

    PHP Code:
    <option value="{$row["GWName"]}">$GWName</option>\

  2. #2
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your query replace GWG.Name GWName with REPLACE(GWG.Name , '_', ' ') AS GWName

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately, that won't work, because I use the same value for my link - and it has to include the underscores. Either I remove the underscores from the displayed value, or I add them to the invisible link. Either way, the str_replace function doesn't work.

    However, that is a neat trick you showed me; I think I'll be using it a lot in the future. Thanks.

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On second thought, I just discovered that I can cite a particular table field TWICE in the same query...

    PHP Code:
    GWG.Name GWLinkREPLACE(GWG.Name '_'' ') AS GWName
    It seems to work perfectly.

    Thanks!

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    P.S. Is there a way to replace TWO values in a query? I'd like to replace underscores with spaces AND the word "Earth" with "World." I tried the old $This'n $That array trick, as follows, but it didn't work:

    PHP Code:
    $That = array('_''Earth');
    $This = array(' ''World');
    $List mysql_query ("SELECT CI.ID_Name, CI.ID_CIA, GWG.IDArea,
     GWG.Name GWLink, REPLACE(GWG.Name , 
    $That$This) AS GWName 
     FROM cia_ids CI
     LEFT JOIN gw_geog GWG ON GWG.IDArea = CI.ID_Name"
    ) or die (mysql_error()); 

  6. #6
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pass result of one function to another:
    Code:
    REPLACE(REPLACE(GWG.Name, '_', ' '), 'Earth', 'World')

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it; thanks.


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
  •