While loop isn't adding a number to my variable...

Discussion in 'PHP' started by Greenmethod, Sep 27, 2007.

  1. #1
    <?php
    require_once('db_con.php');
    $ind_inv_result = mysql_query("SELECT DISTINCT `desc`,`cat_no` FROM `inventory` WHERE `po_num` = '6000'");
    while ($ind_inv_row = mysql_fetch_array($ind_inv_result)){
    	if($main_item){
    		echo 'main set';
    		$main_item++;
    	}
    	if(!$main_item){
    		echo 'main not set';
    		$main_item=0;
    	};
    	echo $main_item;
    }?>
    	
    PHP:
    That is my code. There are 4 results from the query, so it should run the while loop 4 times. The first time it goes through, it should output:

    main not set0

    then the next 3 times it should output
    main set1-3

    correct? It is outputting mainnotset0 4 times and I don't understand why... any help is greatly appreciated!
     
    Greenmethod, Sep 27, 2007 IP
  2. sabmalik

    sabmalik Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if(!isset($main_item))

    as

    if(!$main_item) is also true for 0 value.
     
    sabmalik, Sep 27, 2007 IP
  3. sabmalik

    sabmalik Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    sorry got that the other way around

    if(isset($main_item))

    as

    if($main_item) is also FALSE for 0 value.
     
    sabmalik, Sep 27, 2007 IP
  4. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Great!! Thanks so much for your help!!
     
    Greenmethod, Sep 27, 2007 IP