1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

really quick PHP question

Discussion in 'PHP' started by Roze, Sep 27, 2005.

  1. #1
    Hey guys,

    I've searched php.net but I'm having problems figure out what command to use. I want php to take a field from a database and look for a character, the + (plus) sign specifically. If its there, I want to do something using an if statement, but I think I can take it from there.

    Is there a function that will let me do that, maybe one of the string commands?

    So to summarize I need to figure out

    If (plus sign exists in this database field) Show Image 1
    else Show Image 2

    Thanks for a lead in the right direction, I can usually find the answer but I'm not using the right search terms.
     
    Roze, Sep 27, 2005 IP
  2. frankm

    frankm Active Member

    Messages:
    915
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    83
    #2
    
    
    // assuming mysql connection is up
    $res = mysql_query("SELECT field FROM table WHERE id=123");
    if (!$res) die("error in mysql query: " . mysql_error() );
    
    $field = "";
    $row = mysql_fetch_row($res);
    if ($row) { 
      $field = $row[0];
    }
    mysql_free_result($res);
    
    if ( substr($field, "+") )
    {
      echo "AAA";
    }else{
      echo "BBB";
    }
    
    
    PHP:

    Also check php.net for indexof() function, might be faster
     
    frankm, Sep 27, 2005 IP
  3. michele

    michele Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The task you describe could probably done in more than one way, and would involve more than one function.

    Once PHP has extracted the field's value, ereg() might do the trick, something like:

    if (ereg("+",$value)) { do_something(); }

    The ereg can be more exact than this, it depends on what exactly you want (eg is the + always at the beginning or end of a line, etc).
     
    michele, Sep 27, 2005 IP
  4. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can probably do it straight from the database with LIKE

    SELECT * FROM yourtable WHERE field LIKE '%+%';
     
    TheHoff, Sep 27, 2005 IP
  5. Roze

    Roze Guest

    Messages:
    403
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for your help!!

    I wasn't quite smart enough to get any of your suggestions to work...however in searching I found a workable solution. I realized that the field I was looking at had the + sign, but another field (used for mod-rewrite URLs), had the word "plus" in it, so I used preg_match. php.net tells me it's slow, but this works for now!

    if (preg_match("/plus/i", "$item")) {
       echo "Got Milk?";
    } else {
       echo "No Milk!";
    }
    PHP:
    The point of this all was to show sample product images. Some models have a + at the end, designating a heavier construction. So on normal model pages, I show a regular size sample and on heavy model pages I can show a heavy size sample. In the future I could add another field in the database to say heavy or regular, but it's not that big of a deal, I think this will work. Thanks again!
     
    Roze, Sep 27, 2005 IP
  6. draculus

    draculus Peon

    Messages:
    63
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Roze

    What you were looking for is the strpos() function. It is designed to do what you want.

    if (strpos($item, 'plus') !== FALSE)
    echo 'Got Milk?';
    else
    echo 'No Milk?';
     
    draculus, Sep 27, 2005 IP
  7. Roze

    Roze Guest

    Messages:
    403
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ah ha! Thank you draculus, this seems a bit more elegant than my solution. I can usually find what I need on php.net, but somtimes I just cant figure out what to search for!
     
    Roze, Sep 28, 2005 IP