kr1zmo
kr1zmo

Reputation: 837

PHP detmine numbers in between two numbers then query

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.

Upvotes: 0

Views: 1349

Answers (3)

Bill Karwin
Bill Karwin

Reputation: 562260

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}";

Upvotes: 4

artlung
artlung

Reputation: 34013

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()

Upvotes: 3

psztucz
psztucz

Reputation: 29

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

Also, you can make it with one query..

Upvotes: -1

Related Questions