Hello,

I am currently designing a site that displays photography, and am exploring the different ways in which to do this.

I am trying to develop, using PHP and MYSQL, a way of having image links held in a database and then the first image in sequence to be rendered by the browser, and underneath previous and next links to cycle through the images.

Each table will only hold at a maximum 30 image links.

I'm sure that this is relatively easy to do, (I am a complete programming novice) I know enough to link to the database and display an image in the browser but cannot figure how the hell to create the previous / next links that call to the database.

PLEASE HELP!!!

<?php

$username  = 'xxxx';
$password = 'xxxx';

$link = mysql_connect('localhost', $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('dataBase', $link);
$result = mysql_query('SELECT * FROM images ORDER BY title LIMIT 1;');
if (!$result) {
    die('Could not query:' . mysql_error());
}

while ($row = mysql_fetch_object($result)) {
  
  echo '<img src="'. $row->path.'" alt="'. $row->title .'" title="'. $row->id.'" />'; // print out image
  
  ////below is my, er, attempt...
  
  ///$prev = $row - 1;

  ///next = $row + 1;

  ///echo '<a href="'. $prev->path.'" alt="'. $prev->title .'" title="'. $prev->id.'">previous</a>';
  ///echo '<a href="'. $next->path.'" alt="'. $next->title .'" title="'. $next->id.'">next</a>';
}

mysql_close($link);

?>

Recommended Answers

All 11 Replies

What you are looking for is pagination.

Google it and you will find plenty of websites to help you.

Hey,

Thanks for getting back guys,

kkeith29, don't think i've explained it brilliantly - I've looked into pagination previously but what i need here is the prev / next links (no thumbs required) to load the previous or next image within the same page.

it seems that pagination will load an amount of info to a page and the rest can be found on separate pages?

ayesha789, thanks - my code ($ is missing from var next i know) will display the first image but it wont allow any navigation? I am looking through the tutorials now.

cheers!

Instead of looking at the $row for the next, prev links have you tried using some refenace to the database index key and re fetch data adding or minus from that and re-selecting the row.

I'm not totally sure it would work but its an avenue to try.

You could try selecting the image with a primary key which is +1 from the current if you're going to the next image or -1 from the current if you're going to the previous.

Also, slightly offtopic but this seems like the situation where SQLite would fit in better than MySQL.

Bojero, did you not read my reply before typing that, lol...

Hello,

create another column for "imageid" that will be incrementing id, make a variable named, "$currentid" which will hold the value of the first "imageid" from the table. now on each press of next and previous link, just write simple code for retrieving image from the database having "$currentid" incremented or decremented as per previous or next link is clicked.

Hope you will find this helpful.

Pagination is what I would use here. It seperates the data into pages (which are part of the same page) via a _GET parameter. Pagination has nothing to do with thumbs.

<?php

$user = 'asdfasdf';
$pass = 'asasdfasdf'

//Variables
$thispage = 'index.php';
$perPage = 1; //number of images to show per page

$link = mysql_connect('localhost',$user,$pass) or die('Could Not Connect: ' . mysql_error());
mysql_select_db('dataBase',$link) or die('Could not select database: ' . mysql_error());

$page = 1;
if ( isset( $_GET['page'] ) ) {
	$page = (int) $_GET['page'];
}

$query = mysql_query("SELECT COUNT(*) FROM `images`",$link);
list( $total ) = mysql_fetch_row( $query );
$total = ceil( $total / $perPage );
$start = ( $perPage * ( $page - 1 ) );
$limit = '';
$show = true;
if ( $total > 1 ) {
	$limit = " LIMIT {$start}, {$perPage}";
}
else {
	$show = false;
}

$query = mysql_query("SELECT * FROM `images` ORDER BY `title`{$limit}");
while( $row = mysql_fetch_assoc( $query ) ) {
	echo "<img src=\"{$row['path']}\" alt=\"{$row['title']}\" title=\"{$row['id']}\" />";
}

if ( $show ) {
	$prev = $page - 1;
	$next = $page + 1;
	if ( $prev > 0 ) {
		echo "<a href=\"{$thispage}?page={$prev}\">[PREV]</a>";
	}
	if ( $next < $total ) {
		echo "<a href=\"{$thispage}?page={$next}\">[NEXT]</a>";
	}
}

?>

The code is untested and probably has some errors. I haven't had to create a pagination script by hand in awhile.

Thanks for the input all, sorry havn't been able to get back sooner have been away with work.

KKeith, your script works well, i simply altered some variables to suit and was running first time, thanks!
My design has several galleries on the same page (underneath one another) so i will try to find a way to achieve this with what you have created, had a quick try earlier but will need more attention.
One thing with your script of course is that it loads a new page on each press of prev or next link. Can this be altered to same page?

ie:

echo "<a href=\"". $PHP_SELF ."{$thispage}?page={$prev}\">[PREV]</a>";

The above doesn't seem to make any difference...??

emarshah - will look at your suggestion later on, pushed for time presently, thanks for the post tho.

Thanks again everybody. Once I get this nailed I'll post up the conclusion for others.

Cheers.

change the $thispage variable at the top of the script to the name of you page.

Member Avatar for diafol

Have you thought about using ready-made scripts? Highslide is pretty good as it's got a slideshow mode to cycle through your images. Just a thought.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.