Hi,

I have some code similar to that below. What I want to do is add a simple voting system that does not allow multiple votes for the same image, but the user is still able to vote for other images.

So far i've got it to only allow one vote by setting the cookie containing the link to the image. Yet when i try to vote for another image it won't let me vote even though the links are different.

Can anyone help me figure this one, i've been on it for a few hours now and i'm running out of hair to remove, please, I really don't wanna start on the pubes!

if (isset($_COOKIE['vote'])) 
  {
    foreach ($_COOKIE['vote'] as $name => $value) 
      {
        if($name==$link)
           {
           echo "already exists;"
           }
         else
           {
         $sql = mysql_query("SELECT * FROM links WHERE link='$link'");
      	 $get = mysql_fetch_array($sql);
     	 $oldpoints = $get['points'];

  	 $newpoints = $oldpoints + $points; 

  	 mysql_query("UPDATE links SET points='$newpoints' WHERE link='$link'");

  	 $value = "Has been voted for.";

     setcookie("vote[$link]", $value, time()+3600, "/");
       }

   }
  }

lets assume that the cookie is set already after the first vote and that $link is the link for each image.

Thanks

Ant

P.S. I say voting system, but as you might have guessed it updates points in a database.

    hi anthill!

    try this piece of code and adept it to your needs:

    <?php
    import_request_variables('GPC', '_');
    
    if (isset($_action) && $_action == 'vote') {
    	if (isset($_COOKIE['vote']) && array_key_exists($_name, $_COOKIE['vote'])) {
    		echo "You have already voted !<br>";
    	} else {
    		echo "Thanks for your voting!<br>";
    		setcookie("vote[$_name]", $_value, time()+3600, "/");
    		//$query = "UPDATE links SET points = (points + 1) WHERE link = '$link'";
    	}
    }
    echo "<pre>_COOKIE ==> "; print_r($_COOKIE); echo "</pre>";
    

    <HTML>
    <HEAD>
    <title>_COOKIE</title>
    </HEAD>
    <BODY>
    <a href="10226913.php?action=vote&name=pic01&value=1"><img src="" alt="pic01" border="0"></a>
    <a href="10226913.php?action=vote&name=pic02&value=1"><img src="" alt="pic02" border="0"></a>
    </BODY>
    </HTML>

    hope that helps!!

    ceeyaa.MeX

      Thanks for the reply MeX

      Not sure if it helps as I got a little confused by your method, but going back to my method i've got it now so the cookie would read something like

      vote[0]http://www.domain.com/picture.gif

      and when you vote again because the link already exists it says so.

      so if i vote with another image (say image 2) it will see if the link exists, if not adding it to the cookie like:

      cote[1]http://www.domain.com/picture2.gif

      the only problem i have now is coz there are two cookie names in the cookie (vote[0] and vote [1], if i try to vote for picture2.gif again the result will say

      doesn't existalready exists

      coz first it reads the first cookie which doesn't match, then the second one does. hence adding another vote.

      Hope someone understand me, here's the code now anyway:

      if (isset($_COOKIE['vote']))  
      { foreach ($_COOKIE['vote'] as $name => $value) { if($value==$link) { echo "already exists"; } else { if($name==$count) { $value = $link; $count+=1; setcookie("vote[$count]", $value, time()+3600, "/"); echo "doesn't exist"; } else { header("location: browse.php"); } } } } else { $value = $link; $count = 0; setcookie("vote[$count]", $value, time()+3600, "/"); }

      What i guess i'm asking is how can i stop it from searching through each cookie and making a decision wether the link exists or not?

      Thanks for all your help

      Ant

        Instead of doing what you're doing in the loop, only use a boolean variable. That way nothing is outputted. Then outside the loop, you check for that boolean variable and do whatever needs to do depending on the value of the boolean variable.

        Diego

          thanks

          tried that though, still won't work, best i've got is that you can't vote for the same one but you can vote for others, but once you vote for one of the others you can vote again for the first on.

          If anyone can come up with a solution i'd be sooo happy.

          Maybe some sample code?

          Off to bed 🙁

          Ant

            Can we see what you've tried with the boolean variable?

            Diego

              I think i've got it working now, although i'm not sure i need everything in the script as its all a bit messy.

              To be honest i'm not completely sure what you was asking of me diego25 so pardon my newbism.

              anyway here's the code, is it right (well it works) or is there stuff i don't need (just basic tidying up)?

              What it does is creates a cookie on the first vote, then if that cookie exists vote[0] you can't vote for that again, so when you vote for another it will create vote[1], and so on. If the linke exists in any of the cookies then it returns an error.

              if (isset($_COOKIE['vote']))  
              { foreach ($_COOKIE['vote'] as $name => $value) { if($value==$link) { header("location: error2.php?error=1"); exit; } } foreach ($_COOKIE['vote'] as $name => $value) { if($name==$count) { $count+=1; } } $sql = mysql_query("SELECT * FROM links WHERE link='$link'"); $get = mysql_fetch_array($sql); $oldpoints = $get['points']; $newpoints = $oldpoints + $points; mysql_query("UPDATE links SET points='$newpoints' WHERE link='$link'"); $body = $link; setcookie("vote[$count]", $body, time()+3600, "/"); } else { $sql = mysql_query("SELECT * FROM links WHERE link='$link'"); $get = mysql_fetch_array($sql); $oldpoints = $get['points']; $newpoints = $oldpoints + $points; mysql_query("UPDATE links SET points='$newpoints' WHERE link='$link'"); $body = $link; $count = 0; setcookie("vote[$count]", $body, time()+3600, "/"); }

              Thanks for all your help guys!

              Ant

                why don't you use my script!?
                it's short and simple:

                <?php
                // voting_system.php
                import_request_variables('GPC', '_');
                
                if (isset($_action) && $_action == 'vote') {
                    if (isset($_COOKIE['vote']) && array_key_exists($_name, $_COOKIE['vote'])) {
                        echo "You have already voted !<br>";
                    } else {
                        echo "Thanks for your voting!<br>";
                        setcookie("vote[$_name]", $_value, time()+3600, "/");
                        //$query = "UPDATE links SET points = (points + 1) WHERE link = '$link'";
                    }
                }
                // UNCOMMENT FOR DEBUG
                //echo "<pre>_COOKIE ==> "; print_r($_COOKIE); echo "</pre>";
                ?>
                <HTML> 
                  <HEAD> 
                    <title>_COOKIE</title> 
                  </HEAD> 
                  <BODY> 
                    <a href="voting_system.php?action=vote&name=pic01&value=1"><img src="" alt="pic01" border="0"></a> 
                    <a href="voting_system.php?action=vote&name=pic02&value=1"><img src="" alt="pic02" border="0"></a> 
                  </BODY> 
                </HTML> 
                

                add some debug messages to see what it does!!

                ceeyaa.MeX

                  Hi MeX!

                  Its not that I'm ignoring your script, its just first and foremost i wanted to see if i could write my own 🙂 , also, i wasn't too sure about what some of your code meant. I'm sure your code works fine and yeah it looks nice and short, but I didn't know what the request variabls GPC e.t.c. was.

                  Anyway, like i said, thanks MeX! I just wanted to write my own first as i'm new to this and wanna learn as i go along 🙂 I twould be a shame not to use the one i wrote as i've lost so much hair trying to make it work 🙂

                  Thanks

                  Ant

                    Write a Reply...