empty variable if/then

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mikey P

    empty variable if/then

    Hi there, i'm trying to figure out if there is an empty variable in a
    search to return an alternative result...

    like:

    $query = mysql_query("SE LECT * FROM db WHERE city='$city2' AND
    state='$state2' ORDER BY title");
    while($row = mysql_fetch_ass oc($query)){
    if (empty($url)) {
    echo($row['title'] . '<br>');
    } else {
    echo
    echo('<b><a href="' . $row['url'] . '>' . $row['title'] . '</a></b>');
    }
    }

    from what it looks like it seems to be searching and saying if there is
    any result that is empty in $url return this result. I guess i need to
    know how to say.. if there is an empty $url on this row return this
    result if there isn't then return the other. :) hope that makes some
    sort of sense... Sorry i'm still fairly new at this.

  • clercmedia

    #2
    Re: empty variable if/then

    try this :

    $query = "SELECT * FROM db WHERE city='$city2' AND state='$state2'
    ORDER BY title";
    if(!$res = mysql_query($qu ery))
    {
    print("No Result");
    }
    else
    {
    while($row = mysql_fetch_ass oc($query))
    {
    if (!isset($url) && empty($url))
    {
    echo($row['title'] . '<br>');
    }
    else
    {
    echo('<b><a href="' . $row['url'] . '>' . $row['title'] .
    '</a></b>');
    }
    }
    }

    im not shure if I cleary understand the question because im french ...
    so .. tell me if that help you !!
    by the way u should always valid the $mysql_query before doing anything
    :) !

    Comment

    • Jerry Stuckle

      #3
      Re: empty variable if/then

      Mikey P wrote:[color=blue]
      > Hi there, i'm trying to figure out if there is an empty variable in a
      > search to return an alternative result...
      >
      > like:
      >
      > $query = mysql_query("SE LECT * FROM db WHERE city='$city2' AND
      > state='$state2' ORDER BY title");
      > while($row = mysql_fetch_ass oc($query)){
      > if (empty($url)) {
      > echo($row['title'] . '<br>');
      > } else {
      > echo
      > echo('<b><a href="' . $row['url'] . '>' . $row['title'] . '</a></b>');
      > }
      > }
      >
      > from what it looks like it seems to be searching and saying if there is
      > any result that is empty in $url return this result. I guess i need to
      > know how to say.. if there is an empty $url on this row return this
      > result if there isn't then return the other. :) hope that makes some
      > sort of sense... Sorry i'm still fairly new at this.
      >[/color]

      Mikey,

      It depends on the contents of the column 'url'. It could be null, or it
      could be a zero length string ("").

      If you insert null values when there is no URL, then you should use

      if(is_null($row['url']) ...

      If it's an empty string, it could still be padded with blanks, so you
      can use something like

      if (empty(trim($ro w['url'])) ...


      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      Working...