homepage Welcome to WebmasterWorld Guest from 54.243.23.129
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

    
Poll
how to change a link into radio buttons
Malmo




msg:4184098
 8:51 am on Aug 9, 2010 (gmt 0)

Hi guys!

I have created a poll that is working, but the answers are links, but I would like to have a form with radio buttons like a poll should look like, but for some reason I canīt get it to work. Below is my code and tables

tables:
questions: *id(int), question(varchar)
answers: *id(int), questionid(int), answer(varchar)
votes: *id(int), ip(varchar), questionid(int), answersid(int)
*primery key


@$vprasanjeID = $_GET['questionID'];
@$odgovorID = $_GET['answersID'];
$ip = $_SERVER['REMOTE_ADDR'];

if(!empty($vprasanjeID) && !empty($odgovorID))
{
$q = mysql_query("SELECT * FROM votes WHERE questionID = '$vprasanjeID' AND ip = '$ip'") or die(mysql_error());

if(mysql_num_rows($q) == "0")
{
mysql_query("INSERT INTO votes (IP, questionID, answersID) VALUES ('$ip', '$vprasanjeID', '$odgovorID')") or die(mysql_error());
}
else
{?>
<script>alert("ste ūe glasovali");</script>
<?php }
}

$q = mysql_query("SELECT * FROM questions ORDER BY id DESC LIMIT 1") or die(mysql_error());
$r = mysql_fetch_object($q);

$vprasanje = $r->question;
$vprasanjeID = $r->id;

$q = mysql_query("SELECT * FROM answers WHERE questionID = '$vprasanjeID' ORDER BY id ASC") or die(mysql_error());

$a = mysql_query("SELECT * FROM votes WHERE questionID = '$vprasanjeID'") or die(mysql_error());
$odgovorov = mysql_num_rows($a);


echo "$vprasanje<br><br>";
while($r = mysql_fetch_object($q))
{
$a = mysql_query("SELECT * FROM votes WHERE questionID = '$vprasanjeID' AND odgovorID = '$r->id'") or die(mysql_error());
$zadetkov = mysql_num_rows($a);

@$racun = ceil(100*$zadetkov/$odgovorov);
$st = mysql_query("SELECT count(*) AS glas FROM votes ") or die(mysql_error());
$glasov = mysql_fetch_array($st);

$preveri_ip = mysql_query("SELECT ip FROM votes WHERE ip = '$ip'") or die(mysql_error());
$prikazi_ip = mysql_num_rows($preveri_ip);

if($prikazi_ip != 0) {
echo "$r->odgovor<span class=comment><b>$zadetkov glasov ($racun%)</b></span><br>
<img src=images/procent.png width=$racun% height=5><br>";
}
else {
[b]Here is the link that show the poll answers that I want to have as a form with radio buttons![/b]
echo "<a href=index.php?pg=prva&questionID=$vprasanjeID&odgovorID=$r->id>$r->answer<a><br>";}
}
if($prikazi_ip != 0) {
echo "<span class=comment>vseh glasov: <b>".$glasov['glas']."</b></span>";
}
?>


I thank everyone in advance for the help you will give.

Best regards from Sweden.

 

redhatlab




msg:4184214
 1:55 pm on Aug 9, 2010 (gmt 0)

Hi,

You are not to far something like this might work:

<input type = 'radio' Name ='odgovorID' value= '$r->id'>$r->answer

Also remember you will need the <form></form>tags to make the form work.

redhatlab




msg:4184216
 1:56 pm on Aug 9, 2010 (gmt 0)

In proper PHP it will be something like:

echo "<input type = 'radio' Name ='odgovorID' value= '$r->id'>$r->answer";
Malmo




msg:4184307
 5:02 pm on Aug 9, 2010 (gmt 0)

I had to add a hidden field for vprasanjeID and now it works! Thanks Redhatlab

Malmo




msg:4185575
 8:50 pm on Aug 11, 2010 (gmt 0)

Everything works, but the last problem is that I canīt fix the else statemnt below. As I press the submit button Glasuj it adds the vote like it should but it stays visible I canīt figure out how to set the brackets so it disapears when the results are shown. The code below is my last attempt to solve the problem.

while($r = mysql_fetch_object($q))
{
$a = mysql_query("SELECT * FROM ip_glas WHERE vprasanjeID = '$vprasanjeID' AND odgovorID = '$r->id'") or die(mysql_error());
$zadetkov = mysql_num_rows($a);

@$racun = ceil(100*$zadetkov/$odgovorov);
$st = mysql_query("SELECT count(*) AS glas FROM ip_glas ") or die(mysql_error());
$glasov = mysql_fetch_array($st);

$preveri_ip = mysql_query("SELECT ip FROM ip_glas WHERE ip = '$ip' AND vprasanjeID = '$vprasanjeID'") or die(mysql_error());
$prikazi_ip = mysql_num_rows($preveri_ip);

if($prikazi_ip != 0) {
echo "$r->odgovor<span class=comment><b>$zadetkov ($racun%)</b></span><br>
<img src=slike/procent.png width=$racun% height=10><br>";
} ?>
<form method="GET" action="index.php?pg=prva">
<input type="hidden" name="vprasanjeID" value="<?php echo $vprasanjeID; ?>"><br>
<input type="radio" name="odgovorID" value="<?php echo $r->id; ?>"> <?php echo $r->odgovor; ?><br>
<?php } ?>

This is the button that makes the trouble

<input type="submit" name="sumbit" value="Glasuj"><br>
</form>
<?php
if($prikazi_ip != 0) {
echo "<span class=comment>vseh glasov: <b>".$glasov['glas']."</b></span>";
}

?>

redhatlab




msg:4185600
 9:54 pm on Aug 11, 2010 (gmt 0)

Hi,

If you use the if($prikazi_ip != 0) to print to comment below why not use something similar to hide the submit button after click.

Malmo




msg:4187817
 4:38 pm on Aug 16, 2010 (gmt 0)

One last question has come up during the creation of the poll. I can not figure out how to add up to ten answers to a poll question.

I have a table id, questionid, answer.
Here is my form
<form method="post" action="">
Question: <input type="text" name="question"><br /><br />
answer1: <input type="text" name="answer" id="answer"><br /><br />
answer2: <input type="text" name="answer" id="answer"><br /><br />
answer3: <input type="text" name="answer" id="answer"><br /><br />
answer4: <input type="text" name="answer" id="answer"><br /><br />
answer5: <input type="text" name="answer" id="answerr"><br /><br />
answer6: <nput type="text" name="answer" id="answer"><br /><br />
answer7: <input type="text" name="answer" id="answer"><br /><br />
answer8: <input type="text" name="answer" id="answer"><br /><br />
answer9: <input type="text" name="answer" id="answer"><br /><br />
answer10: <input type="text" name="answer" id="answer"><br /><br />
<input type="submit" value="Add poll">
</form>


I assume that if statements would be appropiate. The main problem is how to setup the field answer so the every added answer goes into a new row in the table and connects with the right question.

Malmo




msg:4188537
 8:36 am on Aug 18, 2010 (gmt 0)

Hi guys!

I have figured out how to add multiple rows into the table, but the only thing I now canīt figure out how to set it up if I only give 3 options and not 5.
Here is my code:
@$vprasanje = $_POST['vprasanje'];

if(!empty($vprasanje))
{
mysql_query("INSERT INTO anketa_vprasanje (vprasanje) VALUES ('$vprasanje')") or die(mysql_error());
}
@$odg1 = $_POST['odgovor1'];
@$odg2 = $_POST['odgovor2'];
@$odg3 = $_POST['odgovor3'];
@$odg4 = $_POST['odgovor4'];
@$odg5 = $_POST['odgovor5'];
if (!empty($odg1) && !empty($odg2) && !empty($odg3) && !empty($odg4) && !empty($odg5)) {
$odg = array ($odg1,$odg2,$odg3,$odg4,$odg5);
foreach ($odg as $key=>$value) {
$q = mysql_query("SELECT * FROM anketa_vprasanje ORDER BY id DESC LIMIT 1") or die(mysql_error());
$r = mysql_fetch_object($q);
$vprasanjeID = $r->id;
mysql_query("INSERT INTO anketa_odgovori (odgovor, vprasanjeID) VALUES ('$value', '$vprasanjeID') ") or die(mysql_error());
}
}
?>
<form method="POST" action="1.php">
Vpra&#353;anje: <input type="text" name="vprasanje"><br />
Odgovor1:<input type="text" id="odgovor" name="odgovor1"><br />
Odgovor2:<input type="text" id="odgovor" name="odgovor2"><br />
Odgovor3:<input type="text" id="odgovor" name="odgovor3"><br />
Odgovor4:<input type="text" id="odgovor" name="odgovor4"><br />
Odgovor5:<input type="text" id="odgovor" name="odgovor5"><br />
<input type="submit" value="Dodaj anketo">
</form>


Does someone have a solution to this?

Malmo




msg:4194650
 5:26 pm on Aug 31, 2010 (gmt 0)

Hi everyone!

I have one last problem to my poll and that is I canīt figure out how to add the questionid. The code is below:

if($_SERVER['REQUEST_METHOD'] == "POST")
{

I canīt figure out how to add this code so the id of the question is added to the
$q = mysql_query("SELECT * FROM anketa_vprasanje ORDER BY id DESC LIMIT 1") or die(mysql_error());
$r = mysql_fetch_object($q);
$vprasanjeID = $r->id;


$Vrednosti = array();
$Ekipe = $_POST["odgovor"];
foreach($Ekipe as $Ekipa)
{
if(!empty($Ekipa))
{
$Vrednosti[] = "('".mysql_real_escape_string($Ekipa)."')";
}
}
if(count($Vrednosti) > 0)

I have tried this, but with no luck

mysql_query("INSERT INTO anketa_odgovori (odgovor,vprasanjeID) VALUES ".implode(", ", $Vrednosti), $vprasanjeID);
}
?>

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
Odgovor1: <input type="text" name="odgovor[]" id="odgovor"><br /><br />
Odgovor2: <input type="text" name="odgovor[]" id="odgovor"><br /><br />
Odgovor3: <input type="text" name="odgovor[]" id="odgovor"><br /><br />
Odgovor4: <input type="text" name="odgovor[]" id="odgovor"><br /><br />
Odgovor5: <input type="text" name="odgovor[]" id="odgovor"><br /><br />
<input type="submit" value="OK">
</form>


Hopefully someone can help me clear this thing and thank you in advance.

redhatlab




msg:4194724
 7:40 pm on Aug 31, 2010 (gmt 0)

I can't wait for this site to start supporting color coding.

1. If out of the first query you only need the ID you might better off running something like this:

list($vprasanjeid) = mysql_fetch_row(mysql_query("SELECT id FROM anketa_vprasanje ORDER BY id DESC LIMIT 1"))

2. The array you fill to generate the SQL is missing the ID that is why the query failed:

Look for:
$Vrednosti[] = "('".mysql_real_escape_string($Ekipa)."')";

Change for:
$Vrednosti[] = "('".mysql_real_escape_string($Ekipa)."','".$vprasanjeid."')";

3. For the SQL INSERT statement try this:

Look for this:
mysql_query("INSERT INTO anketa_odgovori (odgovor,vprasanjeID) VALUES ".implode(", ", $Vrednosti), $vprasanjeID);

Change for something like this:

$sql = "INSERT INTO anketa_odgovori (odgovor, vprasanjeID) VALUES ".implode(", ", $Vrednosti);

mysql_query($sql);


Thank you

Malmo




msg:4194929
 9:58 am on Sep 1, 2010 (gmt 0)

Thanks redhatlab for the help it solved 50 % of the problem. I have still problem with the other half. The problem is that if I enter a new line into anketa_vprasanje then the id changes, but it doesnīt in the variable. Here is an example what I am trying to achieve.
1. Manchester United is from?
England(vprasanjeID = 1), Italy (vprasanjeID = 1), Scotland (vprasanjeID = 1)
2. Who won the Champions League last season?
AC Milan (vprasanjeID = 2), FC Barcelona (vprasanjeID = 2), FC Schalke 04 (vprasanjeID = 2)
3. Robinho has just signed for?
Real Madrid (vprasanjeID = 3), Rapid Vienna (vprasanjeID = 3), AC Milan (vprasanjeID = 3)
and so on

with the current code the variable $vprasanjeID is always 1.

if($_SERVER['REQUEST_METHOD'] == "POST")
{
@$vprasanje = $_POST['vprasanje'];

if(!empty($vprasanje))
{
mysql_query("INSERT INTO team (vprasanje) VALUES ('$vprasanje')") or die(mysql_error());
}

list($vprasanjeid) = mysql_fetch_row(mysql_query("SELECT id FROM anketa_vprasanje ORDER BY id DESC LIMIT 1"));
$Vrednosti = array();
$Ekipe = $_POST["odgovor"];
foreach($Ekipe as $Ekipa)
{
if(!empty($Ekipa))
{
$Vrednosti[] = "('".mysql_real_escape_string($Ekipa)."','".$vprasanjeid."')";
}
}
if(count($Vrednosti) > 0)
$sql = "INSERT INTO ekipe (odgovor,vprasanjeID) VALUES ".implode(", ", $Vrednosti);
mysql_query($sql);
}
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved