OK, I'm attempting to put together a top 25 college hoops poll to do with my friends. I've got the database set up, and I can insert the data via the mysql terminal and it works just fine. Here's my table structure:table-users:userid, usernametable-teamsteamid, teamnametable-votesuserid, teamid, position, timestampNow logically speaking, I'd basically be wanting to do this:for each "position" (1 being the first pull down, 25 being the last) insert that position number (1-25) for the team chosen and apply it to the 'username' chosen in the 'username' pulldown menu. Now if I were inserting via the terminal, the statement would look like this: (I shortened it to 10 teams/10 positions for the sake of making this shorter) insert into votes(uid, tid, pos) values(1, 5,1),(1,10,2),(1,2,3),(1,7,4),(1,6,5),(1,9,6),(1,1,7),(1,3,8),(1,4,9),(1,8,10);The first number would be whatever usernameid was chosen, in this case "1" the second number would be the teamid (which would show in the pulldowns as the actual team name, not the id number) and the third number would be what place said team was in according to that person. Now how the hell can I get this into a form...??? I know loops, or arrays or whatever, but I'm not a coding expert and this is all in fun so who wants to be a hero?
12/23/2006 7:43:27 PM
for the form part
<form name="form" action="<?PHP print $PHP_SELF; ?>" method="post"><select>...</select><input type="submit" name="sub" value="WORDS ON THE BUTTON"></form>
<select name="team[0]"> <option value="1">1</option> <option value="2">2</option> ... <option value="25">25</option></select>
if($sub) { for($i=1; $i<=25; $i++) { query="insert into votes (uid, tid, pos) values (1, $i, team[$i])"; //db code goes here }}
12/23/2006 9:13:29 PM
btw - use PHP to create the tedius form attributes. e.g. a loop from 1-25 to create all the <option> tags
12/23/2006 9:52:20 PM
<?phpfor($i=1; i <= 25; $i++) {echo "<option value=".$i.">".$i."</option>;}?>[Edited on December 23, 2006 at 10:16 PM. Reason : ]
12/23/2006 10:16:05 PM
you guys rock. Seriously....but OK wait......for my initial php code, wouldn't I want instead of the number 1 (for the uid) it should rather be something along the lines of $_POST['uid'] ?if($sub) { for($i=1; $i<=25; $i++) { query="insert into votes (uid, tid, pos) values (1, $i, $_POST['team[$i]'])"; mysql_connect("localhost","login****","password****") or die("Unable to connect to SQL server"); mysql_select_db("ncaahoops") or die("Unable to select database"); mysql_query($query) or die("Insert Failed!"); } ?> and then for my form....I'm a little lost because I need the 1st pulldown to be the username so something like:
12/23/2006 11:33:30 PM
okay, you only connect and select the database once, no need to put that in a loop.
12/24/2006 8:48:27 AM
ok. here's a simple display page for the voting. It queries all the users and teams and does the display, but it does not handle any input processing or actual vote entry. demo here - http://joelion.com/poll/code here
<html><body><? include("dbconnect.php"); $userQuery = mysql_query("SELECT * FROM `table-users` ORDER BY `username`;"); $teamQuery = mysql_query("SELECT * FROM `table-teams` ORDER BY `teamname`;"); while ($row=mysql_fetch_array($teamQuery)) { $teamArray[$row['teamid']] = $row['teamname']; }?>Select User:<form action=<?=$_SERVER['PHP_SELF']?> method="post"> <select name="userID"><?php while( $row=mysql_fetch_array($userQuery) ) echo ("<option value=".$row['userid'].">".$row['username']."</option>\n");?> </select> <p>Enter Votes:<br /><?php for ($i=1; $i<=25; $i++) { echo "$i: <select name=\"rank-$i\">\n"; foreach ($teamArray as $teamid => $teamname) { echo ("<option value=".$teamid.">".$teamname."</option>\n"); } echo "</select><br>\n"; }?><input type=submit name="vote" border=0 value="Submit"></form></body></html>
12/24/2006 3:00:48 PM