4images Forum & Community

4images Modifications / Modifikationen => Mods & Plugins (Releases & Support) => Topic started by: koikonut on May 08, 2006, 06:01:56 AM

Title: [MOD] Rating by using AJAX functions-rating without reload (used by V@no)
Post by: koikonut on May 08, 2006, 06:01:56 AM
Hello,

I've seen the rating form that uses V@no and looked a little bit for possibilities of sending DATA to a server without reloading the actual page. So I found AJAX - a combination of Javascript an XLM I think that makes these things possible.

So I found a simple script made with this that I want to present here:

http://www.konradin.net/hello.html

the hello.html sends information to the hello.php. The hello.php has the following code:

Code: [Select]
<?php
  $id 
$_REQUEST['id'];
  echo 
"rating||Thank you! You rated: $id";
?>


rating is the id of the DIV in that we see later "Thank you! You've rated:..."

So you see it is not difficult to work with to documents without reload.

The only thing I need now, is a rate.php that makes nothing else than process the image rate. And that's the step you could probably help me. Than I would write a mod that is really interesting I think becase it makes rating very comfortable and fast.

Thank you for helping,

Bye Konradin

Edit: @V@no: Is this the way you realized the rating on your page?
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: V@no on May 08, 2006, 06:58:22 AM
try this:
Code: [Select]
<?php
/**************************************************************************
 *                                                                        *
 *    4images - A Web Based Image Gallery Management System               *
 *    ----------------------------------------------------------------    *
 *                                                                        *
 *             File: rateimage.php                                        *
 *        Copyright: (C) 2002 Jan Sorgalla                                *
 *            Email: jan@4homepages.de                                    *
 *              Web: http://www.4homepages.de                             *
 *    Scriptversion: 1.7.2                                                *
 *                                                                        *
 *    Never released without support from: Nicky (http://www.nicky.net)   *
 *                                                                        *
 **************************************************************************
 *                                                                        *
 *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
 *    bedingungen (Lizenz.txt) f&#1100;r weitere Informationen.                 *
 *    ---------------------------------------------------------------     *
 *    This script is NOT freeware! Please read the Copyright Notice       *
 *    (Licence.txt) for further information.                              *
 *                                                                        *
 *************************************************************************/

$main_template '';
$nozip 1;
define('GET_CACHES'1);
define('ROOT_PATH''./');
include(
ROOT_PATH.'global.php');
require(
ROOT_PATH.'includes/sessions.php');
$user_access get_permission();
function 
update_image_rating_new($image_id$rating$image_row) {
  global 
$site_db;
  
$old_votes $image_row['image_votes'];
  
$old_rating $image_row['image_rating'];
  
$new_rating = (($old_rating $old_votes) + $rating) / ($old_votes 1);
  
$new_rating sprintf("%.2f"$new_rating);
  
$sql "UPDATE ".IMAGES_TABLE."
          SET image_votes = (
$old_votes + 1), image_rating = '$new_rating'
          WHERE image_id = 
$image_id";
  return 
$site_db->query($sql);
}
//-----------------------------------------------------
//--- Save Rating -------------------------------------
//-----------------------------------------------------
if ($action == "rateimage" && $id) {
  
$rating intval($HTTP_POST_VARS['rating']);
  
$cookie_name = (defined("COOKIE_NAME")) ? COOKIE_NAME "4images_";
  
$cookie_rated = isset($HTTP_COOKIE_VARS[$cookie_name.'rated']) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookie_name.'rated'])) : array();
  
$sql "SELECT cat_id, image_votes, image_rating
          FROM "
.IMAGES_TABLE."
          WHERE image_id = 
$id";
  
$image_row $site_db->query_firstrow($sql);
  if (
check_permission("auth_vote"$image_row['cat_id']))
  {

    if (
$rating && $rating <= MAX_RATING && $id) {
      if (!isset(
$session_info['rated_imgs'])) {
        
$session_info['rated_imgs'] = $site_sess->get_session_var("rated_imgs");
      }
      
$split_list = array();
      if (!empty(
$session_info['rated_imgs'])) {
        
$split_list explode(" "$session_info['rated_imgs']);
      }
      if (!
in_array($id$split_list) && !in_array($id$cookie_rated)) {
        if (
update_image_rating_new($id$rating$image_row))
        {
          
$session_info['rated_imgs'] .= " ".$id;
          
$session_info['rated_imgs'] = trim($session_info['rated_imgs']);
          
$site_sess->set_session_var("rated_imgs"$session_info['rated_imgs']);
          
$cookie_rated[] = $id;
          
$cookie_expire time() + 60 60 24 4;
          
setcookie($cookie_name.'rated'serialize($cookie_rated), $cookie_expireCOOKIE_PATHCOOKIE_DOMAINCOOKIE_SECURE);
          
$msg $lang['voting_success'];
        }
        else
        {
          
$msg $lang['voting_error'];
        }
      }
      else {
        
$msg $lang['already_voted'];
      }
    }
    else {
      
$msg $lang['voting_error'];
    }
  }
  else
  {
    
$msg $lang['no_permission'];
  }
}
else
{
  
$msg "Security violation";
}
die(
$msg);
?>
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: koikonut on May 09, 2006, 01:50:30 AM
Thank you Vano! I will try it.
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: koikonut on May 09, 2006, 02:41:33 AM
What do I need to show after rating the new rating and the total number of votes? These strings are saved in a function ($new_rating, $old_votes + 1) and I can't use them outside the function. So, do I have to make a new db-query or is there a way to get function-variables?

Thank you, after than I would be done.

Bye, Konradin
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: Stoleti on May 09, 2006, 02:55:37 AM
and how added this on 4images  :?: (steps) ?
Title: MOD - Rating without reload but changing dynamically the content
Post by: koikonut on May 09, 2006, 04:23:51 AM
Mein erster Mod, My first Mod

EN: This Mod will make it possible to rate a picture without that the picture reloads.
DE: Durch diesen Mod können Bilder ohne reload bewertet werden.

DEMO: http://www.konradin.net/http://www.konradin.net/galerie/img879.htm
or something that makes the same but don't know if uses the same script ist the rating form on Vanos page.
auf Vanos Page wird glaube ich durch das gleiche Prinzip gehandelt, dort kann man sich also das Ergebnis ansehen.

STEP 1: Create a rating.php in your 4images folder with the following content (Please check Gurus!)

Code: [Select]
<?php
/**************************************************************************
 *                                                                        *
 *    4images - A Web Based Image Gallery Management System               *
 *    ----------------------------------------------------------------    *
 *                                                                        *
 *             File: rateimage.php                                        *
 *        Copyright: (C) 2002 Jan Sorgalla                                *
 *            Email: jan@4homepages.de                                    *
 *              Web: http://www.4homepages.de                             *
 *    Scriptversion: 1.7.2                                                *
 *                                                                        *
 *    Never released without support from: Nicky (http://www.nicky.net)   *
 *                                                                        *
 **************************************************************************
 *                                                                        *
 *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
 *    bedingungen (Lizenz.txt) f&#1100;r weitere Informationen.                 *
 *    ---------------------------------------------------------------     *
 *    This script is NOT freeware! Please read the Copyright Notice       *
 *    (Licence.txt) for further information.                              *
 *                                                                        *
 *************************************************************************/

$rating $_REQUEST['rating'];
$id $_REQUEST['id'];
$action "rateimage";

$main_template '';
$nozip 1;
define('GET_CACHES'1);
define('ROOT_PATH''./');
include(
ROOT_PATH.'global.php');
require(
ROOT_PATH.'includes/sessions.php');
$user_access get_permission();
function 
update_image_rating_new($image_id$rating$image_row, &$new_rating, &$old_votes) {
  global 
$site_db;
  
$old_votes $image_row['image_votes'];
  
$old_rating $image_row['image_rating'];
  
$new_rating = (($old_rating $old_votes) + $rating) / ($old_votes 1);
  
$new_rating sprintf("%.2f"$new_rating);
  
$sql "UPDATE ".IMAGES_TABLE."
          SET image_votes = (
$old_votes + 1), image_rating = '$new_rating'
          WHERE image_id = 
$image_id";
  return 
$site_db->query($sql);
}
//-----------------------------------------------------
//--- Save Rating -------------------------------------
//-----------------------------------------------------
if ($id) {
  
$rating intval($rating);
  
$cookie_name = (defined("COOKIE_NAME")) ? COOKIE_NAME "4images_";
  
$cookie_rated = isset($HTTP_COOKIE_VARS[$cookie_name.'rated']) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookie_name.'rated'])) : array();
  
$sql "SELECT cat_id, image_votes, image_rating
          FROM "
.IMAGES_TABLE."
          WHERE image_id = 
$id";
  
$image_row $site_db->query_firstrow($sql);
  if (
check_permission("auth_vote"$image_row['cat_id']))
  {

    if (
$rating && $rating <= MAX_RATING && $id) {
      if (!isset(
$session_info['rated_imgs'])) {
        
$session_info['rated_imgs'] = $site_sess->get_session_var("rated_imgs");
      }
      
$split_list = array();
      if (!empty(
$session_info['rated_imgs'])) {
        
$split_list explode(" "$session_info['rated_imgs']);
      }
      if (!
in_array($id$split_list) && !in_array($id$cookie_rated)) {
        if (
update_image_rating_new($id$rating$image_row$new_rating$old_votes))
        {
          
$session_info['rated_imgs'] .= " ".$id;
          
$session_info['rated_imgs'] = trim($session_info['rated_imgs']);
          
$site_sess->set_session_var("rated_imgs"$session_info['rated_imgs']);
          
$cookie_rated[] = $id;
          
$cookie_expire time() + 60 60 24 4;
          
setcookie($cookie_name.'rated'serialize($cookie_rated), $cookie_expireCOOKIE_PATHCOOKIE_DOMAINCOOKIE_SECURE);
          
$msg $lang['voting_success'];
                  
$votes $old_votes 1;
  echo "kbdisplay||".$new_rating." (".$votes." ".$lang['votes'].")||";
        }
        else
        {
          
$msg $lang['voting_error'];
        }
      }
      else {
        
$msg $lang['already_voted'];
      }
    }
    else {
      
$msg $lang['voting_error'];
    }
  }
  else
  {
    
$msg $lang['no_permission'];
  }
}
else
{
  
$msg "Security violation";
}
die(
"kbrating||".$msg);
?>


STEP 2
Open rate_form.html and delete everything. After that insert the following:

Code: [Select]
<span id="kbrating">
<a href="javascript:rate('1');">Rate 1</a><br />
<a href="javascript:rate('2');">Rate 2</a><br />
<a href="javascript:rate('3');">Rate 3</a><br />
<a href="javascript:rate('4');">Rate 4</a><br />
<a href="javascript:rate('5');">Rate 5</a><br />
</span>

STEP 3
Open header.html of your templates and insert in the HEAD-Section the following Code:

Code: [Select]
<script type="text/javascript" language="Javascript">

function createXMLHttpRequest() {
  var ua;
  if(window.XMLHttpRequest) {
    try {
      ua = new XMLHttpRequest();
    } catch(e) {
      ua = false;
    }
  } else if(window.ActiveXObject) {
    try {
      ua = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      ua = false;
    }
  }
  return ua;
}
var req = createXMLHttpRequest();

function rate(id) {

  req.open('get', '{rating_url}&rating=' + id);
  req.onreadystatechange = handleResponse;
  req.send(null);
}

function handleResponse() {
  if(req.readyState <= 3){
    var text = '{lang_loading}';
var divid = 'kbrating';
      document.getElementById(divid).innerHTML = text;
   
  }
  if(req.readyState == 4){
    var response = req.responseText;
    var update = new Array();

    if(response.indexOf('||' != -1)) {
      update = response.split('||');
      document.getElementById(update[0]).innerHTML = update[1];
      document.getElementById(update[2]).innerHTML = update[3];

    }
  }
}
</script>



STEP 4

in details.html of your templates replace

Code: [Select]
{image_rating} ({image_votes} {lang_votes})
with

Code: [Select]
<span id="kbdisplay">{image_rating} ({image_votes} {lang_votes})</span>

STEP 5

open page_header.php in folder /includes/ and look for:

Code: [Select]
  "direction" => $lang['direction']
replace with:

Code: [Select]
  "direction" => $lang['direction'],
  "lang_loading" => $lang['loading']

add to your language files in your language:

Code: [Select]
$lang['loading'] = "wird geladen..."; or
Code: [Select]
$lang['loading'] = "loading...";

STEP 6

add anywhere in the details.php (or in the page_header.php if you want to use the rate form in other pages too) before the template-printout:

Code: [Select]
$site_template->register_vars("rating_url", ROOT_PATH."rating.php?id=".$image_id);
Done.

That should work - without support for rating scalas or Rating Stars but surely there`s somebody that helps me to integrate this too in a few seconds.

You can modify "Rate 1", "Rate 2", etc... with everything you want - stars, another text, etc.

Bye Konradin

Thx too Vano for piece of code

Edit 9. May, 23.50 -> radom.php to rating.php
-> Add {rating_url} registering to details.php
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: V@no on May 09, 2006, 06:48:13 AM
I see you got it ;)

two things though:
1) its not "random.php" but "rating.php" ;)
2) any links to 4images based files should be parsed by $site_sess->url() function, otherwise sessions can mess up.
So, instead of
Code: [Select]
  req.open('get', 'rating.php?rating=' + id + '&id={image_id}');use:
Code: [Select]
  req.open('get', '{rating_url}&rating=' + id);
And then in details.php register {rating_url} tag:
Code: [Select]
$site_template->register_vars("rating_url", ROOT_PATH."rating.php?id=".$image_id);
3) I see you added "echo" in the code, but it ment to load everything you want it to return, into $msg variable, which at the end of the script would output the result.

And then register {rating_url} tag in details.php:
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: koikonut on May 10, 2006, 12:18:21 AM

3) I see you added "echo" in the code, but it ment to load everything you want it to return, into $msg variable, which at the end of the script would output the result.


I added another echo because this one updates the display of the actual/new total votes - And I want to update this only in case of success of the vote.

Thank you for helping me so much with the realisation of the MOd - Are you using the same feature on your site?

Do you have such a site like the rating.php for lightbox-adding, too? I'll try it myself, first.. I think that would be useful too, add lightbox-pictures fast.

Bye Konradin
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: koikonut on May 10, 2006, 01:35:56 AM
Hello, I have a problem with the JavaScript:

I wanted to change it into:

Quote
function createXMLHttpRequest() {
  var ua;
  if(window.XMLHttpRequest) {
    try {
      ua = new XMLHttpRequest();
    } catch(e) {
      ua = false;
    }
  } else if(window.ActiveXObject) {
    try {
      ua = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      ua = false;
    }
  }
  return ua;
}
var req = createXMLHttpRequest();

function rate(id) {

  req.open('get', '{rating_url}&rating=' + id);
  req.onreadystatechange = handleResponse('kbrating');
  req.send(null);
}

function handleResponse(divid) {
  if(req.readyState <= 3){
    var text = '{lang_loading}';
    var divid = 'kbrating';
    document.getElementById(divid).innerHTML = text;
   
  }
  if(req.readyState == 4){
    var response = req.responseText;
    var update = new Array();

    if(response.indexOf('||' != -1)) {
      update = response.split('||');
      document.getElementById(update[0]).innerHTML = update[1];
      document.getElementById(update[2]).innerHTML = update[3];

    }
  }
}

But now the script only updates to "loading..." but not anymore after successful voting to "Thank you.." - but the vote works (after reload you can see it). What is the error of my JavaScript? I changed it for using the script with different divs (handleResponse(divid))

Thank You for helping, Bye Konradin
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: impss on May 12, 2006, 06:34:32 PM
Did you get this to work ?

Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: koikonut on May 13, 2006, 02:52:03 AM
Did you get this to work ?



What thing? The mod I mentiones above with Steps (rating without reloading) works perfectly. Now Lightbox-adding works perfectly too but I didn't public it yet. I found another solution for the javascript error I mentioned in my last topic.

Bye, Konradin
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: V@no on May 13, 2006, 04:52:31 AM
for the lightbox, there is already a mod exists, yet, it doesnt use any AJAX.
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: koikonut on May 14, 2006, 09:43:15 AM
for the lightbox, there is already a mod exists, yet, it doesnt use any AJAX.

And what's its name?
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: mawenzi on May 14, 2006, 07:28:44 PM
[Mod] Add/Remove image to/from lightbox without page refresh (http://www.4homepages.de/forum/index.php?topic=5321.msg22504#msg22504)
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: live@ct on May 15, 2006, 08:54:32 AM
I try this and its working, now a simple problem...

when you already voted, this display the msgs that says that you akready voted, but it display a java script error:

document.getElementByld(...) that its null  or itsnot and object

thanks for your help
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: koikonut on May 16, 2006, 02:21:42 AM
Hello!

The problem is, that in that case there is no point in update the Rating-Display because it didn't change. So, there are no attributes for the second element-ID. Know I made a solution for this javascript-error, but it is not very elegant, but works (updating the same lement two times..):

Code: [Select]
  $votes = $old_votes + 1;
  echo "kbdisplay||".rating_pic($new_rating)." ".$new_rating." (".$votes." ".$lang['votes'].")||";
        }
        else
        {
          $msg = $lang['voting_error'];
        }
      }
      else {
        $msg = $lang['already_voted'];
      }
    }
    else {
      $msg = $lang['voting_error'];
    }
  }
  else
  {
    $msg = $lang['no_permission'];
  }
}
else
{
  $msg = "Security violation";
}
die("kbrating||".$msg);

replace with:

Code: [Select]
  $votes = $old_votes + 1;
  $msg1 = "kbdisplay||".rating_pic($new_rating)." ".$new_rating." (".$votes." ".$lang['votes'].")||";
        }
        else
        {
  $msg1 = "kbrating||-||";
          $msg = $lang['voting_error'];
        }
      }
      else {
        $msg1 = "kbrating||-||";
        $msg = $lang['already_voted'];
      }
    }
    else {
      $msg1 = "kbrating||-||";
      $msg = $lang['voting_error'];
    }
  }
  else
  {
    $msg1 = "kbrating||-||";
    $msg = $lang['no_permission'];
  }
}
else
{
  $msg1 = "kbrating||-||";
  $msg = "Security violation";
}
echo $msg1;
die("kbrating||".$msg);

bye Konradin
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: live@ct on May 16, 2006, 03:31:40 AM
mm no, its not working, nothing happend in the record of votes...
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: koikonut on May 17, 2006, 05:55:58 AM
mm no, its not working, nothing happend in the record of votes...

Todavía el mismo error? En mi pagina también me dió cuenta el error que describiste y con esa modificación solvé el problema  :roll: Por favor escribe cual es el error.

Gracias y saludos, Konradin
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: live@ct on May 17, 2006, 08:34:48 AM
pues has de cuenta q, ya modifique lo que pusite, y al momento de calificar no me dice nada de q ya se ah calificado, me dice q ya se ah puntuado la imagen y no graba el voto, al parecer eso funciona con las ya votadas y creia q funcionaba, pero al momento de q quiero votar las otras imagenes q no eh votado pasa lo mismo, dice gracias por puntuar pero el voto no se guarda y muestra algo totalmente diferente, digamos habia 32 votos, le doy calificar y aparecen 26 votos  :? luego le dor refresh y todo queda como al inicio 32

Regrese como estaba originalmente y ya funciona de nuevo, pero me di cuenta q hay veces q otras imagenes q ya has votado te dice gracias por puntuar pero no califica la imagen pero las q no has calificado si funciona bien
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: koikonut on May 18, 2006, 07:27:27 PM
pues has de cuenta q, ya modifique lo que pusite, y al momento de calificar no me dice nada de q ya se ah calificado, me dice q ya se ah puntuado la imagen y no graba el voto, al parecer eso funciona con las ya votadas y creia q funcionaba, pero al momento de q quiero votar las otras imagenes q no eh votado pasa lo mismo, dice gracias por puntuar pero el voto no se guarda y muestra algo totalmente diferente, digamos habia 32 votos, le doy calificar y aparecen 26 votos  :? luego le dor refresh y todo queda como al inicio 32

Regrese como estaba originalmente y ya funciona de nuevo, pero me di cuenta q hay veces q otras imagenes q ya has votado te dice gracias por puntuar pero no califica la imagen pero las q no has calificado si funciona bien

Cuando yo estoy probando el script en tu pagina con la calificación todo funciona muy bien. También la calculación de los votos después. No sé cual es el problema...
Porque pusiste void('0')? Es necesario para el javascript?

Quizás tienes que eliminar tus cookies para que funcione bien. Para que no se vea el rating si ya has calificado necesitas el script "Votes saved in Db" creo.

Chao
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: live@ct on May 19, 2006, 05:04:56 AM
Si funciona muy bien todo pero con el codigo original
pero cuando una ya esta votada sale el error de java script q comente al inicio, tu has probado q funciona porq regrese al codigo inicial q funcionaba, ya que no puedo dejar algo q no funcione bien,
ahorita volvi a intentar cambiar el codigo q pusiste y al parecer al inicio funciona, pero cuando le doy a otra q ya habia calificado que queda en: Loading... y el mismo error javascript
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: koikonut on May 19, 2006, 09:07:40 AM
hmm, no tengo idea porque podría ser...

Aca te voy a escribir otra vez el codigo completo del documento rating.php. Mi header.html no va a servir mucho porque ya lo modifiqué para funciones similares con "mi caja".

Code: [Select]
<?php
/**************************************************************************
 *                                                                        *
 *    4images - A Web Based Image Gallery Management System               *
 *    ----------------------------------------------------------------    *
 *                                                                        *
 *             File: rateimage.php                                        *
 *        Copyright: (C) 2002 Jan Sorgalla                                *
 *            Email: jan@4homepages.de                                    *
 *              Web: http://www.4homepages.de                             *
 *    Scriptversion: 1.7.2                                                *
 *                                                                        *
 *    Never released without support from: Nicky (http://www.nicky.net)   *
 *                                                                        *
 **************************************************************************
 *                                                                        *
 *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
 *    bedingungen (Lizenz.txt) f&#1100;r weitere Informationen.                 *
 *    ---------------------------------------------------------------     *
 *    This script is NOT freeware! Please read the Copyright Notice       *
 *    (Licence.txt) for further information.                              *
 *                                                                        *
 *************************************************************************/

$rating $_REQUEST['rating'];
$id $_REQUEST['id'];

$main_template '';
$nozip 1;
define('GET_CACHES'1);
define('ROOT_PATH''./');
include(
ROOT_PATH.'global.php');
require(
ROOT_PATH.'includes/sessions.php');
$user_access get_permission();
function 
update_image_rating_new($image_id$rating$image_row, &$new_rating, &$old_votes) {
  global 
$site_db;
  
$old_votes $image_row['image_votes'];
  
$old_rating $image_row['image_rating'];
  
$new_rating = (($old_rating $old_votes) + $rating) / ($old_votes 1);
  
$new_rating sprintf("%.2f"$new_rating);
  
$sql "UPDATE ".IMAGES_TABLE."
          SET image_votes = (
$old_votes + 1), image_rating = '$new_rating'
          WHERE image_id = 
$image_id";
  return 
$site_db->query($sql);
}
//-----------------------------------------------------
//--- Save Rating -------------------------------------
//-----------------------------------------------------
if ($id) {
  
$rating intval($rating);
  
$cookie_name = (defined("COOKIE_NAME")) ? COOKIE_NAME "4images_";
  
$cookie_rated = isset($HTTP_COOKIE_VARS[$cookie_name.'rated']) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookie_name.'rated'])) : array();
  
$sql "SELECT cat_id, image_votes, image_rating
          FROM "
.IMAGES_TABLE."
          WHERE image_id = 
$id";
  
$image_row $site_db->query_firstrow($sql);
  if (
check_permission("auth_vote"$image_row['cat_id']))
  {

    if (
$rating && $rating <= MAX_RATING && $id) {
      if (!isset(
$session_info['rated_imgs'])) {
        
$session_info['rated_imgs'] = $site_sess->get_session_var("rated_imgs");
      }
      
$split_list = array();
      if (!empty(
$session_info['rated_imgs'])) {
        
$split_list explode(" "$session_info['rated_imgs']);
      }
      if (!
in_array($id$split_list) && !in_array($id$cookie_rated)) {
        if (
update_image_rating_new($id$rating$image_row$new_rating$old_votes))
        {
          
$session_info['rated_imgs'] .= " ".$id;
          
$session_info['rated_imgs'] = trim($session_info['rated_imgs']);
          
$site_sess->set_session_var("rated_imgs"$session_info['rated_imgs']);
          
$cookie_rated[] = $id;
          
$cookie_expire time() + 60 60 24 4;
          
setcookie($cookie_name.'rated'serialize($cookie_rated), $cookie_expireCOOKIE_PATHCOOKIE_DOMAINCOOKIE_SECURE);
          
$msg $lang['voting_success'];
  $votes $old_votes 1;
  $msg1 "kbdisplay||".rating_pic($new_rating)." ".$new_rating." (".$votes." ".$lang['votes'].")||";
        }
        else
        {
  $msg1 "kbrating||-||";
          
$msg $lang['voting_error'];
        }
      }
      else {
        
$msg1 "kbrating||-||";
        
$msg $lang['already_voted'];
      }
    }
    else {
      
$msg1 "kbrating||-||";
      
$msg $lang['voting_error'];
    }
  }
  else
  {
    
$msg1 "kbrating||-||";
    
$msg $lang['no_permission'];
  }
}
else
{
  
$msg1 "kbrating||-||";
  
$msg "Security violation";
}
echo 
$msg1;
die(
"kbrating||".$msg);
?>

Ojala que puedes solver el problema :-) Considera que deben estar elementos con id="kbdisplay" y id="kbrating".

Buenas Noches, Ko
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: lotus69 on July 22, 2006, 04:14:07 PM
for those who like me, have issue with special characters,
simply insert after $user_access = get_permission();
Code: [Select]
header("Content-type: text/css; charset=".$lang['charset']."");

$lang['charset'] must be set with "iso-8859-1" in your language main.php file
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: BartAfterDark on July 25, 2006, 12:11:03 PM
Could you please post the complete script (need the thing with the stars) :/
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: Ch*yeuk on July 30, 2006, 01:37:15 AM
Excellent! I've installed it and it's working. Though it did take me two tries .. =P
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: fabio on August 19, 2006, 03:28:32 PM
i don`t know what i`m doing wrong but i have the problem discussed earlier
after voting i ge the loading message and no thank you although i`ve applied the changes konradin suggested in the rating.php



Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: BartAfterDark on August 19, 2006, 08:52:56 PM
i don`t know what i`m doing wrong but i have the problem discussed earlier
after voting i ge the loading message and no thank you although i`ve applied the changes konradin suggested in the rating.php





Did you remember to add this
Code: [Select]
$site_template->register_vars("rating_url", ROOT_PATH."rating.php?id=".$image_id); to page_header.php or details.php?
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: BartAfterDark on August 19, 2006, 10:07:36 PM
Is there anyway to make the star rating update with the the new rating (when this mod is being used http://www.4homepages.de/forum/index.php?topic=923.0 )
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: fabio on August 20, 2006, 09:46:28 AM
yes i have :(
i added   {rating_url} the the path seeme to be registered correctly
also when i`ve already voted i get the corresponding message
so i think it has something to do with rating .php (??)


somene posted this link http://komodomedia.com/blog/index.php/2005/08/24/creating-a-star-rater-using-css/
i`ve tried it and works

in rate_form.html
inside the span tags replace the code with this
<ul class='star-rating'>
  <li><a href="javascript:rate('1');" title='Rate this 1 star out of 5' class='one-star'>1</a></li>
  <li><a href='"javascript:rate('2');" title='Rate this 2 stars out of 5' class='two-stars'>2</a></li>
  <li><a href="javascript:rate('3');" title='Rate this 3 stars out of 5' class='three-stars'>3</a></li>
  <li><a href="javascript:rate('4');" title='Rate this 4 stars out of 5' class='four-stars'>4</a></li>
  <li><a href="javascript:rate('5');" title='Rate this 5 stars out of 5' class='five-stars'>5</a></li>
</ul>
      

and add in style.css

/*             styles for the star rater                */
.star-rating{
   list-style:none;
   margin: 0px;
   padding:0px;
   width: 100px;
   height: 20px;
   position: relative;
   background: url(star_rating.gif) top left repeat-x;      
}
.star-rating li{
   padding:0px;
   margin:0px;
   /*\*/
   float: left;
   /* */
}
.star-rating li a{
   display:block;
   width:20px;
   height: 20px;
   text-decoration: none;
   text-indent: -9000px;
   z-index: 20;
   position: absolute;
   padding: 0px;
}
.star-rating li a:hover{
   background: url(star_rating.gif) left bottom;
   z-index: 1;
   left: 0px;
}
.star-rating a.one-star{
   left: 0px;
}
.star-rating a.one-star:hover{
   width:20px;
}
.star-rating a.two-stars{
   left:20px;
}
.star-rating a.two-stars:hover{
   width: 40px;
}
.star-rating a.three-stars:hover{
   width: 60px;
}
.star-rating a.three-stars{
   left: 40px;
}
.star-rating a.four-stars{
   left: 60px;
}   
.star-rating a.four-stars:hover{
   width: 80px;
}
.star-rating a.five-stars{
   left: 80px;
}
.star-rating a.five-stars:hover{
   width: 100px;
}
   
don`t foregt to change the paths of the images    
ps it`s for a 20X20p star image
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: leh00 on August 20, 2006, 08:35:58 PM
Hi fabio,

how does your javascript looks like? You did not post it...

You should have something like this in : (sample not working...)

Code: [Select]
function rate() {
  id=document.all.fieldname.value;
  var text = 'loading...';
  document.getElementById('objectname').innerHTML = text;

  req.open('GET', '{rating_url}&rating=' + id, true);
   req.onreadystatechange = handleResponse;
  req.send(null);
}


Regards

Leh00
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: fabio on August 20, 2006, 10:58:27 PM
you mean the js in header.html ?

i just c&p was is posted here
my javascript knowledge is just above 0 so i avoid messing with things like this!

anyay that`s the coed i added
<script type="text/javascript" language="Javascript">

function createXMLHttpRequest() {
  var ua;
  if(window.XMLHttpRequest) {
    try {
      ua = new XMLHttpRequest();
    } catch(e) {
      ua = false;
    }
  } else if(window.ActiveXObject) {
    try {
      ua = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      ua = false;
    }
  }
  return ua;
}
var req = createXMLHttpRequest();

function rate(id) {

  req.open('get', '{rating_url}&rating=' + id);
  req.onreadystatechange = handleResponse;
  req.send(null);
}

function handleResponse() {
  if(req.readyState <= 3){
    var text = '{lang_loading}';
var divid = 'kbrating';
      document.getElementById(divid).innerHTML = text;
   
  }
  if(req.readyState == 4){
    var response = req.responseText;
    var update = new Array();

    if(response.indexOf('||' != -1)) {
      update = response.split('||');
      document.getElementById(update[0]).innerHTML = update[1];
      document.getElementById(update[2]).innerHTML = update[3];

    }
  }
}
</script>
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: Azibi on September 06, 2006, 12:05:46 AM
Hi. Can you to explain me, why it is't working at my page? it only shows "0.00 (0 Stimme(n))"

http://www.azibi.de/details.php?image_id=2405


Gruß/Greetings Azibi
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: Stoleti on October 01, 2006, 09:19:09 PM
Use the rate form in another pages ??? like where ?

can use it at member_profile ?
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: 2ndzenith on October 21, 2006, 02:32:17 AM
I have installed this mod using the css stars. The stars show up and work, votes are able to be counted. But it doesn't display loading or thanks for voting after you vote. The star graphics just remain there, unable to vote again on that image for that user. 

My gallery is here: http://empiresportscar.org/4images


Within Explorer I get an error in my header.html file on the following line:

document.getElementById(update[2]).innerHTML = update[3];

any help with this would be great!

Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: BartAfterDark on February 19, 2007, 02:04:06 PM
2ndzenith I'm going to post the code from my site (that should work)
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: troopers on April 04, 2007, 10:01:16 PM
thx, works perfect :D

(nachm 10.mal -g-)
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: SoftDux on May 05, 2007, 05:41:58 PM
I'm just adding myself to this thread to be notified of updates
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: jrey on May 07, 2007, 11:51:34 PM
Im using this mod and works very good, but I have a litte problem. The problem is that   $lang['already_voted'] and $lang['voting_success']  are showed only in spanish ( I have two languages in my gallery, spanish and english ). I have both messages in english and spanish ( in the main.php file of the lang dir ), but when you rate you see the messages always in spanish ( if you are in the english version its the same, you see these message in spanish ), its a mistake...
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: impss on June 13, 2007, 04:23:32 PM
I would like to add this to my thumbnail_bit.html with a modified rateform (for stars) so that my members can rate directly from the thumbnails.

Everything seems to be working on the detail page, but when i rate from the thumbnails i get a "Security violation" error.

Can anybody help me?

thanks

 :)
Title: Re: [MOD] Rating by using AJAX functions-rating without reload (used by V@no)
Post by: mawenzi on June 14, 2007, 04:19:17 PM
... thanks Konradin for this rating-MOD with AJAX-function , it works perfectly ( with little modifications ) on my site ...
... I've mixed this MOD with Kurt's [MOD] Mouseover/hover rateform (http://www.4homepages.de/forum/index.php?topic=17518.0) ... thanks for this Kurt ...
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: darvid on June 18, 2007, 12:53:39 PM
yes i have :(
i added   {rating_url} the the path seeme to be registered correctly
also when i`ve already voted i get the corresponding message
so i think it has something to do with rating .php (??)


somene posted this link http://komodomedia.com/blog/index.php/2005/08/24/creating-a-star-rater-using-css/
i`ve tried it and works

in rate_form.html
inside the span tags replace the code with this
<ul class='star-rating'>
  <li><a href="javascript:rate('1');" title='Rate this 1 star out of 5' class='one-star'>1</a></li>
  <li><a href='"javascript:rate('2');" title='Rate this 2 stars out of 5' class='two-stars'>2</a></li>
  <li><a href="javascript:rate('3');" title='Rate this 3 stars out of 5' class='three-stars'>3</a></li>
  <li><a href="javascript:rate('4');" title='Rate this 4 stars out of 5' class='four-stars'>4</a></li>
  <li><a href="javascript:rate('5');" title='Rate this 5 stars out of 5' class='five-stars'>5</a></li>
</ul>
      

and add in style.css

/*             styles for the star rater                */
.star-rating{
   list-style:none;
   margin: 0px;
   padding:0px;
   width: 100px;
   height: 20px;
   position: relative;
   background: url(star_rating.gif) top left repeat-x;      
}
.star-rating li{
   padding:0px;
   margin:0px;
   /*\*/
   float: left;
   /* */
}
.star-rating li a{
   display:block;
   width:20px;
   height: 20px;
   text-decoration: none;
   text-indent: -9000px;
   z-index: 20;
   position: absolute;
   padding: 0px;
}
.star-rating li a:hover{
   background: url(star_rating.gif) left bottom;
   z-index: 1;
   left: 0px;
}
.star-rating a.one-star{
   left: 0px;
}
.star-rating a.one-star:hover{
   width:20px;
}
.star-rating a.two-stars{
   left:20px;
}
.star-rating a.two-stars:hover{
   width: 40px;
}
.star-rating a.three-stars:hover{
   width: 60px;
}
.star-rating a.three-stars{
   left: 40px;
}
.star-rating a.four-stars{
   left: 60px;
}   
.star-rating a.four-stars:hover{
   width: 80px;
}
.star-rating a.five-stars{
   left: 80px;
}
.star-rating a.five-stars:hover{
   width: 100px;
}
   
don`t foregt to change the paths of the images    
ps it`s for a 20X20p star image

there is still one mistake in the code in rate_form.html:

<span id="kbrating">
<ul class='star-rating'>
  <li><a href="javascript:rate('1');" title='Rate this 1 star out of 5' class='one-star'>1</a></li>
  <li><a href='"javascript:rate('2');" title='Rate this 2 stars out of 5' class='two-stars'>2</a></li>
  <li><a href="javascript:rate('3');" title='Rate this 3 stars out of 5' class='three-stars'>3</a></li>
  <li><a href="javascript:rate('4');" title='Rate this 4 stars out of 5' class='four-stars'>4</a></li>
  <li><a href="javascript:rate('5');" title='Rate this 5 stars out of 5' class='five-stars'>5</a></li>
</ul>
</span>

correct code:

<span id="kbrating">
<ul class='star-rating'>
  <li><a href="javascript:rate('1');" title='Rate this 1 star out of 5' class='one-star'>1</a></li>
  <li><a href="javascript:rate('2');" title='Rate this 2 stars out of 5' class='two-stars'>2</a></li>
  <li><a href="javascript:rate('3');" title='Rate this 3 stars out of 5' class='three-stars'>3</a></li>
  <li><a href="javascript:rate('4');" title='Rate this 4 stars out of 5' class='four-stars'>4</a></li>
  <li><a href="javascript:rate('5');" title='Rate this 5 stars out of 5' class='five-stars'>5</a></li>
</ul>
</span>
Title: Re: Rating by using AJAX functions-rating without reload (probably used by V@no)
Post by: impss on June 18, 2007, 09:00:49 PM
I would like to add this to my thumbnail_bit.html with a modified rateform (for stars) so that my members can rate directly from the thumbnails.

Everything seems to be working on the detail page, but when i rate from the thumbnails i get a "Security violation" error.

Can anybody help me?

thanks

 :)

I would really like my members to be able to vote from thumbnails without page refresh.
I really cant figure out why when i try to , it gives me a Security violation error.

If someone could help me with this it would be greatly appreciated.
Title: Re: [MOD] Rating by using AJAX functions-rating without reload (used by V@no)
Post by: KurtW on June 20, 2007, 07:59:13 PM
Hi konradin,

thanks for your great Mod.
I change your code a little bit and mixed it with codes from 2 other mods from me  :wink:

=> http://www.4homepages.de/forum/index.php?topic=17829.0 (http://www.4homepages.de/forum/index.php?topic=17829.0)

Thanks again....

cu
Kurt
Title: Re: [MOD] Rating by using AJAX functions-rating without reload (used by V@no)
Post by: ahmed_badawy on January 29, 2009, 11:21:10 PM
thanx for mod

but i want reload image rate

http://www.4homepages.de/forum/index.php?topic=923.0