Jump to content

Php photo gallery!! NEED HELP!!


gleamthecube

Recommended Posts

This isn't completely a php question but here it is:

 

So I can load images onto a page with no problem, but I'm dealing with a lot of images. Right now my php code loads certain images into an unordered list on a page.

 

function getPhotos($select_album) {
$q = 'SELECT id, src, tn_src FROM photo WHERE album_id="' . $select_album . '/" ORDER BY id desc';
$result = mysql_query($q) or die("Error with query");

if($result) {
	while($row = mysql_fetch_object($result)) {
		$tn_src = $row->tn_src;
		$src = $row->src;
		$id = $row->id;

                     print '<li>
        <a href="backend/' . $src . '">
            <img src="backend/' . $tn_src . '" />
        </a>
    </li>';

		print "\n";
	}
}
}

 

That works fine.

My question is, is if there is a way with php to only view, say, 10 items at a time and be able to navigate through pages of them?

 

I've thought about doing something with jquery, but all the premade gallery programs I find won't work.

I'm working with a limited amount of space. (900px X 1200px)

So what I would need is just thumbnails of 10 or 20 pictures and then when clicked they launch a lightbox.

 

Is there anything out there that can do that? Or how would I write the code to organize a lot of thumbnails into a multiple page gallery?

Link to comment
Share on other sites

you can do that with your mysql query

 

to pull the newest (0-10)

 

SELECT id, src, tn_src FROM photo WHERE album_id="whatever" ORDER BY id DESC LIMIT 10

 

to get the next (10-20)

 

SELECT id, src, tn_src FROM photo WHERE album_id="whatever" ORDER BY id DESC LIMIT 10, 10

 

and to get the next (20-30) after that:

 

SELECT id, src, tn_src FROM photo WHERE album_id="whatever" ORDER BY id DESC LIMIT 20, 10

 

you use php and some if statements or switch statements to change your query depending on what page you're on.  you can use $_GET variables to keep track of what page you're on

 

$StartingPosition =  ( $_GET['Page'] - 1 ) * 10;

$Limit = $StartingPosition . ', 10';

 

Then just add $Limit onto the end of your query

 

$query = $YourOldQuery . $Limit;

 

just send the page number as a $_GET variable. simple, yea?

Link to comment
Share on other sites

The thing is, is that there are multiple galleries with different number of pictures. Also, pictures can be added or deleted from each gallery, so they're always going to change in size. Is there some sort of way to know the number of items you query so that I could use that as some kind of condition for how many times you should do that?

Link to comment
Share on other sites

The thing is, is that there are multiple galleries with different number of pictures. Also, pictures can be added or deleted from each gallery, so they're always going to change in size. Is there some sort of way to know the number of items you query so that I could use that as some kind of condition for how many times you should do that?

lets say you click to goto the last page, which only has 5 pictures.  the query won't error out, it'll just pull the last 5 pictures and return them to you.

Link to comment
Share on other sites

You can use below given code to paging

 

function pagingPN($sql, $page, $limit, $getvars, $class)
{
	if ($page == "")
		$page = 1;
	if ($limit == 0)
		$limit = $this->limit;
	$tsql = $sql;
	$result = mysql_query($tsql) or die("Error: ".mysql_errno().":- ".mysql_error());
	$total = mysql_num_rows($result);
	$totnumpages = ceil($total/$limit);
	if ($offset < 0)
		$offset = 0;
	else          
		$offset = ($page - 1) * $limit;
	$sql = $sql. "  limit $offset, $limit"; 
	$res = $this->select_row($sql);
	$serial_no = ($page - 1) * $limit;

	if ($total > 0)
	{
		$link = "<font face='verdana' size='1'>Page: <strong>".$page."</strong> of <strong>".$totnumpages."</strong>  ";			
		if ($page > 1)
		{
			$link .= "<a href=".$_SERVER['PHP_SELF']."?page=1$getvars class='".$class."' title='Jump to First Page'><<</a> | ";
			$prev = $page - 1;
			$link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$prev."$getvars class='".$class."' title='Goto Previous Page'>Previous</a><span class='".$class."'> | </span>";
		}
		else
		{
			$link .= "<span class='".$class."' title='Jump to First Page'><<</span> | <span class='".$class."' title='Goto Previous Page'>Previous | </span>";
		}
		if ($page < $totnumpages)
		{
			$next = $page + 1;
			$link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$next."$getvars class='".$class."' title='Goto Next Page'>Next</a> | ";
			$link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$totnumpages."$getvars class='".$class."' title='Jump to Last Page'>>></a>";
		}
		else
		{
			$link .= "<span class='".$class."' title='Goto Next Page'>Next</span> | <span class='".$class."' title='Jump to Last Page'>>></span>";
		}
	}		
	$retarr["sql"] = $sql;
	$retarr["records"] = $res;
	$retarr["serial_no"] = $_no;
	$retarr["link"] = $link;
	return $retarr;
}

 

 

It will give you << | Previous | Next | >> pagination.

Parameters of functions are

$sql = sql query

$page = page number(just use $_REQUEST['page'])

$limit = number of record on one page

$getvars = query string variables, if you want to pass anything

$class = CSS for the links

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.