next prev links in the image gallery !!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nita
Forum Commoner
Posts: 39
Joined: Wed May 02, 2007 5:49 am

next prev links in the image gallery !!

Post by nita »

Hi im having a big problem to set up next prev links proparly.

Im doing standard image gallery, once in selected category, browser is printing out all thumbs and links to their bigger version. (by id of record) - i manage to display a big version.
but i dont know how to do links to next and prev id in selected category.

so i have following queries:

1 - is displaing big image.

Code: Select all

$result=mysql_query("SELECT * FROM morgal WHERE categ='$categ' AND id='$id' LIMIT 1") or die(mysql_error());
 	while($row=mysql_fetch_array($result))
 	{
	echo "<img src='http://www.nita-on-line.com/morgal/photos/";
		echo $row['image'];
		echo ".jpg' border='0'>";
	}
2 - is the query selecting all records from selected category

Code: Select all

$res1=mysql_query("SELECT * FROM morgal WHERE cat='$categ' ORDER BY id") or die(mysql_error());
and on the base of this query i would like to display nex prev links (to next prev id)

i was traying to count records, and then display links to prev next image but this method is not covering with the id of record so wrong images are being displayed. (standad paging result).

to check the script out go to:
http://www.nita-on-line.com/morgal

Im relly stuck on this and would like to move on with it, if some one can give me some idea how to get these links right.


Thanka a lot for your help!!

Nita
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
this
http://nita-on-line.com/morgal/index.php?categ=01&id=58
and that
http://nita-on-line.com/morgal/index.ph ... =01&page=1

is not the same.

The second link (your next link) shows pages, whereas the first shows ids. page=1 and id=1 are for sure not the same.


djot
-
nita
Forum Commoner
Posts: 39
Joined: Wed May 02, 2007 5:49 am

Post by nita »

thanks for your respond

well thats right....

i will post you some more code so you can see how it is done now and at the moment,
and i know is wrong but i seems to have no other idea how is this can be done.
so its all wrong at this point (its temporary trail)

Code: Select all

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}
$max_results = 1;
$from = (($page * $max_results) - $max_results); 
$categ=$_GET['categ'];
$id=$_GET['id'];

	$result=mysql_query("SELECT * FROM morgal WHERE cat='$categ' AND id='$id' ORDER BY image LIMIT $from, $max_results") or die(mysql_error());
 	while($row=mysql_fetch_array($result))
 	{
	echo "<img src='http://www.nita-on-line.com/morgal/photos/";
		echo $row['image'];
		echo ".jpg' border='0'>";
	}
echo "</td></tr>
<tr>
<td align='center'>";

$res1=mysql_query("SELECT * FROM morgal WHERE cat='$categ' ORDER BY id") or die(mysql_error());
$total_results = mysql_num_rows($res1);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

if($page > 1){
    $prev = ($page - 1);
    echo "
	<td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?categ=$categ&page=$prev\">Previous</a></td>
  	<td width='2'></td>
 ";
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?categ=$categ&page=$next\">Next</a><td>";
}

well then when you look at category - thumbs are displayed and links created (code for links below)

Code: Select all

echo "<A href='index.php?categ=$cat&id=$id'><img src='http://www.nita-on-line.com/morgal/photos/";
	echo "$thumb";
	echo ".jpg' border='0'></a></td>
and i know it doesnt make sense, couse paging result is apllaying to variable $page not &id.

What i now for sure now also that the way links are created by $next = ($id + 1) it not going to work
couse i have gaps in database (couse by deleting some of the records.)

there must be some solution to print out the prev next links basesd on id of the records in the category.

Nita.
nita
Forum Commoner
Posts: 39
Joined: Wed May 02, 2007 5:49 am

Post by nita »

hi i come up with some idea and im closer to the final result
but still have a problem when:

1. what if i have gaps in database (so i cant use "+ 1" and "- 1" with &id).
2. i have to add some if statment to code (so link previous is showing in case where there is a previous id in the category) - same think for next. Couse at the moment i have this links showing up in any case.

updated code of when you look at selected id (big photo + next prev links)

Code: Select all

else if(isset($_GET['categ'])||isset($_GET['id'])) // when looking at selected id
{

$categ=$_GET['categ'];
$id=$_GET['id'];

	$result=mysql_query("SELECT * FROM morgal WHERE cat='$categ' AND id='$id' ORDER BY image LIMIT 1") or die(mysql_error());
 	while($row=mysql_fetch_array($result))
 	{
	echo "<img src='http://www.nita-on-line.com/morgal/photos/";
		echo $row['image'];
		echo ".jpg' border='0'>";
	}

$prev = $_GET['id'] - 1;
$next = $_GET['id'] + 1;

echo "<a href=\"index.php?categ=$categ&id=$prev\">Previous Image</a>";
echo "<a href=\"index.php?categ=$categ&id=$next\">Next Image</a>";

}
i need some help on this one

tkanks a lot in advance ...

nita
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
You don't have any plan of PHP or SQL or even this Gallery, do you?

djot
-
nita
Forum Commoner
Posts: 39
Joined: Wed May 02, 2007 5:49 am

Post by nita »

hi
yes i do, so....

so in mysql there is 2 tables:

1) morgalcat (list of categories)
with columns - id, cat (category), name
example:
1 - 01 - israel
2 - 02 - israel 2

2) morgal (list of photos)
with columns - id, cat (category), image (big image), thumb (thumb

example:

1 - 01 - image01 - thimage01
2 - 01 - image02 - thimage02
3 - 01 - image03 - thimage03 an so on

so both tables are conected by cat column...

for PHP:

morgalcat is a base to print out categories..

Code: Select all

$result = mysql_query("SELECT * FROM morgalcat ORDER BY name") or die(mysql_error());
	while($row = mysql_fetch_array( $result )) 
	{
	echo "<span class='style2'><A href='index.php?cat=$row[cat]'>$row[name]</a></span><br>";
	}
one link pressed
browser displying new page with thumbs of images and links to their bigger version...

Code: Select all

$res = mysql_query("SELECT id, page, cat, thumb FROM morgal WHERE cat='$cat' ORDER BY image");
..
..
..

echo "<A href='index.php?categ=$cat&id=$id'><img src='http://www.nita-on-line.com/morgal/photos/";
	echo "$thumb";
	echo ".jpg' border='0'></a>
the code for what is happening one you press on this link you have above.....


you can see it working at
http://www.nita-on-line.com/morgal

as i mention before - code is working as long as ids from selected grups are all +1 or -1 from each other,
problems will start when i decide to add or delete some id from selected category, that will couse the gap in ids
.

and also i need to add if statment for next prev links. so when i look at 1st pic form selected category is not showing me link to previus id (which is there) but it dosnt belong to choosen category.


some ideas

thx

nita
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, if you ever delete something, there will be a gap. So... don't count the ids like that. Use your SQL statement to handle it for you.

Think of it like this...

Code: Select all

$page = (int)$_GET['page'] - 1;
$limitPerPage = 10;

// Get images to display
$result = mysql_query("SELECT `imageName` FROM `imageTable` LIMIT " . (($page * $limitPerPage) + 1) . "," . $limitPerPage . ";");

// Display images
while(mysql_fetch_object($result))
{
    echo '<img src="' . $result->imageName . '" />';
}
Handle the actual selection of the images to display through your SQL rather than your PHP.

Of course, you'll have to alter this code to fit your categories, table names, and such, but hopefully you can see where I'm coming from with this.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

nita wrote:and also i need to add if statment for next prev links. so when i look at 1st pic form selected category is not showing me link to previus id (which is there) but it dosnt belong to choosen category.
Didn't even see this part. Sorry. :-p

This, really, should be the easy part if everything works okay. Using the variables from the snippet I just posted:

Code: Select all

if($page > 1)
{
    // Show "prev" link
}


// The "next" link takes a bit more thought

$result = mysql_fetch_array(mysql_query("SELECT COUNT(`imageName`) FROM `imageTable`"));
$limit = $result[0];

if(($page * $limitPerPage) < $limit)
{
    // Show "next" link
}
I think. :?:
nita
Forum Commoner
Posts: 39
Joined: Wed May 02, 2007 5:49 am

Post by nita »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thx for you advice....

that looks to me as a example of pagination of the results.
i can manage to do that:
count the record in the category, print them out on by one with the links, that work correct too.

The problem is starting form the page with the thumbnails (thumb +link), link is passing categ + id
and then displaying big pic of selected cat and id..

Then on the same page (i did try it before) i use pagination script  but then $id and $ page are different..
so i will not work proparly.


i decide to give more code so you can have a clear view

when category is chosen and thumbnails printed

Code: Select all

if(isset($_GET['cat'])) // when looking at selected category of recipes
{

	$res = mysql_query("SELECT id, page, cat, thumb FROM morgal WHERE cat='$cat' ORDER BY image");

	echo "<A href='index.php?categ=$cat&id=$id'><img src='http://www.nita-on-line.com/morgal/photos/";
	echo "$thumb";
	echo ".jpg' border='0'></a>
}
thums/links then passing categ and id to

Code: Select all

else if(isset($_GET['categ'])||isset($_GET['id'])) // when looking at selected id
{
$categ=$_GET['categ'];
$id=$_GET['id'];

	$result=mysql_query("SELECT * FROM morgal WHERE cat='$categ' AND id='$id' ORDER BY image LIMIT 1") or die(mysql_error());
 	while($row=mysql_fetch_array($result))
 	{
	echo "<img src='http://www.nita-on-line.com/morgal/photos/";
		echo $row['image'];
		echo ".jpg' border='0'>";
	}

$prev = $_GET['id'] - 1;
$next = $_GET['id'] + 1;

echo "<a href=\"index.php?categ=$categ&id=$prev\">Previous Image</a>";
echo "<a href=\"index.php?categ=$categ&id=$next\">Next Image</a>";

}

im not so sure what to do.
that must be some syntax talling to the browser to display next id in selected category, with no matter what is the value of id. Well, im hopping so...

thanks

nita


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, I wasn't trying to fix what you had... I was suggesting a different approach. Using pages allows you to have, between the "next" and "prev" links, all of the pages numbered.

But if it doesn't work for you, then you're free to disregard my posts.
nita
Forum Commoner
Posts: 39
Joined: Wed May 02, 2007 5:49 am

Post by nita »

Hi superdezign
Oh im actually very happy for your post and i working on it now,
i'm not so sure how to apply your sugesstion, thats all..
Should i try to fit it in my current version and how to get it connected with the links from thumbnails.

see script in action at:
http://www.nita-on-line.com/morgal

hope you can give some advice

Im sorry if my post gave you disregard feeling, i didn mean this one at all.

Thanks again


nita
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Oh! I think I misinterpreted what you were after completely.

Do you want prev and next links for each individual picture?
nita
Forum Commoner
Posts: 39
Joined: Wed May 02, 2007 5:49 am

Post by nita »

yes that right,
maybe i didnt make myself clear enough !!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Lol, maybe not. :-p

That's a toughie, considering that you'll want to use their actual ids in order to create permalinks.

Okay, what you'll want to do is leave as much of it up to SQL as possible, since there can be gaps in your id numbers.

This is how I'd find the next id:

Code: Select all

SELECT `id` FROM `imageTable` WHERE `id`>$currentId ORDER BY `id` ASC LIMIT 1,1
And the previous id:

Code: Select all

SELECT `id` FROM `imageTable` WHERE `id`<$currentId ORDER BY `id` DESC LIMIT 1,1

I think that might work. I didn't even think of it until I started writing, so I could be wrong. Give them a try anyway.
nita
Forum Commoner
Posts: 39
Joined: Wed May 02, 2007 5:49 am

Post by nita »

that is it !!! superdezign

this is what i was looking for everything works perfectly fine,

thanks a lot

nita
Post Reply