Jump to content

php query asc desc


corillo181

Recommended Posts

how would i put a desc and asc order so only the next 2 and the last 2 pictures are shown?

<? 
$selectless=mysql_query("SELECT * FROM tra_gallery_photo WHERE album_id='$album' AND photo_id<'$photo_id' LIMIT 2 ")or die(mysql_error());
while($lessthumb=mysql_fetch_array($selectless)){?>
<a href="displayimage.php?album=<?=$album?>&ptd=<?=$lessthumb['photo_id']?>"><img src="<?=$lessthumb['thumb_path']?>" alt="<?=$lessthumb['thumb_path']?>" /></a>
<?
}?><?
$picture=mysql_query("SELECT thumb_path FROM tra_gallery_photo WHERE album_id='$album' AND photo_id='$photo_id'")or die(mysql_error());
$display=mysql_fetch_array($picture);
?>
<img src="<?=$display['thumb_path']?>" alt="<?=$display['image_path']?>"/>
<? 
$select=mysql_query("SELECT * FROM tra_gallery_photo WHERE album_id='$album' AND photo_id>'$photo_id' LIMIT 2")or die(mysql_error());
while($morethumb=mysql_fetch_array($select)){?>
<a href="displayimage.php?album=<?=$album?>&ptd=<?=$morethumb['photo_id']?>"><img src="<?=$morethumb['thumb_path']?>" alt="<?=$morethumb['thumb_path']?>"/></a>
<?
}?>
[code]

[/code]

Link to comment
Share on other sites

i try using a desc and if the current image is 22 it display the fallow wing  | 22 | 21 | 23 | 24 | 25 |

 

if i use asc it goes back tot he first image and displays | 1 | 2 | 23 | 24 | 25 |

 

notice that i only apply to the first 2 images before the current image.

Link to comment
Share on other sites

Say you have 50 rows in a table. To show 1 | 2 | 22 | 49 | 50

 


"SELECT * FROM table ORDER BY id ASC LIMIT 2"

and

"SELECT * FROM table ORDER BY id DESC LIMIT 2"

 

To show 20 | 21 | 22 | 23 | 24

 


$currentimage = 22;
$currentimage = $currentimage - 2;

$blah = mysql_query("SELECT * FROM table WHERE id = $currentimage ORDER BY id ASC" LIMIT 5);
while ($row = mysql_fetch_array($blah)){
echo $row["id"];
}

 

 

Link to comment
Share on other sites

no pros here to answer that question?

 

Apparently not.

 

At anyrate you are doing it wrong. I would suggest looking into Pagination, a nice tutorial is on this site.

 

The LIMIT function usage you are doing is incorrect.

 

LIMIT HOWMANY, START

 

So if you wanted to limit it to 5 images starting at 20 it would look like so:

 

LIMIT 5,20

 

http://dev.mysql.com/doc/refman/5.0/en/select.html

 

Make sense?

Link to comment
Share on other sites

that's the problem that i'm  working on a gallery the number are always going to be changing, i had it before but someone deleted my code by mistake. here is a website of what i have in mind.

 

http://www.lostraficante.com/user/displayimage.php?album=138&pos=2

 

see there is the 2 past picture and the 2 next to come and the current in the middle and they not by order of numbers they are just by > or < the middle picture.

Link to comment
Share on other sites

(SELECT * FROM tra_gallery_photo WHERE album_id=xx ORDER BY ordercol LIMIT 2) UNION (SELECT * FROM tra_gallery_photo WHERE album_id=xx ORDER BY ordercol DESC LIMIT 2) ORDER BY ordercol

 

This will give you the first two and last two rows where "ordercol" is the column by which you are ordering the results.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.