Forum Moderators: coopster

Message Too Old, No Replies

Mysql if conditions? Loops conditions?

         

chopin2256

11:16 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Are if statements and looping possible in a sql command line?

Well, actually I want to Select only 2 unique "ids" from a table to be shown per page....but this seems impossible to do using mysql. But I want to verify.

If the ids are listed like this:

ID
1
1
1
2
2
3
3
3
4
4
5

Can I use a mysql query to only show 1 and 2 on one page, 3 and 4 on another page, 5 and 6 on another page, and so on?

mcibor

8:47 am on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The ifs are available in mysql, but why do you need them? Wouldn't it be better to use:

$page_number = 2;
$id1 = $page_number * 2 - 1;//3
$id2 = $page_number * 2;//4
$sql = "SELECT * FROM table WHERE id='$id1' OR id='$id2'";

I think this is simpler way, and still you need to refresh the page on transition from page 1 to page 2...

Best regards
Michal Cibor

jd01

10:04 am on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use something like this:

// set the results to 2 or however many results you would like.
$results_per_page=2;

// if the page is not set, or for some reason is empty, make it one.
if(!isset($page) or $page='') $page = 1;

// DB results start at 0 and our pages start at 1, so take one away to get the first DB result.
$start=(($page-1)*$results_per_page);

// LIMIT = 'startfromthisrow','getthismanyresults'
$query = "SELECT * FROM ".$db_table." WHERE variable='".$variable."' LIMIT ".$start.",".$results_per_page."";

This will scroll through results and you can keep adding pages by increasing the results in the DB and the page number.

Then you can link like this:
echo "<a href=\"/yourdirectory/yourfile.php?page=".($page+1)."\">Your Link</a>";
echo "<a href=\"/yourdirectory/yourfile.php?page=".($page-1)."\">Your Link</a>";

Hope this helps.

Justin