0

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.

3 Answers 3

4

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}";
Sign up to request clarification or add additional context in comments.

Comments

3

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

3 Comments

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
Updated the answer to include $curid
Take a look at php.net/manual/en/function.mysql-fetch-assoc.php - great example.
-1
foreach (range(240, 251) as $n) {
    mysql_query("SELECT * FROM wall WHERE id='".$n."'")
}

Also, you can make it with one query..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.