SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 48
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    trying to install a rating system

    I'm new to PHP so bear with me:

    I have a lot of problems with the following:

    I'm trying to create a php rating system for my website. however, there are alot of problems with it I can't solve, and I was hoping someone here could.

    it involves two pages right now, one called "rate.php" which is where the initial rating form is, and where the actual ratings are posted.
    The second page is "rateprocess.php" which involves the coding to process the rating submissions.

    both pages can be found here:

    http://garage.noammo.org/rate.php
    http://garage.noammo.org/rateprocess.php

    you won't be able to see all of the coding for either, so I will post them here.

    Here are the problems for each page:

    RATE.PHP

    coding:

    PHP Code:
      <?PHP
        $dbh
    =mysql_connect ("localhost""francis_rate""(I have my password here)") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("francis_ratings");
    $sql "SELECT
    name,
    rating,
    email,
    comment
    FROM
    ratings
    "
    ;
    $sql_result mysql_query($sql) or die ('Could not select data');
    print(
    "<BR><BR>");
    while (
    $row mysql_fetch_row($sql_result))
    {
    $name$row[0];
    $email$row[1];
    $rating$row[2];
    $comment$row[3];
    $commentnl2br($comment);

    print(
    "<table width='50%' border='0' height='100'>");
    print(
    "<tr>");
    print(
    "<td height='5' align='left' width='78%'><font face='Verdana, Arial, Helvetica, sans-serif' size='2'>$name</font></td>");
    print(
    "<td height='5' align='left' width='78%'><font face='Verdana, Arial, Helvetica, sans-serif' size='2'>$email</font></td>");
    print(
    "</tr>");
    print(
    "<tr>");
    print(
    "<td align='left' valign='top' height='5' width='78%'><font face='Verdana, Arial, Helvetica, sans-serif' size='2'>$rating</font></td>");
    print(
    "</tr>");
    print(
    "<tr>");
    print(
    "<td align='left' valign='top' height='5' width='78%'><font face='Verdana, Arial, Helvetica, sans-serif' size='2'>$comment</font></td>");
    print(
    "</tr>");
    print(
    "</table>");
    print(
    "------------------------------------------------------------------------");
    }
    mysql_close($dbh);
    ?>
    here is the HTML coding of the form:

    <table width="100%" bgcolor="#FFFFFF">

    <tr>
    <td bgcolor="#EBEBEB">


    <form name="rate" method="post" action="rateprocess.php">
    <table width="50%" border="0" height="192">
    <tr>
    <td height="5"><div align="right"><font face="Tahoma" size="2">Name/font></div></td>
    <td height="5" align="left" valign="top"><input type="text" name="name" size="30"></td>
    </tr>
    <tr>
    <td height="5"><div align="right"><font face="Tahoma" size="2">Email/font></div></td>
    <td height="5" align="left" valign="top"><input type="text" name="email" size="30"></td>
    </tr>
    <tr>
    <td height="9"><div align="right"><font face="Tahoma" size="2">Rate/font></div></td>
    <td height="9" align="left" valign="top">
    <strong><font size="2" face="Tahoma">1</font></strong><INPUT TYPE="radio" NAME="rating" value="1">

    <strong><font size="2" face="Tahoma">2</font></strong><INPUT TYPE="radio" NAME="rating" value="2">

    <strong><font size="2" face="Tahoma">3</font></strong><INPUT TYPE="radio" NAME="rating" value="3">

    <strong><font size="2" face="Tahoma">4</font></strong><INPUT TYPE="radio" NAME="rating" value="4">

    <strong><font size="2" face="Tahoma">5</font></strong><INPUT TYPE="radio" NAME="rating" value="5">
    </td>
    </tr>
    <tr>
    <td height="136" valign="top">
    <div align="right"><font face="Tahoma" size="2">Comments/font></div></td>
    <td align="left" valign="top" height="136">
    <div align="left">
    <textarea name="comment" cols="50" rows="7"></textarea>
    </div></td>
    </tr>
    </table>
    <input type="submit" name="Submit" value="Submit">
    </form>

    </td>
    </tr>

    </table>


    the first problem is, that when there have been no rating submissions, there is still that big box with all the default settings (ex. the 0 for the rating) at the top. I would like to know how to get rid of that, and keep that area completely empty until the first rating submission.

    the second problem is that I try to make it so the email appears next to the name, but instead, the rating number does. i dont see anything wrong with my code, but there might be something I'm looking over.

    that is all for rate.php

    RATEPROCESS.PHP

    PHP CODING:

    PHP Code:
     <?PHP

        $dbh
    =mysql_connect ("localhost""francis_rate""(I have my password here)") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("francis_ratings");
    $sql "INSERT INTO ratings
    (
    name,
    email,
    rating,
    comment
    ) VALUES
    (
    '
    $name',
    '
    $email',
    '
    $rating',`
    '
    $comment'
    )"
    ;
    $sql_result mysql_query($sql,$dbh) or die ('Could not insert data');
    mysql_close($dbh);
    print(
    "<BR><BR>");
    print(
    "<font face='Verdana, Arial, Helvetica, sans-serif' size='3'>&nbsp;&nbsp;&nbsp;Thank you for your rating.</font><BR><BR>");


    ?>
    there are a few things with this page I need help with.

    1. can someone show me the redirect script that will send a poster back to the page they came from?
    2. I need help with a few of the messages given to a poster after a post. I have the "Thank you for your rating" working fine, but I would like to know how to create a "You have already rated" message for when someone who has posted before is attempting to do so again.
    The second message I would like to know how to do is how to make it so when somoene hits the submit button without filling in any or all of the forms, they get a msg on the rateprocess page that saying "You have not filled out all the forms".

    Those are all the problems. I am new to PHP, so these may be simple problems for others.

    thanks.

    on a final note, I'm almost certain all mySQL databases and tables are set up correctly

  2. #2
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats nice mate, are you going to be displaying each separate comment and rating, or are you going to be working out an average ratings etc.?

    You would have to put a cookie onto their machine if you wanted to say they had already voted. I.e. setcookie $alreadyrated and then say if $alreadyrated isset then display already rated message.

    To make them come back where they came from, you could just use a java script back button, or better you could put in:

    header("location: yourpage.php); Which automatically takes them back to another page.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks alot

    I dont think I am going to worry about average ratings until I get this main part down. I am planning to display each person's rating and comment as well.

    now I would set these cookies on the process page, right?

    I got the javascript button, thanks for that as well.

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I am trying to get this cookie thing to work, as well as insert a response for if any of the forms are left blank. before I said I was new at php, so I have no idea if I'm doing this correctly:

    PHP Code:
    <?PHP

    setcookie
    ($alreadyrated)

    if (
    $alreadyrated isset) {

    print (
    "Sorry, you have already rated.");

    } else {


        
    $dbh=mysql_connect ("localhost""francis_rate""rate") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("francis_ratings");
    $sql "INSERT INTO ratings
    (
    name,
    email,
    rating,
    comment
    ) VALUES
    (
    '
    $name',
    '
    $email',
    '
    $rating',`
    '
    $comment'
    )"
    ;
    $sql_result mysql_query($sql,$dbh) or die ('Could not insert data');
    mysql_close($dbh);

    if (
    $name =, $email =

    print(
    "<BR><BR>");
    print(
    "<font face='Verdana, Arial, Helvetica, sans-serif' size='3'>&nbsp;&nbsp;&nbsp;Thank you for your rating.</font><BR><BR>");

    }
    ?>
    <A HREF="javascript:history.go(-1)"> Back</A>
    this is the php coding of rateprocess.php. as you can see I left some parts blank:

    PHP Code:
    if ($name =, $email 
    here i wasn't sure what values to enter. should they be 0? since my rating values are intergers I wasn't sure if it would interfere or something. also do I just add or to indicate that only one of these has to be empty in order to recieve the message? like:

    PHP Code:
    if ($name =OR $email =OR $rating OR $comment =0) {

    print (
    "Please fill in all forms");

    } else {

    //rest of the coding, i dont know where to insert this into the original coding

    }

    ?> 
    was I right to include the "}" right before the closing of the php coding to close the "else" statemnts?

    finally, have I done the cookie coding correctly?

    thanks again

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, here is the coding: its not working right, and I can't tell whats wrong:

    PHP Code:
    <?PHP

    setcookie
    ($alreadyrated)


     

    if (
    $alreadyrated isset) {

    print (
    "Sorry, you have already rated.");

    } else {


        
    $dbh=mysql_connect ("localhost""francis_rate""rate") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("francis_ratings");
    $sql "INSERT INTO ratings
    (
    name,
    email,
    rating,
    comment
    ) VALUES
    (
    '
    $name',
    '
    $email',
    '
    $rating',`
    '
    $comment'
    )"
    ;
    $sql_result mysql_query($sql,$dbh) or die ('Could not insert data');
    mysql_close($dbh);

    if (
    $name "0" or $email "0" or $rating "0" or $comment "0") {

    print(
    "<br><br>");
    print(
    "<font face='Tahoma' size='2'>Please fill in all necessary forms.</font><BR><BR>");

    } else {

    print(
    "<BR><BR>");
    print(
    "<font face='Tahoma' size='2'>Thank you for your rating.</font><BR><BR>");

    }

    }
    ?>
    whenever I test the page in a browser, this is all that comes up:


    "); print("Please fill in all necessary forms.

    "); } else { print("

    "); print("Thank you for your rating.

    "); } } ?> Back

  6. #6
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. isset should be if ( isset($alreadyrated) ) {

    2. rating = "0" or $comment "0") - should it not be rating = "0" or $comment = "0") ??

    3. Use echo not print..

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo does not work either.

    2. rating = "0" or $comment "0") - should it not be rating = "0" or $comment = "0") ??
    yes, see i have it here:

    PHP Code:
    if ($name "0" or $email "0" or $rating "0" or $comment "0") { 

  8. #8
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Attach the code here in full..

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is rateprocess.php:

    PHP Code:
    <?PHP

    setcookie
    ($alreadyrated)


     

    if ( isset(
    $alreadyrated) ) {

    echo (
    "Sorry, you have already rated.");

    } else {


        
    $dbh=mysql_connect ("localhost""francis_rate""rate") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("francis_ratings");
    $sql "INSERT INTO ratings
    (
    name,
    email,
    rating,
    comment
    ) VALUES
    (
    '
    $name',
    '
    $email',
    '
    $rating',`
    '
    $comment'
    )"
    ;
    $sql_result mysql_query($sql,$dbh) or die ('Could not insert data');
    mysql_close($dbh);

    if (
    $name "0" or $email "0" or $rating "0" or $comment "0") {

    echo(
    "<br><br>");
    echo(
    "<font face='Tahoma' size='2'>Please fill in all necessary forms.</font><BR><BR>");

    } else {

    echo(
    "<BR><BR>");
    echo(
    "<font face='Tahoma' size='2'>Thank you for your rating.</font><BR><BR>");

    }

    }
    ?>

  10. #10
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you use, I would use || instead of or, and also when you want to say if something equals something, DO NOT use only one equal sign. this is your problem, you are simply assigning 0 to the variables not asking whethey they equal 0, to do this, you would say this:

    PHP Code:
    if ($name == "0" || $email == "0" || $rating == "0" || $comment == "0") { 

  11. #11
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed that, but I'm still getting a strange preview. I am going to test it again later, but right now its still causing problems:

    PHP Code:
    <?PHP

    setcookie
    ($alreadyrated);


     

    if (isset(
    $alreadyrated)) {

    echo (
    "Sorry, you have already rated.");

    } else {


        
    $dbh=mysql_connect ("localhost""francis_rate""rate") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("francis_ratings");
    $sql "INSERT INTO ratings
    (
    name,
    email,
    rating,
    comment
    ) VALUES
    (
    '
    $name',
    '
    $email',
    '
    $rating',`
    '
    $comment'
    )"
    ;
    $sql_result mysql_query($sql,$dbh) or die ('Could not insert data');
    mysql_close($dbh);

    if (
    $name == "0" || $email == "0" || $rating == "0" || $comment == "0") {

    echo(
    "<font face='Tahoma' size='2'>Please fill in all necessary forms.</font><BR><BR>");

    } else { 

    echo(
    "<font face='Tahoma' size='2'>Thank you for your rating.</font><BR><BR>");

    }

    }
    ?>

    thanks for the help so far.

    also, I'm getting an error for line 10:

    PHP Code:
    setcookie($alreadyrated); 
    it keeps saying "
    Warning: Cannot modify header information - headers already sent by (output started at /home/francis/public_html/garage/rateprocess.php:8) in /home/francis/public_html/garage/rateprocess.php on line 10
    Could not insert data"

  12. #12
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Set cookie must come before all other output. So if you have any html printing before that set cookie it wont work.

    Not sure whats wrong with the preview, tell us whats going on with it and what it should be..

  13. #13
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow I feel stupid, I just had parentheses around what I wanted to echo...I removed them, and thats working fine now. I will try that to fix my cookies, thanks

  14. #14
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heeh - youll get used to that.

  15. #15
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow thanks alot cristiano, its working almost perfectly now. the only problem now is this:
    how do I make it so that the page initial form page reloads after someone rated something? what happens is I enter a rating, and it takes me to the process page. I click the back, but the rating does not show up. I have to reload the page first. I want to script it so that people don't need to manually reload it.

  16. #16
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also how do I make it so the latest rating appears at the top? right now the latest appears at the bottom.

  17. #17
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To make it appear with the newest at the top, you would need some way of ordering. Are you loggin the date of each rating? This would be the best way, then in your query you would just say
    PHP Code:
    ORDER by ratingdate DESC 
    Which just says query in order of the date the rating was made in descending order.

    As for the for the automatic reloading, you can't do that with the java script function, or not easily. just use what i said before header("location: yourpage.php); which will take them to the page and automatically refresh it. So if you are on rate.php, and the form goes to rateprocess.php - at the end of rateprocess.php just put
    PHP Code:
    header("location: rate.php); 
    and this should work fine.

  18. #18
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    getting this error:

    Warning: Cannot modify header information - headers already sent by (output started at /home/francis/public_html/garage/rateprocess.php:12) in /home/francis/public_html/garage/rateprocess.php on line 57

    php I used:

    PHP Code:
    header("rate.php"
    i used it right before the closing tag for php.

  19. #19
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check for any output before the header function, it might even be blank space before the <?php.

  20. #20
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gaheris
    Check for any output before the header function, it might even be blank space before the <?php.
    What he said - if you are trying to do this under some html or something it wont work..try this out..

    PHP Code:
    echo "<META HTTP-EQUIV='refresh' CONTENT='0; URL=rate.php'>"

  21. #21
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok I think i have arrived at my final issue:

    the cookie thing isn't working.

    here is what I have

    PHP Code:

    <?PHP
    setcookie
    ($alreadyrated);
    ?>
    that is above all other content and html

    and

    PHP Code:

    if (isset($alreadyrated)) {

    echo (
    "Sorry, you have already rated.<BR><BR><A HREF='javascript:history.go(-1)'> <font size='2' face='Tahoma'>Back</font></A>");

    } else {
    ....
    this part is continued 
    this is in the main html body's php coding.

    does that other refresh go on rateprocess.php or rate.php?

  22. #22
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    does that other refresh go on rateprocess.php or rate.php?
    rateprocess

    I dont see how the set cookie is not working?

  23. #23
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know why either...here it is included in the entire php, if it helps:

    PHP Code:
    <?PHP
    setcookie
    ($alreadyrated);
    ?>

    <html>
    <head>
    <title>RATE PROCESS</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF" text="#000000" link="#666666">
    <?PHP




     

    if (isset($alreadyrated)) {

    echo (
    "Sorry, you have already rated.<BR><BR><A HREF='javascript:history.go(-1)'> <font size='2' face='Tahoma'>Back</font></A>");

    } else {

        if (empty(
    $name) || empty($email) || empty($rating) || empty($comment)) {

        echo 
    "<font face='Tahoma' size='2'>Please fill in all necessary forms.</font><BR><BR>";

        } else { 

            
    $dbh=mysql_connect ("localhost""francis_rate""rate") or die ('I cannot connect to the database because: ' mysql_error());
        
    mysql_select_db ("francis_ratings");
        
    $sql "INSERT INTO ratings
    (
    name,
    email,
    rating,
    comment
    ) VALUES
    (
    '
    $name',
    '
    $email',
    '
    $rating',
    '
    $comment'
    )"
    ;
    $sql_result mysql_query($sql,$dbh) or die ('Could not insert data.');
    mysql_close($dbh);



    echo 
    "<font face='Tahoma' size='2'>Thank you for your rating.</font><BR><BR>";

    }

    }
    ?>
    <A HREF="javascript:history.go(-1)"> <font size="2" face="Tahoma">Back</font></A> 
    </body>
    </html>
    uhh...also, where does the refresh scripting go? above the html tags?

  24. #24
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To set a cookie you need at least a name and some content. See setcookie.

  25. #25
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    USA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i do it like this?

    PHP Code:

    <?PHP
    setcookie
    ("alreadyrated"$alreadyrated);
    ?>

    <html>
    <head>
    <title>RATE PROCESS</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF" text="#000000" link="#666666">
    <?PHP






    if (isset(alreadyrated)) {

    echo (
    "Sorry, you have already rated.<BR><BR><A HREF='java_script_:history.go(-1)'> <font size='2' face='Tahoma'>Back</font></A>");
    Last edited by aaazx; Nov 29, 2003 at 20:48.


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
  •