php operators not working correctly for me. Can you spot what I am doing wrong?

Discussion in 'PHP' started by exodus, Jan 1, 2008.

  1. #1
    What am I doing wrong?

    $rs['count'] is set to "468732" in a function that is returns an array.
    
    $max_results = 25;
    $top = $rs['count'];
    echo ($top + 1)."<br />";
    $numpages = $top / 25;
    echo $top. " / ".$max_results." = ".$numpages;
    exit;
    PHP:
    Output:
    1
    469190 / 25 = 0

    I do not understand what is wrong.
     
    exodus, Jan 1, 2008 IP
  2. lv211

    lv211 Peon

    Messages:
    168
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You're script works when I take out the $top = $rs['count'];

    I think I had a similar problem in a script I wrote in perl. I used sprintf. I think it might have to do with the type of variable you are trying to pass.

    This works for me.

    check out the script here. www.wineryfinder.net/dude.php
    
    $max_results = 25;
    $top = 5;
    echo ($top + 1)."<br />";
    $numpages = $top / 25;
    echo $top. " / ".$max_results." = ".$numpages;
    Code (markup):
     
    lv211, Jan 1, 2008 IP
  3. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Count is an array. The array assigns it's as an string even though I type cast it as an int. Still shouldn't php change it to int when doing math operations? Instead for some reason it is assigned the var to be a zero instead of the number. Which is wierd, because there is no white spaces in the string and should be read by php like a integer.

    Maybe it is just something wrong with my install of php?

     
    exodus, Jan 1, 2008 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Do this
    
    $top = intval($rs['count']);
    
    PHP:
    This will process the array as an integer if it is not already.

    To check if it's an integer do this.
    
    print_r($rs);
    
    PHP:
    This will print all keys, indexes and values of the array $rs;
     
    Kaizoku, Jan 1, 2008 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    
    $top = intval($rs['count']);
    if($top > 0){
    	$max_results = 25;
    	echo ($top + 1)."<br />";
    	$numpages = $top / 25;
    	echo $top. " / ".$max_results." = ".$numpages;
    	exit;
    }
    
    PHP:
    Peace,
     
    Barti1987, Jan 1, 2008 IP
  6. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #6
    I found the error. For some reason the count var had xml stuff in it and firefox wasn't showing it. I feel stupid. I changed up the rss scrape routine to do the [1] instead of [0] and now it is grabbing only the ints.
     
    exodus, Jan 1, 2008 IP