SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with an Undefined Index notice on line 158,159 and 160

    Notice: Undefined index: vote in C:\wamp\www\shoe\stores\rating.php on line 158

    Notice: Undefined index: article_id in C:\wamp\www\shoe\stores\rating.php on line 159

    Notice: Undefined variable: article_id in C:\wamp\www\shoe\stores\rating.php on line 160

    The file below is divided in three part a Ajax, style sheet and php script. This is another rating system but there are a couple of variable undefined. The lines are signed below in comments.
    PHP Code:
    <script>
        var http_request = false;
        function makePOSTRequest(url, parameters) { http_request = false;
        if (window.XMLHttpRequest) {http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); }
        } else if (window.ActiveXObject) {
        try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
        try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
        }
        }
        if (!http_request) { alert('Cannot create XMLHTTP instance'); return false; }
        document.getElementById('myspan').innerHTML = "Loading...";
        http_request.onreadystatechange = alertContents;
        http_request.open('POST', url, true);
        http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http_request.setRequestHeader("Charset", "windows-1251");
        http_request.setRequestHeader("Content-length", parameters.length);
        http_request.setRequestHeader("Connection", "close");
        http_request.send(parameters);
        }

        function alertContents() {
        if (http_request.readyState == 4) {
        if (http_request.status == 200) {
        result = http_request.responseText;
        document.getElementById('myspan').innerHTML = result;
        } else { alert('Please try again later.'); }
        }
        }

        function rate(article_id, vote){ makePOSTRequest('star_rating.php', 'article_id=' +article_id+'&vote='+vote); }

        if (document.getElementById) { window.onload = myUnobtrusiveBehavior; }

        function myUnobtrusiveBehavior() {
        var percentstyle="width:<?echo @number_format($current_rating/$count,2)*20 ?> px;";
        var content ="<div class=\"rating\" id=\"rating\"> <ul class=\"star-rating\"> <li class=\"current-rating\" style=\""+percentstyle+"\" >Current rating</li> <li><a id=\"rate1\" class=\"one-star\">1</a></li> <li><a id=\"rate2\" class=\"two-stars\">2</a></li> <li><a id=\"rate3\" class=\"three-stars\">3</a></li> <li><a id=\"rate4\" class=\"four-stars\">4</a></li> <li><a id=\"rate5\" class=\"five-stars\">5</a></li></ul>";

        /*
        var myspan = document.createElement("div");
        myspan.setAttribute("id", "myspan");
        myspan.innerHTML = content;
        document.body.appendChild(myspan);
        */

        document.getElementById("myspan").innerHTML=content;

        if (document.getElementById("rating")) {
        var article_id=document.getElementById("item").value;
        document.getElementById("rate1").onclick = function () {rate(article_id,'1'); return false;}
        document.getElementById("rate2").onclick = function () {rate(article_id,'2'); return false;}
        document.getElementById("rate3").onclick = function () {rate(article_id,'3'); return false;}
        document.getElementById("rate4").onclick = function () {rate(article_id,'4'); return false;}
        document.getElementById("rate5").onclick = function () {rate(article_id,'5'); return false;}
        }

        }

        </script>

        <style>
        .star-rating,
        .star-rating a:hover,
        .star-rating a:active,
        .star-rating a:focus,
        .star-rating .current-rating{
        background: url(star_rating.gif) left -1000px repeat-x;
        }
        .star-rating{
        position:relative;
        width:125px;
        height:25px;
        overflow:hidden;
        list-style:none;
        margin:0;
        padding:0;
        background-position: left top;
        }
        .star-rating li{
        display: inline;
        }
        .star-rating a,
        .star-rating .current-rating{
        position:absolute;
        top:0;
        left:0;
        text-indent:-1000em;
        height:25px;
        line-height:25px;
        outline:none;
        overflow:hidden;
        border: none;
        }
        .star-rating a:hover,
        .star-rating a:active,
        .star-rating a:focus{
        background-position: left bottom;
        }
        .star-rating a.one-star{
        width:20%;
        z-index:6;
        }
        .star-rating a.two-stars{
        width:40%;
        z-index:5;
        }
        .star-rating a.three-stars{
        width:60%;
        z-index:4;
        }
        .star-rating a.four-stars{
        width:80%;
        z-index:3;
        }
        .star-rating a.five-stars{
        width:100%;
        z-index:2;
        }
        .star-rating .current-rating{
        z-index:1;
        background-position: left center;
        }

        /* for an inline rater */
        .inline-rating{
        display:-moz-inline-block;
        display:-moz-inline-box;
        display:inline-block;
        vertical-align: middle;
        }

        /* smaller star */
        .small-star{
        width:50px;
        height:10px;
        }
        .small-star,
        .small-star a:hover,
        .small-star a:active,
        .small-star a:focus,
        .small-star .current-rating{
        background-image: url(star_small.gif);
        line-height: 10px;
        height: 10px;
        }
        </style>


        <?php
        $dbhost 
    'localhost';
        
    $dbuser 'root';
        
    $dbpass 'poliferico';
        
    $dbname 'menu';
        
    $tableName="ratings";
        
    $conn mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname);

        
    $rating_posted=$_POST['vote'];//the actual user vote
        
    $id=$_POST['article_id'];//passed article_id Line 158
        
    if (empty($id)) {$id=$article_id;} // line 159

        
    $query=mysql_query("SELECT total_votes, total_value, used_ips FROM $tableName WHERE id='$id' ")or die(" Error: ".mysql_error());

        
    $numbers=mysql_fetch_assoc($query);
        
    $checkIP=unserialize($numbers['used_ips']);
        
    $count=$numbers['total_votes'];//how many votes total
        
    $current_rating=$numbers['total_value'];//total number of rating added together and stored
        
    $sum=$rating_posted+$current_rating;// add together the current vote value and the total vote value
        
    $tense=($count==1) ? "vote" "votes";//plural form votes/vote

        
    $voted=mysql_fetch_assoc(mysql_query("SELECT * FROM $tableName WHERE used_ips LIKE '%".$_SERVER['REMOTE_ADDR']."%' AND id='$id' ")); //This variable searches through the previous ip addresses that have voted and returns true or false

        //when already voted
        
    if($voted){
        echo 
    "<ul class=\"star-rating\">
        <li class=\"current-rating\" style=\"width:"
    . @number_format($current_rating/$count,2)*20 ."%;\"></li>
        </ul>
        Rating: <strong>"
    .@number_format($current_rating/$count,2)."</strong> ( $count $tense )
        <br /><span style=\"color:red;\">You've already voted.</span><br />"
    ;
        }

        else{
        
    //if not voted do the actual voting
        
    if(isset($rating_posted)){
        
    //open initial voting row if necessary
        
    if (mysql_num_rows($query) == 0) { $sql "INSERT INTO $tableName (id, total_votes, total_value, used_ips) VALUES ('$id', '0', '0', '')"$result mysql_query($sql) or die("err");}
        
    //increment votes, check ips & add/update vote to table
        
    if($sum==0){ $added=0;} else{ $added=$count+1;}
        if(
    is_array($checkIP)){array_push($checkIP,$_SERVER['REMOTE_ADDR']);}else{ $checkIP=array($_SERVER['REMOTE_ADDR']);}
        
    $insert=serialize($checkIP);
        
    mysql_query("UPDATE $tableName SET total_votes='$added', total_value='$sum',used_ips='$insert' WHERE id='$id'") or die("Error");
        
    $response "<div class=\"rating\">Rate: <strong>".@number_format($sum/$added,2)."</strong> <br /> <span style=\"color:red;\">Thank you for your vote cast!</span></div>";
        echo 
    iconv("windows-1251""UTF-8"$response);
        exit;
        }

        
    //if not voting just show the current rating
        
    else { echo "<div id=\"myspan\"></div>";}


        }
    //end if

        
    ?>

  2. #2
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If these notices appear also in first page load then you can just see if the $_POST variables are set or not and if they are then assign the values.

    PHP Code:
    // ..instead do this
    if (isset($_POST['vote']) && !empty($_POST['vote']) && isset($_POST['id']) && !empty($_POST['id']))
    {
        
    $rating_posted $_POST['vote'];
        
    $article_id $_POST['id'];
        
    // run db query..
    }
    else
    {
        
    // something else..


  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    TeNDoLLA, using both isset and !empty is redundant. both isset and empty suppress php notices. isset checks for type null, while empty checks for a falsey value(which includes type null). Basically, the functionality of empty already encompases everything isset does, and more, making the use of isset redundant.

  4. #4
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yes... very true. I've used lately isset together with strlen in a project, because i think empty handles '0' and 0 value also as false. So that is why I automatically put both of these together.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    TeNDoLLA, using both isset and !empty is redundant. both isset and empty suppress php notices. isset checks for type null, while empty checks for a falsey value(which includes type null). Basically, the functionality of empty already encompases everything isset does, and more, making the use of isset redundant.
    Interesting. I had always thought that a variable which was initialized, but had no value would return true for isset(), as well as empty(). I just tested your statement, and it turns out that's not the case, isset() returns false, and empty returns true.

    You learn something every day!

  6. #6
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so instead of like this

    PHP Code:
    if (isset($_POST['vote']) && !empty($_POST['vote']) && isset($_POST['id']) && !empty($_POST['id'])) 
    it is ok to go ahead and do it like this
    PHP Code:
    if (isset($_POST['vote']) && ($_POST['vote']) && ($_POST['id']) && ($_POST['id'])) 
    I have everything in the same page the only value comming from the URL is a variable value contained in $id variale which I have isset on top of the script and I will use it instead of article_id which is the variable that the tutorial use.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •