CodeIgniter Forums
Simple vote up/down? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Simple vote up/down? (/thread-10733.html)



Simple vote up/down? - El Forum - 08-10-2008

[eluser]Unknown[/eluser]
I've been trying for hours...

How would I go about creating two buttons/images which are labeled "up" and "down"?
Upon clicking one of them, they will launch a SQL query that will add +1 to the current row?

For example:
Current up: 8
Current down: 3
*User clicks up button/image*
*mySQL does it magic*
Current up: 9
Current down: 3


Simple vote up/down? - El Forum - 08-10-2008

[eluser]ywftdg[/eluser]
I would say the typical, basic php method, would be to pass a number (or value) through the url, then use the URI helper to get those values and run your query. You could also use a more modern approach, if you need/want it, and use ajax.


Simple vote up/down? - El Forum - 08-10-2008

[eluser]Unknown[/eluser]
[quote author="ywftdg" date="1218441891"]I would say the typical, basic php method, would be to pass a number (or value) through the url, then use the URI helper to get those values and run your query. You could also use a more modern approach, if you need/want it, and use ajax.[/quote]Yea, I wanted to do ajax. Would it be possible to help me out on this? If ajax is too much, we can go for the non-ajax version.


Simple vote up/down? - El Forum - 08-10-2008

[eluser]Sumon[/eluser]
Very easy with AJAX also. Use former solution by ajax.
Lets say your url is: www.mysite.com/counts/3 (You have tested this works fine when you hit this url directly)
For example consider this is your AJAX function.
Code:
[removed]
    function CreateNewHttpObject()    //Create an Instance of HTTP request.
    {
        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;
    function DisplayOutput (divID)
    {
        var ObjHttp= new CreateNewHttpObject();
        if(ObjHttp)
        {
            var obj = document.getElementById(divID);
            ObjHttp.open("GET", 'www.mysite.com/counts/3');
            ObjHttp.onreadystatechange = function()
            {
                if (ObjHttp.readyState == 4 && ObjHttp.status == 200)
                    obj[removed] = ObjHttp.responseText;
            }
            ObjHttp.send(null);
        }
    }

Simply call the function from your buttons/images which are labeled “up” and “down”
Code:
DisplayOutput('divwhereyourCurrentup8show')
I Hope you get my point.