Advanced search  

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Pages: [1]   Go Down

Author Topic: Album List PHP and AJAX Live Search  (Read 41949 times)

0 Members and 1 Guest are viewing this topic.

dreams83

  • Contributor
  • Coppermine regular visitor
  • ***
  • Offline Offline
  • Posts: 54
Album List PHP and AJAX Live Search
« 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

Code: [Select]
<?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

Code: [Select]
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

Code: [Select]
<?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

Code: [Select]
<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.
« Last Edit: March 14, 2007, 07:23:21 am by GauGau »
Logged

Mauritsvink.net

  • Coppermine newbie
  • Offline Offline
  • Posts: 5
Re: Album List PHP and AJAX Live Search
« Reply #1 on: March 18, 2007, 10:05:25 am »

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

Code: [Select]
<?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();
?>
Logged

Mauritsvink.net

  • Coppermine newbie
  • Offline Offline
  • Posts: 5
Re: Album List PHP and AJAX Live Search
« Reply #2 on: March 18, 2007, 10:44:38 am »

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:
Code: [Select]
<div id="livesearch"></div>
change into:
Code: [Select]
<div id="livesearch" style="position:absolute;background-color: #ffffff;"></div>
An example (Dutch): http://www.busfoto.nl/foto/index.php
Logged

jesskajess

  • Coppermine novice
  • *
  • Offline Offline
  • Posts: 30
Re: Album List PHP and AJAX Live Search
« Reply #3 on: April 18, 2007, 06:11:56 pm »

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!

Logged

jesskajess

  • Coppermine novice
  • *
  • Offline Offline
  • Posts: 30
Re: Album List PHP and AJAX Live Search
« Reply #4 on: April 18, 2007, 06:28:35 pm »

I fixed it thank you - doh...
Logged

dreams83

  • Contributor
  • Coppermine regular visitor
  • ***
  • Offline Offline
  • Posts: 54
Re: Album List PHP and AJAX Live Search
« Reply #5 on: April 22, 2007, 09:33:47 pm »

Update livesearch.php

Code: [Select]
<?php
define
('IN_COPPERMINE'true);
define('LOGIN_PHP'true);
require(
'include/init.inc.php');

$q=$_GET["q"];

if (strlen($q) > )
{
$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 again

The update tell how many files are in the album.
Logged

dreams83

  • Contributor
  • Coppermine regular visitor
  • ***
  • Offline Offline
  • Posts: 54
Re: Album List PHP and AJAX Live Search
« Reply #6 on: April 24, 2007, 10:12:11 am »

Update anycontent.php

added a time delay to only search after a set millisecond instead of doing the search everytime a key is press

Code: [Select]
<?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.
Logged

redlock

  • Coppermine novice
  • *
  • Offline Offline
  • Posts: 25
Re: Album List PHP and AJAX Live Search
« Reply #7 on: May 08, 2007, 01:57:39 pm »

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


Logged

dreams83

  • Contributor
  • Coppermine regular visitor
  • ***
  • Offline Offline
  • Posts: 54
Re: Album List PHP and AJAX Live Search
« Reply #8 on: May 11, 2007, 12:14:53 am »

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.
Logged
Pages: [1]   Go Up
 

Page created in 0.024 seconds with 19 queries.