next prev image

C++ JAVA
Post Reply
torreto
Нов
Нов
Posts: 58
Joined: Thu Apr 19, 2007 9:57 am

next prev image

Post by torreto » Thu Aug 02, 2012 10:20 am

здравейте
имам следния код с който показвам следваща и предишна снимка

Code: Select all

$id=$_GET['image'];

$select = mysql_query("SELECT `id` FROM `photos` order by id desc limit 1") or die (mysql_error());
$totalimages = mysql_fetch_array($select) or die (mysql_error()); 

$select2 = mysql_query("SELECT `id` FROM `photos` order by id asc limit 1") or die (mysql_error());
$totalimages2 = mysql_fetch_array($select2) or die (mysql_error()); 


if ($id == $totalimages[id]){
$pv = $id-1;
$pvpage = "<a href=?image=$pv> > </a>";

&#125; elseif &#40;$id == $totalimages2&#91;id&#93;&#41;&#123;
$nv = $id+1;
$nextpage = "<a href=?image=$nv> < </a>";

&#125; else &#123;
$nv = $id+1;
$nextpage = "<a href=?image=$nv> < </a>";
$pv = $id-1;
$pvpage = "<a href=?image=$pv> > </a>";
&#125;
по много нубски начин съм го направил... имам id на снимки 2,3,4,7,8,13. Примерно съм на снимка с id 3 и като дам на следваща или предишна снимка си работи, но ако съм примерно на 7 и дам на предишна снимка(id=6) ми дава грешка. Искам да попитам как ще стане така че да прескача несъществуващите id-та

User avatar
dakata__92
Web-tourist
Web-tourist
Posts: 3319
Joined: Tue Aug 02, 2011 7:24 pm
Answers: 126

Post by dakata__92 » Thu Aug 02, 2012 10:28 am

http://web-tourist.net/login/login/view.php?st=740

Ето този урок ще ти помогне много :)

User avatar
dakata__92
Web-tourist
Web-tourist
Posts: 3319
Joined: Tue Aug 02, 2011 7:24 pm
Answers: 126

Re: next prev image

Post by dakata__92 » Thu Aug 02, 2012 10:35 am

Пробвай и така :

Code: Select all

function VerifyImageID&#40;$id&#41; 
&#123;        
	$id_chek = mysql_query&#40;"SELECT * FROM `photos` WHERE `id`='&#123;$id&#125;';"&#41;or die&#40;mysql_error&#40;&#41;&#41;;        
	return mysql_num_rows&#40;$id_chek&#41;? 1 &#58; 0; 
&#125;
$id=$_GET&#91;'image'&#93;;
$select = mysql_query&#40;"SELECT `id` FROM `photos` order by id desc limit 1"&#41; or die &#40;mysql_error&#40;&#41;&#41;;
$totalimages = mysql_fetch_array&#40;$select&#41; or die &#40;mysql_error&#40;&#41;&#41;; 

$select2 = mysql_query&#40;"SELECT `id` FROM `photos` order by id asc limit 1"&#41; or die &#40;mysql_error&#40;&#41;&#41;;
$totalimages2 = mysql_fetch_array&#40;$select2&#41; or die &#40;mysql_error&#40;&#41;&#41;; 


if &#40;$id == $totalimages&#91;id&#93; &&  VerifyImageID&#40;$id&#41; == 1&#41;&#123;
$pv = $id-1;
$pvpage = "<a href=?image=$pv> > </a>";

&#125; elseif &#40;$id == $totalimages2&#91;id&#93; &&  VerifyImageID&#40;$id&#41; == 1&#41;&#123;
$nv = $id+1;
$nextpage = "<a href=?image=$nv> < </a>";

&#125; else &#123;
$nv = $id+1;
$nextpage = "<a href=?image=$nv> < </a>";
$pv = $id-1;
$pvpage = "<a href=?image=$pv> > </a>";
&#125;

torreto
Нов
Нов
Posts: 58
Joined: Thu Apr 19, 2007 9:57 am

Post by torreto » Thu Aug 02, 2012 2:12 pm

за жалост и двете не помагат :/

torreto
Нов
Нов
Posts: 58
Joined: Thu Apr 19, 2007 9:57 am

Post by torreto » Sat Aug 25, 2012 3:59 pm

някой има ли други идеи?

User avatar
Genius_m
Турист
Турист
Posts: 502
Joined: Sat Mar 06, 2010 2:08 pm
Location: localhost

Post by Genius_m » Sat Aug 25, 2012 4:42 pm

Пробвай така:
[php]
$next = mysql_fetch_array(mysql_query("SELECT id FROM photos WHERE id > '$id' ORDER BY id ASC LIMIT 1"));
$prev = mysql_fetch_array(mysql_query("SELECT id FROM photos WHERE id < '$id' ORDER BY id DESC LIMIT 1"));
[/php]

[html]
<a href ='?image=<?=$prev[id];?>'>Предишна снимка</a>
<a href ='?image=<?=$next[id];?>'>Следваща снимка</a>
[/html]

Дано съм помогнал! :)

TryMe
Турист
Турист
Posts: 365
Joined: Wed Jul 04, 2012 7:33 pm
Answers: 22

Post by TryMe » Sat Aug 25, 2012 7:31 pm

Най-лесно е да набухаш снимките в един масив, вместо да правиш постоянни заявки към базата данни. Така спокойно ще можеш да ползваш функциите next(), current(), prev(), reset() и т.н.

Post Reply