tags:

views:

35

answers:

3

This should be simple but I can't figure it out


<?php
$testid = 240;
$curid = 251;

$cal = $curid - $testid;
echo $cal;

?>

I want to determine the numbers in between two other numbers so for this example it detects there are 11 numbers in between the $testid and $curid, but i dont need it to do that.

I need it to literally figure the numbers in between $curid - $testid which in this example would be 241, 242, 243... all the way to 251 then i need to create a loop with those numbers and do a query below with each one

$cal = $curid - $testid;
mysql_query("SELECT * FROM wall WHERE id='".$cal."'")

// and for each number mysql should out put each data field with those numbers.

Thanks again, love you guys.

A: 
foreach (range(240, 251) as $n) {
    mysql_query("SELECT * FROM wall WHERE id='".$n."'")
}

Also, you can make it with one query..

psztucz
Im going to try this thanks for the quick response =)
kr1zmo
+3  A: 

This assumes that $testid is smaller than $curid:

$testid = 240;
$curid = 251;
$numbers = range(($testid + 1), ($curid));
$query = 'SELECT * FROM wall WHERE id IN (' . implode(', ', $numbers). ')';

If I print $query; I get:

SELECT * FROM wall WHERE id IN
    (241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251)

So just do your query:

mysql_query($query);

More about range() and implode()

artlung
Thanks again for your response, im probably going to end up combing these two answers because i forgot to mention i need to include all the numbers in between plus the current one which in this example is 251 and yours does that
kr1zmo
Updated the answer to include `$curid`
artlung
Not doing aim, sorry.
artlung
Take a look at http://php.net/manual/en/function.mysql-fetch-assoc.php - great example.
artlung
+3  A: 

You could make a list of numbers with range() as @artlung suggestions, but it's possible that the list is very long, which would not be an efficient query.

Why not use BETWEEN?

$testid = 240;
$curid = 251;

$query = "SELECT * FROM wall WHERE id BETWEEN {$testid}+1 AND {$curid}";
Bill Karwin