forum.coppermine-gallery.net
No Support => Modifications/Add-Ons/Hacks => Mods: Searching => Topic started by: dreams83 on March 13, 2007, 10:35:11 pm
-
Not sure if this has been posted but I couldn't find it find it in the forum so I made this using the example from http://www.w3schools.com/php/php_ajax_livesearch.asp
with a different modification to have an Ajax Live Search for the Albums on my website http://www.ehmongmusic.com
you should see the search feature in the Artist and Album Search section
file you will need to make
anycontent.php // save in root of coppermine
<?php
/*************************
Coppermine Photo Gallery
************************
Copyright (c) 2003-2006 Coppermine Dev Team
v1.1 originally written by Gregory DEMAR
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
********************************************
Coppermine version: 1.4.10
$Source$
$Revision: 3275 $
$Author: gaugau $
$Date: 2006-09-03 12:10:47 +0200 (So, 03 Sep 2006) $
**********************************************/
/**
* Coppermine Photo Gallery 1.4.10 anycontent.php
*
* This file file gets included in the index.php if you set the option in admin
* can be used to display any content from any program, it is always to be edited
* according to tastes and then used
*
* @copyright 2002,2005 Gregory DEMAR, Coppermine Dev Team
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License V2
* @package Coppermine
* @version $Id: anycontent.php 3275 2006-09-03 10:10:47Z gaugau $
*/
if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');
starttable("100%", "Anycontent");
?>
<tr><td class="tableb" >
<form>
<p>Enter a name you want to search: </p>
<input type="text" id="txt1" size="90"
onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
</td></tr>
<?php
endtable();
?>
livesearch.js // save in root of coppermine
var xmlHttp
function showResult(str)
{
if (str.length==0)
{
document.getElementById("livesearch").
innerHTML="";
document.getElementById("livesearch").
style.border="0px";
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="livesearch.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("livesearch").
innerHTML=xmlHttp.responseText;
document.getElementById("livesearch").
style.border="1px solid #A5ACB2";
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
livesearch.php // save in root of coppermine
<?php
define('IN_COPPERMINE', true);
define('LOGIN_PHP', true);
require('include/init.inc.php');
$q=$_GET["q"];
if (strlen($q) > 0)
{
$hint = "";
$searchword = explode(" ", $q);
for( $i = 0; $i < sizeof($searchword); $i++ ){
$searchword[$i] = "title LIKE '%" .$searchword[$i]. "%'";
//echo $searchword[$i] . "\n";
}
$ready = implode(" AND ", $searchword);
//echo $ready;
$result = cpg_db_query("SELECT aid, title FROM {$CONFIG['TABLE_ALBUMS']} WHERE $ready");
while ($row = mysql_fetch_array($result)) {
if ( $hint == "" ){
$hint = '<a href="thumbnails.php?album='. $row['aid'] .'">'. $row['title'] . '</a>';
}
else{
$hint = $hint . '<br /><a href="thumbnails.php?album='. $row['aid'] .'">'. $row['title'] . '</a>';
}
}
mysql_free_result($result);
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == ""){
$response = "Try short one word name and then go from there";
}
else{
$response = $hint;
}
//output the response
echo $response;
?>
plus add these codes
<style type="text/css">
#livesearch
{
margin:0px;
width:75%;
}
#txt1
{
margin:0px;
}
</style>
<script src="livesearch.js"></script>
before the </head> section of your theme template.html
It seem useful for my site anyways hope it is useful for yours too.
I have about 400+ albums and the search seems like of slow if you guys that know php can help make it faster I would appreciate that.
Don't forget to add "anycontent" to the "The content of the main page" in config
Hopefully that is everything.
PS: I don't know where to post, so I post it here instead.
-
I like this mod! There's one thing you must change I think. In your anycontent.php-page above, the page "dies" if it's in the Coppermine-gallery. You'd better use this:
anycontent.php // save in root of coppermine
<?php
define('IN_COPPERMINE', true);
require('include/init.inc.php');
pageheader('Donatie');
starttable("100%", 'Donatie', 1);
?>
<tr>
<td class="tableb" valign="top" align="left">
<form>
<p>Enter a name you want to search: </p>
<input type="text" id="txt1" size="90"
onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
</td></tr>
</td>
</tr>
<?php
endtable();
pagefooter();
ob_end_flush();
?>
-
The last post from me is only if you don't want to include anycontent.php to the index-page...., I apologize....
If you want to change it to a dropdown-box like Google Suggest, see the short instruction below.
Search in Anycontent.php for:
<div id="livesearch"></div>
change into:
<div id="livesearch" style="position:absolute;background-color: #ffffff;"></div>
An example (Dutch): http://www.busfoto.nl/foto/index.php (http://www.busfoto.nl/foto/index.php)
-
I would really like to get this script to work.
I have tried, but I'm not sure what I am doing wrong...
http://www.juliastilesonline.com/gallery
What code do I place in template.html so make the box appear? (I have added the script to the head)..
Thanks!
-
I fixed it thank you - doh...
-
Update livesearch.php
<?php
define('IN_COPPERMINE', true);
define('LOGIN_PHP', true);
require('include/init.inc.php');
$q=$_GET["q"];
if (strlen($q) > 0 )
{
$hint = "";
$searchword = explode(" ", $q);
for( $i = 0; $i < sizeof($searchword); $i++ ){
$searchword[$i] = "{$CONFIG['TABLE_ALBUMS']}.title LIKE '%" .$searchword[$i]. "%'";
//echo $searchword[$i] . "\n";
}
$ready = implode(" AND ", $searchword);
//echo $ready;
$result = cpg_db_query("SELECT {$CONFIG['TABLE_ALBUMS']}.aid, {$CONFIG['TABLE_ALBUMS']}.title, COUNT({$CONFIG['TABLE_PICTURES']}.pid) FROM {$CONFIG['TABLE_ALBUMS']}, {$CONFIG['TABLE_PICTURES']} WHERE ({$CONFIG['TABLE_PICTURES']}.aid={$CONFIG['TABLE_ALBUMS']}.aid AND $ready) GROUP BY {$CONFIG['TABLE_PICTURES']}.aid ORDER BY {$CONFIG['TABLE_ALBUMS']}.title ASC");
while ($row = mysql_fetch_array($result)) {
if ( $hint == "" ){
$hint = '<div><div style="width:85%;float:left;"><a href="thumbnails.php?album='. $row[0].'">'. $row[1]. '</a></div><div style="width:10%;float:right;text-align:right">'.$row[2].' files</div></div>';
}
else{
$hint = $hint . '<div><div style="width:85%;float:left;"><a href="thumbnails.php?album='. $row[0].'">'. $row[1]. '</a></div><div style="width:10%;float:right;text-align:right">'.$row[2].' files</div></div>';
}
}
mysql_free_result($result);
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == ""){
$response = "Try short one word name and then go from there";
}
else{
$response = $hint;
}
//output the response
echo $response;
?>
example at ehmongmusic.com (http://www.ehmongmusic.com) again
The update tell how many files are in the album.
-
Update anycontent.php
added a time delay to only search after a set millisecond instead of doing the search everytime a key is press
<?php
/*************************
Coppermine Photo Gallery
************************
Copyright (c) 2003-2006 Coppermine Dev Team
v1.1 originally written by Gregory DEMAR
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
********************************************
Coppermine version: 1.4.10
$Source$
$Revision: 3275 $
$Author: gaugau $
$Date: 2006-09-03 12:10:47 +0200 (So, 03 Sep 2006) $
**********************************************/
/**
* Coppermine Photo Gallery 1.4.10 anycontent.php
*
* This file file gets included in the index.php if you set the option in admin
* can be used to display any content from any program, it is always to be edited
* according to tastes and then used
*
* @copyright 2002,2005 Gregory DEMAR, Coppermine Dev Team
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License V2
* @package Coppermine
* @version $Id: anycontent.php 3275 2006-09-03 10:10:47Z gaugau $
*/
if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');
starttable("100%", "Anycontent");
?>
<tr><td class="tableb" >
<!--Delay when entering search -->
<script type="text/javascript">
var alertTimerId = 0;
function doSearch ( )
{
clearTimeout ( alertTimerId );
document.getElementById("txt1").style.color = "#FF0000";
alertTimerId = setTimeout ( "DoShowResult()", 500 );
}
function DoShowResult ( )
{
document.getElementById("txt1").style.color = "#000000";
clearTimeout ( alertTimerId );
showResult(document.getElementById("txt1").value);
}
</script>
<!--end delay when search -->
<form>
<p>Enter a name you want to search: </p>
<input type="text" id="txt1" size="90" onkeyup="doSearch()">
<div id="livesearch"></div>
</form>
</td></tr>
<?php
endtable();
?>
Hopefully that works
you can change this line:
alertTimerId = setTimeout ( "DoShowResult()", 500 );
change the 500 to other number is you want. Seem like 500 millisecond after a key is press and no other key is press then it will do the search.
-
hi,
its an nice search, but unfortunately only for albums.
Can i change for search to picture names and categories? or only categories?
thanks for help.
redlock
-
You can change it so that it will search for title of pictures too. But not sure how that will be like if your collection is in the thousands. It might be slow since it will query the database a lot.