1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Add to favorites script question

Discussion in 'PHP' started by qwikad.com, Sep 14, 2015.

Thread Status:
Not open for further replies.
  1. #1
    So I found this script:

    http://www.aleixcortadellas.com/main/2009/07/05/650/

    It works fine in the demo:

    http://www.aleixcortadellas.com/demos/ajaxfavourites/index.php

    For the demo purposes the author used $favid = '50'; To adapt it to a site he suggests using something like $favid= $_GET['pageid'];

    I am trying to adapt this script to my classifieds, but I am failing to dynamically add unique ids to $favid so that each ad would be added to favorites independently. The ads have a unique row['adid'], the question I have how would I use that to make the script work?

    I did this number: I added <?php $favid = $row['adid'] ?> to the ads.php file in my classifieds and then I changed $favid = '50'; to $favid = mysql_real_escape_string($_GET['favid']); in addremove.php file and it would let me store those unique favids for each ad in the database when I click on the Add to favourites button, however, in the life of me I can't figure out how to use those stored favids in favouritescript.php file where it says:

    // === IMPORTANT! Configure to work with your site ===

    $favid = 50;



    index.php
    <?php
    
    // Session
    
    session_start();
    $_SESSION['userid'] = '9'; // CONFIGURE THIS (SEE PART 4 OF THE GUIDE)
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
    <title>AJAX Favourites</title>
    <head>
    
    <!-- Favourites script -->
    <link rel="stylesheet" type="text/css" href="favscript/styles.css">
    <script src="favscript/ajax.js"></script>
    <?php require("favscript/favouritescript.php"); ?>
    <!-- END -->
    
    </head>
    
    <body>
    
    <!-- Favourites status bar -->
    <div id="status">
    <?php
    if($matches == 0){
    ?>
    <div class="red">This is NOT a favourite</div>
    <?php } else { ?>
    <div class="green">This is a favourite</div>
    <?php
    }
    ?>
    </div>
    <!-- END -->
    
    <!-- This is random page content -->
    <br />
    <b><p>Article #50</p></b><br />
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
    eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
    minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
    ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
    voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur
    sint occaecat cupidatat non proident, sunt in culpa qui officia
    deserunt mollit anim id est laborum.</p>
    <!-- END -->
    
    <!-- Favourites button -->
    <?php
    if($matches == 0){
    ?>
    <img id="button" name="button0" src="images/0.jpg" onclick="addremove('<?php echo $favid;?>')" width="150" height="35">
    <?php } else { ?>
    <img id="button" name="button1" src="images/1.jpg" onclick="addremove('<?php echo $favid;?>')" width="150" height="35"><?php
    }
    ?>
    <!-- END -->
    
    </body>
    
    </html>
    Code (markup):
    addremove.php
    <?php
    
    // Include needed files
    
    include 'mysql.php';
    
    // Connect to MySQL
    
    connectMySQL();
    
    //****** SECURITY CHECK *********
    
    session_start();
    if(isset($_SESSION['userid'])){
    $user = mysql_real_escape_string($_SESSION['userid']);
    
    //*******************************
    
    // Retrieves variables through AJAX
    
    $favid = '50';
    // $favid = mysql_real_escape_string($_GET['favid']);
    
    // Firstly, check if article is favourite or not
    
    $query = mysql_query("SELECT * FROM ajaxfavourites WHERE user=$user AND favid=$favid");
    $matches = mysql_num_rows($query);
    
    // If it is not favourited, add as favourite
    
    if($matches == '0'){
    mysql_query("INSERT INTO ajaxfavourites (user, favid) VALUES ('$user', '$favid')");
    
    echo "<div class=\"green\">This is a favourite</div>";
    }
    
    // Instead, if it is favourited, then remove from favourites
    
    if($matches != '0'){
    mysql_query("DELETE FROM ajaxfavourites WHERE user=$user AND favid=$favid");
    
    echo "<div class=\"red\">This is NOT a favourite</div>";
    }
    
    } else {
    
    // Someone tries to directly access the file!
    
    echo "Invalid session!";
    
    }
    ?>
    Code (markup):
    favouritescript.php
    <?php
    
    $user = $_SESSION['userid'];
    
    // ===  IMPORTANT! Configure to work with your site ===
    
    $favid = 50;
    
    // ====================================================
    
    
    // Include needed files
    
    include 'mysql.php';
    
    
    // Connect to MySQL
    
    connectMySQL();
    
    
    // Find out if the article is favourited
    
    $query = mysql_query("SELECT * FROM ajaxfavourites WHERE user=$user AND favid=$favid");
    $matches = mysql_num_rows($query);
    
    ?>
    Code (markup):
    ajax.js
    function addremove(favid){
    
    
    // Configure those variables as appropriate
    
    var divid = 'status';
    var url = 'favscript/addremove.php';
    
    
    // The XMLHttpRequest object
    
    var xmlHttp;
    try{
    xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
    }
    catch (e){
    try{
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
    }
    catch (e){
    try{
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e){
    alert("Your browser does not support AJAX.");
    return false;
    }
    }
    }
    
    
    // Generate timestamp for preventing IE caching the GET request
    
    fetch_unix_timestamp = function()
    {
    return parseInt(new Date().getTime().toString().substring(0, 10))
    }
    
    var timestamp = fetch_unix_timestamp();
    var nocacheurl = url+"?t="+timestamp;
    
    
    // This code sends the variables through AJAX and gets the response
    
    xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState!=4){
    document.getElementById(divid).innerHTML='<img src="images/spinner.gif">  Wait...';
    }
    if(xmlHttp.readyState==4){
    document.getElementById(divid).innerHTML=xmlHttp.responseText;
    }
    }
    xmlHttp.open("GET",nocacheurl+"&favid="+favid,true);
    xmlHttp.send(null);
    
    
    // Finally, some code for button toggle
    
    var button = document.getElementById('button');
    
    switch(button.name)
    {
    case 'button0':
      button.src = 'images/1.jpg';
      button.name = 'button1';
      break;
    case 'button1':
      button.src = 'images/0.jpg';
      button.name = 'button0';
      break;
    }
    
    }
    Code (markup):
     
    qwikad.com, Sep 14, 2015 IP
Thread Status:
Not open for further replies.