Hey, I am working on a PHP script that, well is a poll. Right now I'm just working on the actual physical poll code, not how you enter. I have some written, but I was wondering if anyone knew an easier way to call the variables for all the options instead of $option1, $option2 etc. Heres my code. <?php /* $con = mysql_connect("host,"username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $result = mysql_query("SELECT * FROM polls WHERE poll_id = '$currentpoll'") or die(mysql_error()); while($row = mysql_fetch_array($result)) { $option1 = $row[option1]; $option2 = $row[option2]; $option3 = $row[option3]; $op1total = $row[op1total]; $op2total = $row[op2total]; $op3total = $row[op3total]; */ $option1 = Yes; $option2 = No; $option3 = Other; $op1total = 5; $op2total = 6; $op3total = 2; $total = $op1total + $op2total + $op3total; $percent1 = $op1total / $total; $percent2 = $op2total / $total; $percent3 = $op3total / $total; // } echo 'Results'; echo '<br><br>'; echo "<table border=0><tr><td>" . $option1 . "</td><td> <img src=poll.gif height=20 width=" . $percent1 * 300 . "</img></td></tr>"; echo "<tr><td>" . $option2 . " </td><td><img src=poll.gif height=20 width=" . $percent2 * 300 . "</img></td></tr>"; echo "<tr><td>" . $option3 . " </td><td><img src=poll.gif height=20 width=" . $percent3 * 300 . "</img></td></tr></table>"; ?> Code (php): And this the output (No style, just plain text) http://theinterweb.freehostia.com/test/testpoll.php
Looking for like something that pulls the option name, option total, etc one at a time for each option, instead of having to set individual variables, I could set one variable that either changes or holds all of them, and lists them in sequence. so instead of $option 1 = $row[option1]; $option 2 = $row[option2]; $option 3 = $row[option3]; PHP: maybe have like $option% = $row[option%]; PHP: where % is the option number that changes
When you grab $row, it's already an array with those values in it. If you use mysql_fetch_assoc, it will make it an associative array with the keynames correlating to the table column names. This needs a little polishing and such, but it may get you on the right track. I also added a little error checking for you. $con = mysql_connect("host","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $sql = "SELECT * FROM polls WHERE poll_id = '{$currentpoll}'"; $result = mysql_query($sql) or die(mysql_error()); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while($poll = mysql_fetch_assoc($result)) { $poll['total'] = $poll['opt1total'] + $poll['opt2total'] + $poll['opt3total']; $poll['percent1'] = $poll['opt1total'] / $poll['total']; $poll['percent2'] = $poll['opt2total'] / $poll['total']; $poll['percent3'] = $poll['opt3total'] / $poll['total']; } echo 'Results'; echo '<br><br>'; echo "<table border=0><tr><td>" . $poll['option1'] . "</td><td> <img src=poll.gif height=20 width=" . $poll['percent1'] * 300 . "</img></td></tr>"; echo "<tr><td>" . $poll['option2'] . " </td><td><img src=poll.gif height=20 width=" . $poll['percent2'] * 300 . "</img></td></tr>"; echo "<tr><td>" . $poll['option3'] . " </td><td><img src=poll.gif height=20 width=" . $poll['percent3'] * 300 . "</img></td></tr></table>"; Code (php): If $currentpoll is user input IE: the user types in the poll ID, or it's displayed in the location bar where it can be edited, make sure you sanitize the input to protect against SQL injection.
You may actually want to get rid of the while statement since you'll only be getting one row as a result: if (mysql_num_rows($result) !== 1) { if (mysql_num_rows($result) > 1) { echo "Too many rows returned. Make sure there aren't duplicate keys in the database."; exit; } if (mysql_num_rows($result) == 0) { echo "No results were returned."; exit; } } $poll = mysql_fetch_assoc($result) $poll['total'] = $poll['opt1total'] + $poll['opt2total'] + $poll['opt3total']; $poll['percent1'] = $poll['opt1total'] / $poll['total']; $poll['percent2'] = $poll['opt2total'] / $poll['total']; $poll['percent3'] = $poll['opt3total'] / $poll['total']; PHP: Sorry, I'm still waking up.
Thats close to what I was getting at, but I was also wondering if there was a way to do a similar thing when you are recalling the variables. so instead of $poll = mysql_fetch_assoc($result) $poll['total'] = $poll['opt1total'] + $poll['opt2total'] + $poll['opt3total']; $poll['percent1'] = $poll['opt1total'] / $poll['total']; $poll['percent2'] = $poll['opt2total'] / $poll['total']; $poll['percent3'] = $poll['opt3total'] / $poll['total']; PHP: just have like $poll = mysql_fetch_assoc($result) $poll['total'] = $poll['opt1total'] + $poll['opt2total'] + $poll['opt3total']; $poll['percent$num'] = $poll['opt$numtotal'] / $poll['total']; PHP: Notice the $num in there. I was wondering if instead of listing each total, if one line of code could repeat for each option. Also, the $currentpoll will be the latest entry into the database. I just put $currentpoll so my mind would remember where im getting that. It will be like SELECT * FROM polls ORDER BY poll_id DESC LIMIT 1 PHP:
You can mix variables and strings when setting/retrieving values from array keys. IE: $poll['opt1total'] = 5; // Just setting a value $num = 1; // These statements will return the same value echo $poll['opt1total'] . "<br />"; echo $poll['opt' . $num . 'total'] . "<br />"; echo $poll["opt{$num}total"] . "<br />"; // Notice the double quotes. PHP will 'parse' the contents inside double quotes but not single quotes // will echo 5 5 5 PHP: So if you have multiple "opttotal"s you could do this if you know how many you will have: $poll = mysql_fetch_assoc($result) $poll['total'] = $poll['opt1total'] + $poll['opt2total'] + $poll['opt3total']; $num = 1; while ($num <= 3) { $poll["percent{$num}"] = $poll["opt{$num}total"] / $poll['total']; $num++; } PHP: If you don't know how many "opttotal"s you have you could do this: $num = 1; while (isset($poll["opt{$num}total"])) { $poll["percent{$num}"] = $poll["opt{$num}total"] / $poll['total']; $num++; } PHP: Is that closer to what you're looking for?
That's exactly what I was looking for, I just didn't know exactly how to make it do what I wanted. Thank you so much. One last question though. If I do it like the first example, where $num <=3, and i only have 2 options, would it try to return something for the third, would it throw an error or would it just not return anything at all?
Yup. You can change it to <= 2 if you want, but if the number is going to change on it's own I would suggest using the second example which will test to see if there's another one before it runs each loop.
Oh, that's the one I will use, I was just wondering so I would know for future reference. So it would do which, just not output anything?
Well, the while() statement loops over that block of code over and over until the condition inside the while() statement returns false. So every time it will check to see if $poll["opt{$num}total"] is set using the isset() function. When isset() returns false, because .. say .. $poll['opt4total'] doesn't exist, instead of executing the code in the while block, it will stop looping, skip the code block, and continue parsing the rest of your page. It could output something when it stops looping, but you would have to tell it to. You could put an echo() right after the while() block if you want. It's really up to you.
Okay, I won't use it in this script, but I just wanted to know what would happen. Also, now I am a bit confused on how to set up my database. I need a Poll title, and titles and totals for all options. Thinking of setting it up like one table for polls, and one for options. Then enter the name of the poll along with the id in the polls table. Then in the options table, have an ID, title, total and poll_id. Then I can call the poll (SELECT * FROM polls ORDER BY poll_id DESC LIMIT 1) and then get all options that have a poll_id that matches the poll question. (SELECT * FROM options WHERE $poll_id = $currentpoll) Would this work? Thank you so much for the help though.
Well, I set up my database this way, but have no way how to recall both poll_title variable, and option_title where option_poll = $currentpoll