guido2004…you obviously know so much… thank you for your help…
Where does that $pid come from?
This came from the php code of the gallery… pid is the photo_id (as I understand $pid is the input variable …)
What is it you want to do exactly? The user types in some text, and you want to display all the photos that have that text in the caption?
… I should have explained this better first… I would like to display the photo_caption as part of the photo link. Right now the link just shows this
viewgallery.php?cid=3&pid=277
…So instead I wanted to show the photo caption on the link. So my friend changed the code so that " now the query runs against the photo caption rather than the numeric id" and he tried to use this
$result = mysql_query( "SELECT photo_caption, photo_description, photo_filename FROM gallery_photos WHERE photo_caption='". $pid."'" );
Here is my entire code for it…
// initialization
$result_array = array();
$counter = 0;
$cid = isset($_GET['cid']) ? (int)($_GET['cid']) : 0;
//--RT:
$pid = isset($_GET['pid']) ? ($_GET['pid']) : 0; // now not an interger - now it hold the gallery title
$pid = str_replace("_", " ", $pid); // remove the _ from the url
// Category Listing
if( empty($cid) && empty($pid) )
{
$number_of_categories_in_row = 2;
$result = mysql_query( "
SELECT
c.category_id
, c.category_name
, COUNT(photo_id)
FROM
gallery_category as c
LEFT JOIN
gallery_photos as p
ON
p.photo_category = c.category_id
GROUP BY
c.category_id"
);
while( $row = mysql_fetch_array( $result ) )
{
$result_array[] = "<div class=cthumbs><a href='viewgallery.php?cid=".$row[0]."'><br/>
<img src='/photos/categoryimages/category".$row[0].".jpg' height='153px' width='230px' ><br/>
<span class=cname>".$row[1]."</span></a></div>";
}
mysql_free_result( $result );
$result_final = "<div class=cpage><div class=categories><table class=categoryview><tr>\
";
foreach($result_array as $category_link)
{
if($counter == $number_of_categories_in_row)
{
$counter = 1;
$result_final .= "\
</tr>\
<tr>\
";
}
else
$counter++;
$result_final .= "\ <td>".$category_link."</td>\
";
}
if($counter)
{
if($number_of_categories_in_row-$counter)
$result_final .= "\ <td colspan='".($number_of_categories_in_row-$counter)."'></td>\
";
$result_final .= "</tr></table></div>
<!-- .cpage --></div> ";
}
}
// Thumbnail Listing
else if( $cid && empty( $pid ) )
{
$number_of_thumbs_in_row = 3;
// If current page number, use it
// if not, set one!
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 80;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
$result = mysql_query("
SELECT
photo_id
, photo_caption
, photo_filename
, photo_category
, gallery_description
FROM
gallery_photos
WHERE
photo_category='".addslashes($cid)."'
LIMIT
$from, $max_results
");
$nr = mysql_num_rows( $result );
if( empty( $nr ) )
{
$result_final = "\ <tr><td>No Photos found</td></tr>\
";
}
else
{
while( $row = mysql_fetch_array( $result ) )
{
$result_array[]= "<a href='viewgallery.php?cid=$cid&pid=".$row[0]."'><img
src='".$images_dir."/tb_".$row[2]."' alt='".$row[1]."' /></a>";
}
$result = mysql_query( "SELECT category_name, gallery_description FROM gallery_category WHERE category_id='".addslashes($cid)."'" );
$nr = mysql_num_rows( $result );
list($category_name, $gallery_description ) = mysql_fetch_array( $result );
mysql_free_result( $result );
$result_final = "
<div class=thumbsPage>
<div class=link><a href=viewgallery.php>Albums</a><span class=arrow>>></span><a href=viewgallery.php?cid=$cid>$category_name</a><br><br></div>
<div class=des>$gallery_description<br><br></div>
<tr><td align=center valign=middle><div class=thumbs><table><tr>\
";
foreach($result_array as $thumbnail_link)
{
if($counter == $number_of_thumbs_in_row)
{
$counter = 1;
$result_final .= "\
</tr>\
<tr>\
";
}
else
$counter++;
$result_final .= "\ <td>".$thumbnail_link."</td>\
";
}
if($counter)
{
if($number_of_thumbs_in_row-$counter)
$result_final .= "\ <td colspan='".($number_of_thumbs_in_row-$counter)."'> </td>\
";
$result_final .= "</tr></table></td></tr></table>
</div>
</div>
";
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM gallery_photos WHERE photo_category=".addslashes
($cid).""),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
if ($total_pages >1)
{
// build links if more than one page
// Build Page Number Hyperlinks
$result_final .= "<div class=plinks><tr><td colspan='".$number_of_thumbs_in_row."' class='tcat'>\
";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
$result_final .= "\
<a href=\\"".$_SERVER['PHP_SELF']."?cid=$cid&page=$prev\\" title='Previous Page'><<PREVIOUS PAGE</a>";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
$result_final .= " $i";
} else {
$result_final .= "\
<a href= \\"".$_SERVER['PHP_SELF']."?cid=$cid&page=$i\\" title='Page ".$i."'class=pagenos>$i</a>";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
$result_final .= "\
<a href=\\"".$_SERVER['PHP_SELF']."?cid=$cid&page=$next\\" title='Next Page'>NEXT PAGE<span class=arrow>>></span></a>";
}
$result_final .= "\
</td></tr></div>";
}
else
{
$result_final .= "\
";
}
}
}
}
// display previous and next links if more than one photo
else if( $pid )
{
//--RT: now the query runs against the photo cation rather than the numeric id
$result = mysql_query( "SELECT photo_caption, photo_description, photo_filename FROM gallery_photos WHERE photo_caption='". $pid."'" );
list($photo_caption, $photo_description, $photo_filename) = mysql_fetch_array( $result );
$nr = mysql_num_rows( $result );
mysql_free_result( $result );
//fill pid_array with sorted pids in current category
$result = mysql_query( "SELECT photo_id FROM gallery_photos WHERE photo_category='".addslashes($cid)."' ORDER BY photo_id" );
$ct = mysql_num_rows( $result );
while ($row = mysql_fetch_array($result)) {
$pid_array[] = $row[0];
}
mysql_free_result( $result );
if( empty($nr ) )
{
$result_final = "\ <tr><td>No Photo found</td></tr>\
";
}
else
{
$result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_id='".addslashes($cid)."'" );
list($category_name) = mysql_fetch_array( $result );
mysql_free_result( $result );
$result_final = "
<div class=limagePage>
<div class=llink><a href=viewgallery.php>Albums</a><span class=arrow>>></span><a href=viewgallery.php?cid=$cid>$category_name</a></div>
";
// display previous and next links if more than one photo
if ($ct > 1) {
$key = array_search($pid, $pid_array);
$prev = $key - 1;
if ($prev < 0) $prev = $ct - 1;
$next = $key + 1;
if ($next == $ct) $next = 0;
//--RT: changed the $pid_array to replace the spaces with _
$result_final .= "<div class='prevnext'>";
$result_final .= "<span class='prev'><a href=viewgallery.php?cid=$cid&pid=". str_replace(" ", "_", $pid_array[$next]) ."><img src=photos/assets/left.png border=0 ></a></span>";
$result_final .= "<span class='next'><a href=viewgallery.php?cid=$cid&pid=". str_replace(" ", "_", $pid_array[$prev])."><img src=photos/assets/right.png border=0 ></a></span>";
$result_final .= "</div>";
}
}
$result_final .= "<div class=limage><table><tr><td><table class=image><tr>\
\ <td><a href=viewgallery.php?cid=$cid&pid=". str_replace(" ", "_", $pid_array[$next]) ."><img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' /></a>
<div class=caption>".$photo_caption."</div>
<div class='excerpt'>".$photo_description."</div>
</td>
</tr></table></td></tr></table><div class=underline></div></div>
<!-- .limagePage --></div>
";
}