what is wrong with this piece of code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fendi Baba

    what is wrong with this piece of code

    Hi everyone, I am new to php. I am using a piece of code which is
    testing if a certain variable is <=0. and the code is written as
    follows:

    $lc_align = 'center';
    if ($listing['products_quant ity']<=0) {
    $lc_text = tep_image_butto n('button_buy_n ow_na.gif',
    IMAGE_BUTTON_BU Y_NOW_NA) . '&nbsp;';
    } else {
    $lc_text = '<a href="' . tep_href_link(b asename($PHP_SE LF),
    tep_get_all_get _params(array(' action')) .
    'action=buy_now &products_id =' . $listing['products_id']) . '">' .
    tep_image_butto n('button_buy_n ow.gif', IMAGE_BUTTON_BU Y_NOW) .
    '</a>&nbsp;';
    }
    break;
    }



    The problem I am facing is that the this code returns all result to be
    true ie. <=0 even though most of the variable are greater than 1. Is
    the test wrong?
    Or can I change the syntax and how do I do that

    Any suggestion are welcomed.

    Thanks
  • Pedro Graca

    #2
    Re: what is wrong with this piece of code

    Fendi Baba wrote:[color=blue]
    > Hi everyone, I am new to php.[/color]

    Start *all* your scripts with

    error_reporting (E_ALL);
    ini_set('displa y_errors', '1');

    This will make php report everything that may be wrong with your code.
    [color=blue]
    > I am using a piece of code which is
    > testing if a certain variable is <=0. and the code is written as
    > follows:
    >
    > $lc_align = 'center';
    > if ($listing['products_quant ity']<=0) {[/color]
    [snip][color=blue]
    > The problem I am facing is that the this code returns all result to be
    > true ie. <=0 even though most of the variable are greater than 1. Is
    > the test wrong?
    > Or can I change the syntax and how do I do that
    >
    > Any suggestion are welcomed.[/color]


    Try

    var_dump($listi ng['products_quant ity']);

    This will tell you exactly what that variable holds.
    Any value that php cannot convert to a numeric value is taken as 0
    (zero) and, if that is your case, all comparisons will return true.

    --
    mail sent to my From: address is publicly readable at www.mailinator.net
    I probably will never read it (possibly some one else will).

    Comment

    • Theo

      #3
      Re: what is wrong with this piece of code

      effendi@epitome .com.sg (Fendi Baba) wrote in
      news:c9e0fe8b.0 411120754.5f1b1 e7a@posting.goo gle.com:
      [color=blue]
      > Hi everyone, I am new to php. I am using a piece of code which is
      > testing if a certain variable is <=0. and the code is written as
      > follows:
      >
      > $lc_align = 'center';
      > if ($listing['products_quant ity']<=0) {
      > $lc_text = tep_image_butto n('button_buy_n ow_na.gif',
      > IMAGE_BUTTON_BU Y_NOW_NA) . '&nbsp;';
      > } else {
      > $lc_text = '<a href="' . tep_href_link(b asename($PHP_SE LF),
      > tep_get_all_get _params(array(' action')) .
      > 'action=buy_now &products_id =' . $listing['products_id']) . '">' .
      > tep_image_butto n('button_buy_n ow.gif', IMAGE_BUTTON_BU Y_NOW) .
      > '</a>&nbsp;';
      > }
      > break;
      > }
      >
      >
      >
      > The problem I am facing is that the this code returns all result to be
      > true ie. <=0 even though most of the variable are greater than 1. Is
      > the test wrong?
      > Or can I change the syntax and how do I do that
      >
      > Any suggestion are welcomed.
      >
      > Thanks[/color]

      $listing['products_quant ity'] is only one variable in an array.

      Comment

      • Gary

        #4
        Re: what is wrong with this piece of code

        > if ($listing['products_quant ity']<=0) {

        1. make sure $listing is an array:
        is_array($listi ng);

        2. make sure $listing has values:
        !empty($listing );

        3. double check the values to make sure they are not empty:
        print_r($listin g);

        Comment

        • John Almberg

          #5
          Re: what is wrong with this piece of code


          On Fri, 12 Nov 2004 07:54:16 -0800, Fendi
          Baba wrote:
          [color=blue]
          > Hi everyone, I am new to php. I am using a piece of code which is
          > testing if a certain variable is <=0. and the code is written as
          > follows:
          >
          > $lc_align = 'center';
          > if ($listing['products_quant ity']<=0) {[/color]

          First, start with the assumption that there's nothing wrong with PHP.

          This leads you to the conclusion that most of the products_quanit y are <=
          0. Which may mean that the listing array is getting initialized wrong, or
          some other upstream problem.

          You can prove this to yourself with a simple print statement in the loop.
          Or, if the code is being run on a webserver or some other place that makes
          print statements inconvenient, print something to a log file.

          When in doubt, print it out.

          -- John

          Comment

          • Fendi Baba

            #6
            Re: what is wrong with this piece of code

            HI everyone. Thanks for the suggestion. I printed put the
            var_dump($listi ng['products_quant ity']) and it returned Null. And
            this at least confirms that the piece of code is behaving properly.

            I will be tracing the sql query and with your kind suggestions, I
            think i should be ok to trace the errors.

            Thanks you

            Effendi



            John Almberg <jalmberg@Ident ry.com> wrote in message news:<pan.2004. 11.13.14.37.25. 636358@Identry. com>...[color=blue]
            > On Fri, 12 Nov 2004 07:54:16 -0800, Fendi
            > Baba wrote:
            >[color=green]
            > > Hi everyone, I am new to php. I am using a piece of code which is
            > > testing if a certain variable is <=0. and the code is written as
            > > follows:
            > >
            > > $lc_align = 'center';
            > > if ($listing['products_quant ity']<=0) {[/color]
            >
            > First, start with the assumption that there's nothing wrong with PHP.
            >
            > This leads you to the conclusion that most of the products_quanit y are <=
            > 0. Which may mean that the listing array is getting initialized wrong, or
            > some other upstream problem.
            >
            > You can prove this to yourself with a simple print statement in the loop.
            > Or, if the code is being run on a webserver or some other place that makes
            > print statements inconvenient, print something to a log file.
            >
            > When in doubt, print it out.
            >
            > -- John[/color]

            Comment

            Working...