Looping Problem

Discussion in 'PHP' started by kuepper, Jul 11, 2006.

  1. #1
    Alright, I'm trying to make a loop that will categorize data into "set" numbers. Here is how it looks as it stands now:

    
    $counter = 2;
    $min = -5;
    $max = 7
    
    while($current <= $max) {
    	$set = 1;
    	$query = "SELECT id, name, score FROM scores WHERE score BETWEEN " . $last . " AND " . $current . "";
    	$result = mysql_query($query) or die('Query Failed!');
    	while($row = mysql_fetch_row($result)) {
    		$id = $row[0];
    		$name = $row[1];
    		$score = $row[2];
    		echo $name . "<br>" . $set . "<br><br>";
    	}
    	$holder = $current;
    	$current = $last + $counter;
    	$last = $holder;
    	$set++;
    }	
    
    PHP:
    For some reason, this code doesn't work. It outputs a score of "1" for all the numbers in the database, even though the scores vary from -5 to +7. Is there a problem with this loop or a better way it should be constructed?

    Thanks for any help!
     
    kuepper, Jul 11, 2006 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,824
    Likes Received:
    4,539
    Best Answers:
    123
    Trophy Points:
    665
    #2
    you have $set = 1 at the top of every loop so $set never actually gets any bigger.

    Plus the values for last and current aren't initialised

    try having
    error_reporting(E_ALL);
    PHP:
    at the top of the script while you're developing to point out errors.
     
    sarahk, Jul 11, 2006 IP
  3. kuepper

    kuepper Peon

    Messages:
    80
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Doh! Forgot about the $set inside the loop - that would do it. The other two variables are actually initialized above the code snippet here. I'll be sure to turn error reporting on too - thanks for the tip!

    The values now seem to be correct, but the "set" numbers are still ranging from 1 to 12 even though the count only increase by two six times between -5 and +7... Also, objects show up multiple times with different set numbers. I can't figure out why this is happening.

    Also: $last is initialized at -5 and $current is initialized at -3 (which is -5 plus the 2 counter)

    I echo'd it out, and it's repeating -3s, -1s, 1s, 3s, 5s, and 7s in the query... so the query will search for numbers between correct figures and between -1 and -1, -3 and -3, etc. This brings up duplicates if the score happens to be an odd number (since the counter is 2). I tried setting the $holder back to 0 every time, but that didn't make a difference.

    Any ideas?
     
    kuepper, Jul 11, 2006 IP
  4. kuepper

    kuepper Peon

    Messages:
    80
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Alright, I think I have most of this working. My only problem now is that MySQL includes both of the numbers when using the BETWEEN statement... this creates duplicates of some data. Does anyone know if there is anything you can add to the statement offhand... otherwise I'll need to change around the PHP a little.
     
    kuepper, Jul 12, 2006 IP