FlashNinja Posted April 11, 2012 Share Posted April 11, 2012 So I'm implementing a ratings system into my site, the problem here though is my WHILE loop for the voting part of the rating system seems to be bugged. The SQL queries that are used for this script work, the problem seems to be that the $rating variable used to start the while loop never seems to contain anything - I've tried echoing it to confirm this fact. Can anyone see what I've done wrong in this script? <?php include 'connect.php'; session_start(); $_SESSION['username']; $username = $_SESSION['username']; $member = $_GET['usr']; if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){ header("Location: login.php"); } $tablename = 'usr_test'; $sql1 = "SELECT * FROM $tablename WHERE usr = '$member'"; $result1 = mysql_query($sql1) or die (mysql_error()); $display = mysql_fetch_row($result1); $newline = "\n"; ?> <html> <h1>User Profile</h1> <body> <div ="userprofile"> Username: <?php echo $member; ?> <br></br> Email: <?php echo $display[3]; ?> <br></br> University: <?php echo $display[4];?> <br></br> Subject: <?php echo $display[5];?> <br></br> </div> <div ="rating"> <?php while($rating = mysql_fetch_array($result1)) { if ($rating['rating'] != 0) $curr = $rating['total'] / $rating['rating']; else $curr = 0; echo "Rating: ". round($curr, 1) . "<br>"; echo "Rate this seller: "; echo "<a href=".$_SERVER['PHP_SELF']."&mode = vote & voted =1&id =".$rating[usr].">Vote 1</a> | "; echo "<a href=".$_SERVER['PHP_SELF']."&mode = vote & voted =2&id=".$rating[usr].">Vote 2</a> | "; echo "<a href=".$_SERVER['PHP_SELF']."&mode = vote & voted =3&id=".$rating[usr].">Vote 3</a> | "; echo "<a href=".$_SERVER['PHP_SELF']."&mode =vote & voted =4&id=".$rating[usr].">Vote 4</a> | "; echo "<a href=".$_SERVER['PHP_SELF']."&mode = vote & voted =5&id=".$rating[usr].">Vote 5</a><p>"; } ?> </div> <div> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260766-implementing-a-ratings-system/ Share on other sites More sharing options...
dcro2 Posted April 11, 2012 Share Posted April 11, 2012 It seems a little strange that you would use the same SQL result for both echoing user info and then looping through ratings. What does the information in this table look like? Is there really more than one row where usr=$member? Quote Link to comment https://forums.phpfreaks.com/topic/260766-implementing-a-ratings-system/#findComment-1336540 Share on other sites More sharing options...
FlashNinja Posted April 11, 2012 Author Share Posted April 11, 2012 I fixed that problem, allowing the voting system to be seen and used . . . but now another problem realting to this has arrived. When a user visits another users profile page, the second user''s username is kept in the url like this: localhost/profile.php?usr=FlashNinja. I then use the usr variable - now $member - to display the seconds user's profile information. The problem that's occuring now though is that I'm losing that username information in the url when I click a voting number in my voting form on the profile page. This of course makes the profile page now useless. Is there anyway I can get fix this so I can have both the user information AND the voting system on one page? Full profile code: <?php include 'connect.php'; session_start(); $_SESSION['username']; $username = $_SESSION['username']; $member = $_GET['usr']; $cookie = $member; if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){ header("Location: login.php"); } $tablename = 'usr_test'; $sql1 = "SELECT * FROM $tablename WHERE usr = '$member'"; $result1 = mysql_query($sql1) or die (mysql_error()); $display = mysql_fetch_row($result1); $newline = "\n"; if ( 'mode' == "vote") { if(isset($_COOKIE[$cookie])) { Echo "Sorry You have already rated this user <p>"; } else { $month = 2592000 + time(); setcookie($member, Voted, $month); mysql_query ("UPDATE $tablename SET total = total+$voted, rating = votes+1 WHERE usr = $member"); Echo "You have voted!"; } } ?> <html> <h1>User Profile</h1> <body> <div ="userprofile"> Username: <?php echo $member; ?> <br></br> Email: <?php echo $display[3]; ?> <br></br> University: <?php echo $display[4];?> <br></br> Subject: <?php echo $display[5];?> <br></br> </div> <div ="rating"> <?php $rating = mysql_fetch_array($result1); if ($rating['rating'] != 0) $curr = $rating['total'] / $rating['rating']; else $curr = 0; echo "Rating: ". round($curr, 1) . "<br>"; echo "Rate this seller: "; echo "<a href=".$_SERVER['PHP_SELF']."?mode = vote & voted =1&id =".$rating['usr'].">Vote 1</a> | "; echo "<a href=".$_SERVER['PHP_SELF']."?mode = vote & voted =2&id=".$rating['usr'].">Vote 2</a> | "; echo "<a href=".$_SERVER['PHP_SELF']."?mode = vote & voted =3&id=".$rating['usr'].">Vote 3</a> | "; echo "<a href=".$_SERVER['PHP_SELF']."?mode = vote & voted =4&id=".$rating['usr'].">Vote 4</a> | "; echo "<a href=".$_SERVER['PHP_SELF']."?mode = vote & voted =5&id=".$rating['usr'].">Vote 5</a><p>"; ?> </div> <div> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260766-implementing-a-ratings-system/#findComment-1336542 Share on other sites More sharing options...
MMDE Posted April 11, 2012 Share Posted April 11, 2012 Either re-direct them back or put the vote registering script on the profile page. You shouldn't really rely on $_SERVER['PHP_SELF'] Quote Link to comment https://forums.phpfreaks.com/topic/260766-implementing-a-ratings-system/#findComment-1336547 Share on other sites More sharing options...
FlashNinja Posted April 11, 2012 Author Share Posted April 11, 2012 Yeah it's not a form but I fixed that $member problem, but the rating system still doesn't work as none of the voting information is being added to the database. This rating system will be the death of me. Quote Link to comment https://forums.phpfreaks.com/topic/260766-implementing-a-ratings-system/#findComment-1336548 Share on other sites More sharing options...
chriscloyd Posted April 12, 2012 Share Posted April 12, 2012 just add the information to the url <?php echo "Rate this seller: "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=1&id={$rating['usr']}\">Vote 1</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=2&id={$rating['usr']}\">Vote 2</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=3&id={$rating['usr']}\">Vote 3</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode vote&voted=4&id={$rating['usr']}\">Vote 4</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=5&id={$rating['usr']}\">Vote 5</a><p>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/260766-implementing-a-ratings-system/#findComment-1336549 Share on other sites More sharing options...
chriscloyd Posted April 12, 2012 Share Posted April 12, 2012 Rating systems are simple, once you get the hang of it you will be like really. It was that easy! Quote Link to comment https://forums.phpfreaks.com/topic/260766-implementing-a-ratings-system/#findComment-1336550 Share on other sites More sharing options...
FlashNinja Posted April 12, 2012 Author Share Posted April 12, 2012 Nearly there. As far as I can tell the voting works except the votes cast aren't being updated on to the database. I'm also trying to make a cookie of the id stored in the url so the user can only vote once, but the servers keeps throowing me an UNEXPECTED T_VARIABLE error. I've updated the code and am using fetch_row instead of an array. <?php include 'connect.php'; session_start(); $_SESSION['username']; $username = $_SESSION['username']; $member = $_GET['usr']; $cookie = Mysite$id; if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){ header("Location: login.php"); } $tablename = 'usr_test'; $sql1 = "SELECT * FROM $tablename WHERE usr = '$member'"; $result1 = mysql_query($sql1) or die (mysql_error()); $display = mysql_fetch_row($result1); $newline = "\n"; if ( "mode" == "vote") { if(isset($_COOKIE[$cookie])) { Echo "Sorry You have already rated this user <p>"; } else { $month = 2592000 + time(); setcookie(mysite.$id, Voted, $month); mysql_query ("UPDATE $tablename SET total = total+$voted, rating = votes+1 WHERE usr = $member"); Echo "You have voted!"; } } ?> <html> <h1>User Profile</h1> <body> <div ="userprofile"> Username: <?php echo $member; ?> <br></br> Email: <?php echo $display[3]; ?> <br></br> University: <?php echo $display[4];?> <br></br> Subject: <?php echo $display[5];?> <br></br> </div> <div ="rating"> <?php $curr = $display[7] / $display[6]; echo "Rating: ". round($curr, 1) . "<br>"; echo "Rate this seller: "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=1&id={$display[0]}\">Vote 1</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=2&id={$display[0]}\">Vote 2</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=3&id={$display[0]}\">Vote 3</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=4&id={$display[0]}\">Vote 4</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=5&id={$display[0]}\">Vote 5</a><p>"; ?> </div> <div> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260766-implementing-a-ratings-system/#findComment-1336554 Share on other sites More sharing options...
FlashNinja Posted April 12, 2012 Author Share Posted April 12, 2012 Sorry for the double post, but I've nearly got it, just wondering if someone can beat me to the solution to this final problem with it. I've fixed up everything, the cookies are stored correctly and all the variables seemingly work, but the script still isn't updating the database with the votes. I've tested the SQL query, that does the actual updating, on phpmyadmin and it works as it should. This obviously means there's something wrong with the php side of things. Can anyone spot the problem? <?php include 'connect.php'; session_start(); $_SESSION['username']; $username = $_SESSION['username']; $member = $_GET['usr']; $id = $_GET['id']; $mode = $_GET['mode']; $voted = $_GET['voted']; $cookie = $id; if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){ header("Location: login.php"); } $tablename = 'usr_test'; $sql1 = "SELECT * FROM $tablename WHERE usr = '$member'"; $result1 = mysql_query($sql1) or die (mysql_error()); $display = mysql_fetch_row($result1); $newline = "\n"; if ( $mode == "vote") { if(isset($_COOKIE[$cookie])) { Echo "Sorry You have already rated this user <p>"; } else { $month = 2592000 + time(); setcookie($id, $voted, $month); mysql_query ("UPDATE $tablename SET total = total+$voted, rating = rating+1 WHERE usr = $member"); Echo "You have voted!"; } } ?> <html> <h1>User Profile</h1> <body> <div ="userprofile"> Username: <?php echo $member; ?> <br></br> Email: <?php echo $display[3]; ?> <br></br> University: <?php echo $display[4];?> <br></br> Subject: <?php echo $display[5];?> <br></br> </div> <div ="rating"> <?php $denometer = $display[6]; if ($denometer == 0) $curr = 0; else $curr = $display[7] / $display[6]; echo "Rating: ". round($curr, 1) . "<br>"; echo "Rate this seller: "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=1&id={$display[0]}\">Vote 1</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=2&id={$display[0]}\">Vote 2</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=3&id={$display[0]}\">Vote 3</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=4&id={$display[0]}\">Vote 4</a> | "; echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=5&id={$display[0]}\">Vote 5</a><p>"; ?> </div> <div> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260766-implementing-a-ratings-system/#findComment-1336566 Share on other sites More sharing options...
FlashNinja Posted April 12, 2012 Author Share Posted April 12, 2012 Got it. Quote Link to comment https://forums.phpfreaks.com/topic/260766-implementing-a-ratings-system/#findComment-1336568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.