SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    variable works outside if statement, but not inside

    I can echo a variable nicely thus:

    PHP Code:
     echo ($rowPresenter["biography"]); 
    However, when I put it inside an if statement, it doesn't display.

    PHP Code:
     if (empty($s)) { 
    echo(
    $rowPresenter["biography"]);
         } 
    Any clues why?
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  2. #2
    Non-Member
    Join Date
    May 2005
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    first make sure $s is empty second try just echo $rowPresenter["biography"]; dont need tha brackets!

  3. #3
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha! I find that the variable $s had content, as it was set to:
    PHP Code:
     $s explode(' ',$s); 
    , i.e. a space between the two apostrophes.

    I took the space out and
    PHP Code:
    $rowPresenter["biography"]); 
    displays fine.

    However, without a space between the explode apostrophes, I get the following error message:
    Warning: explode(): Empty delimiter. in /home/kripalu/public_html/test/presenter_v11.php on line 19
    Anyone know of a work around?
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  4. #4
    Non-Member
    Join Date
    May 2005
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are you exploding without a delimiter, whats the point

  5. #5
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you just set $s to nothing like:
    Code:
    $s='';

  6. #6
    SitePoint Addict
    Join Date
    Apr 2005
    Location
    San Diego, CA
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you don't know what $s is going to be so make sure it can be exploded:

    PHP Code:
    if (strpos(' '$s) !== false)
        
    $s explode(' ',$s); 
    Now since you may have forced $s to be an array check it for contents:
    PHP Code:
    if (count($s))
        echo(
    $rowPresenter["biography"]); 
    I study speed waiting. I can wait an entire hour in 10 minutes.

  7. #7
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are you exploding without a delimiter, whats the point
    I was simply trying to eliminate content in $s, so the "if" statement would return true and would echo
    PHP Code:
    $rowPresenter["biography"]); 
    . The empty delimiter error was an inintended consequence, indicating that trying to use explode without a delimiter was a bad idea
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  8. #8
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you just set $s to nothing
    Yes, that would allow the variable
    PHP Code:
    $rowPresenter["biography"]); 
    to print. But I am trying to print
    PHP Code:
    $rowPresenter["biography"]); 
    one of two ways, depending on whether the page is requested with a search term in the url with up to three terms

    $queryString = explode("/", $_SERVER["PATH_INFO"]);
    $presenter_id = addslashes($queryString[1]);
    $s = addslashes($queryString[2]);
    $presenter_id = intval($presenter_id);
    $s = explode(' ',$s);

    The delimiter of the 2nd explode() is the space between the search terms.

    The two ways to print are:
    1) if there are no search terms in the url, just echo what's in the database:
    PHP Code:
    if (empty($s)) { 
    //echo ($biography);
    echo($rowPresenter["biography"]);
         } 
    2) otherwise, highlight the search terms using the function str_highlight
    PHP Code:
    else { 
    $string $biography
    $search $s
    echo 
    str_highlight($string$searchSTR_HIGHLIGHT_WHOLEWD); 
         } 
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  9. #9
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHPCamp.com
    Sounds like you don't know what $s is going to be
    Yes. You are right.

    I tried your suggested code with some success.
    PHP Code:
     if (strpos(' '$s) !== false
        
    $s explode(' ',$s); 
     
    if (
    count($s)) {
    echo(
    $rowPresenter["biography"]);
         } else { 
    $string $biography
    $search $s
    echo 
    str_highlight($string$searchSTR_HIGHLIGHT_WHOLEWD); 
         } 
    It does print the variable
    PHP Code:
    $rowPresenter["biography"]); 
    , but I still come up withe the empty delimiter message on the line
    PHP Code:
    if (strpos(' '$s) !== false
    even though there is a space for the delimiter.

    Here's the url for the page without a search term:
    http://test.kripalu.org/presenter_v11/5/

    Here's one with three search terms:
    http://test.kripalu.org/presenter_v1...ied%20motional
    No error message, but str_highlight is not doing it's highlighting job.

    Here is a url for some stand alone code where I got everything working before I tried to incorporate the code snippets into a real page.
    http://test.kripalu.org/presenter.php

    The one piece that has been added to the real code is getting the search terms from the url:
    PHP Code:
     $queryString explode("/"$_SERVER["PATH_INFO"]);
    $presenter_id addslashes($queryString[1]);
    $s addslashes($queryString[2]);
    $presenter_id intval($presenter_id);
    $s explode(' ',$s); 
    Paul C.
    ClickBasics
    http://www.clickbasics.com


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
  •