hello guys, i have spent one week on this problem but untill now my proplem does not soloved.
my code is about rating system and i can not insert vote for question that i voted for, throught this code vote will insert for the all displays question.

note that i have while inside while to display the questions and it's answers.

i have tree tables as this:

questions:

| id | question|

answer:

| answer | id_answer | id_question|

vote:

| id_vote | id_question | id_answer | ip

<html>

<head>
<link href='style.css' type='text/css' rel='stylesheet'/>
<meta charset="utf-8"/>

</head>
<body>

<?php include "config.php";?>



<form action='vote.php' method='post'>
<div id='container'>

<?php 
$select_question=mysql_query("select * from question order by id desc ")or die(mysql_error());

while ($object_qu=mysql_fetch_object($select_question)){
$id_question=$object_qu->id;
$question=$object_qu->question;


echo"<div id='headblcok'>$question</div>";


///////////////////////////////insert vote into database
if(!empty($_POST['answer'])){
$ip=getenv("remote_addr");

$id_answer_vote=$_POST['answer'];
if($_POST['vote']){
$ip=getenv("remote_addr");


$check_ip=mysql_query("select * from answer where id_question='$id_question'");
$get_target_question=mysql_fetch_object($check_ip);
$get_target_question->id_question;

echo"<br/>";


$insert_vote=mysql_query("insert into vote values('','$id_question','$id_answer_vote','$ip')")or die(mysql_error());

}

}
/////////////////////////////////////////////////////////



?>


<?




$select_answer=mysql_query("select * from answer where id_question='$id_question'")or die(mysql_error());
$num_answer=mysql_num_rows($select_answer);


////////////////////////////////looping answer's question
while ($object_ans=mysql_fetch_object($select_answer)){
 $id_answser=$object_ans->id_answer;
  $id_question_fromanswer=$object_ans->id_question;
 $answer=$object_ans->answer;
$select_per=mysql_query("select * from vote where id_question='$id_question' and id_answer='$id_answser'");

 $num_votes=mysql_num_rows($select_per);
$fore_vote=ceil(($num_votes/$num_answer)*100)/6;
///////////////////////////////////////////////



echo"<input type='radio' name='answer' value='$id_answser'/> ";

echo"$answer    ($num_votes Votes)<div id='block'> <img  src='v.png' width='$fore_vote' height='20'/> </div>";
echo"<br/>";
}





}





?>


<input type='submit' value='Vote' name='vote'/>
</form>
</div>





</body>


</html>

Recommended Answers

All 4 Replies

Member Avatar for diafol

So this is your code [indented and tidied]:

<html>
<head>
<link href='style.css' type='text/css' rel='stylesheet'/>
<meta charset="utf-8"/>
</head>
<body>
<?php include "config.php";?>

<form action='vote.php' method='post'>
    <div id='container'>

<?php 
$select_question=mysql_query("select * from question order by id desc ")or die(mysql_error());

while ($object_qu=mysql_fetch_object($select_question)){
    $id_question=$object_qu->id;
    $question=$object_qu->question;
    echo"<div id='headblcok'>$question</div>";

    ////insert vote into database
    if(!empty($_POST['answer'])){
        $ip=getenv("remote_addr");
        $id_answer_vote=$_POST['answer'];

        if($_POST['vote']){
            $ip=getenv("remote_addr");
            $check_ip=mysql_query("select * from answer where id_question='$id_question'");
            $get_target_question=mysql_fetch_object($check_ip);
            $get_target_question->id_question;
            echo"<br/>";
            $insert_vote=mysql_query("insert into vote values('','$id_question','$id_answer_vote','$ip')")or die(mysql_error());
        }
    }
    ////

    $select_answer=mysql_query("select * from answer where id_question='$id_question'")or die(mysql_error());
    $num_answer=mysql_num_rows($select_answer);

    ////looping answer's question
    while ($object_ans=mysql_fetch_object($select_answer)){
        $id_answser=$object_ans->id_answer;
        $id_question_fromanswer=$object_ans->id_question;
        $answer=$object_ans->answer;
        $select_per=mysql_query("select * from vote where id_question='$id_question' and id_answer='$id_answser'");
        $num_votes=mysql_num_rows($select_per);
        $fore_vote=ceil(($num_votes/$num_answer)*100)/6;
        ////
        echo "<input type='radio' name='answer' value='$id_answser'/> ";
        echo "$answer    ($num_votes Votes)<div id='block'> <img  src='v.png' width='$fore_vote' height='20'/> </div>";
        echo"<br/>";
    }
}
?>
        <input type='submit' value='Vote' name='vote'/>
    </form>
</div>
</body>
</html>

OK, few notes in general:

Lines 9/10 form/div - wrong way around compared to last few lines.
Mixing php in the middle of the form makes little sense to me. Why not hold all this code above the DTD, save all the output to a variable and echo the variable inside the form?
There's a lot of DB calls going on here - perhaps there's a way of cutting down.
Showing all the questions and their answers may take its toll on loading your page - it'll be a mile long! :(
Why not use a <previous><next> for the questions? Or even a hidden/show system with a button next to each question?
Separate your update/insert to the select phases - you could/should use ajax to vote so it's not an obtrusive action - especially for mobile.

Also - do you allow a voter to change his/her mind? I can't see any update query (or replace / update on duplicate key), so I guess not. This being the case, the questions already voted upon by the ip (ip = not very reliable), should not get a set of radiobuttons?

I'm a bit confused - is this supposed to be like a StackOverflow vote for the best answer thingy or is it like a questionnaire?

aha i understand all points you have mentioned.but i am not understand this piont: hold all this code above the DTD"what do you mean dtb".
i see many sites displays multiple votes on the page i just want display 2 question on the page.
i am not perfect with ajax.
you have not menthioed the solve of issue when i insert vote the vote will insert for all display question because it is inside while looping i think

Member Avatar for diafol

DTD means the <!DOCTYPE HTML> (doctype declaration)

So your solution may be more complicated than originally suggested - maybe using pagination

I can't offer a solution, as I'm struggling to understand what you're trying to do.

I suggest:

Keep your post/insert code totally separate from your select/view code.
So when you send (post vote), you send to a different file (not the one with the form in it).

We need to know:

  • can an user change his/her vote at a later date e.g. decide that another answer is better - so transfer the vote?
  • can answers be added to a question even though voting has started?
  • are there fixed number of answers to each question e.g. 6

I have an old class for something similar to yours. If you're interested, I can dig it out and dust it off.

i separate insert vote from questions and their answers as this and it work's well.but now i want if the the user have already voted on this question , i want to display the results without radio button, or something similar to this idea.

this is an image to my rating system: http://oi40.tinypic.com/vgla84.jpg

this is my code:

<html>
<head>
<link href='style.css' type='text/css' rel='stylesheet'/>
<meta charset="utf-8"/>
</head>
<body>
<?php include "config.php";?>


 <?php 

 ////insert vote into database
    if(!empty($_POST['answer'])){
    $tyr=$_POST['answer'];
        $ip=getenv("remote_addr");
        $id_answer_vote=$_POST['answer'];

        if($_POST['vote']){

            $ip=getenv("remote_addr");
            $check_ip=mysql_query("select * from answer where id_answer='$tyr'");
            $get_target_question=mysql_fetch_object($check_ip);
            $g=$get_target_question->id_question;

            $insert_vote=mysql_query("insert into vote values('','$g','$id_answer_vote','$ip')")or die(mysql_error());
        }
    }

?>



<form action='vote.php' method='post'>
    <div id='container'>

<?php 
///////////////////////////loopin questions
$select_question=mysql_query("select * from question order by id desc ")or die(mysql_error());
$ip=getenv("remote_addr");
while ($object_qu=mysql_fetch_object($select_question)){
    $id_question=$object_qu->id;
    $question=$object_qu->question;
    echo"<div id='headblcok'>$question</div>";


        $select_answer=mysql_query("select * from answer where id_question='$id_question'")or die(mysql_error());
        $num_answer=mysql_num_rows($select_answer);

    ////////////////////looping answer for the questions
    while ($object_ans=mysql_fetch_object($select_answer)){
        $id_answser=$object_ans->id_answer;
        $id_question_fromanswer=$object_ans->id_question;
        $answer=$object_ans->answer;
        $select_per=mysql_query("select * from vote where id_question='$id_question' and id_answer='$id_answser'");
        $num_votes=mysql_num_rows($select_per);
        $fore_vote=ceil(($num_votes/$num_answer)*100)/6;
        ////
        echo "<input type='radio' name='answer' value='$id_answser'/> ";
        echo "$answer    ($num_votes Votes)<div id='block'> <img  src='v.png' width='$fore_vote' height='20'/> </div>";
        echo"<br/>";
    }
}
?>
        <input type='submit' value='Vote' name='vote'/>
    </form>
</div>


</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.