User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » PHP/MYSQL question Page [1]  
mytwocents
All American
20654 Posts
user info
edit post

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, username

table-teams
teamid, teamname

table-votes
userid, teamid, position, timestamp

Now 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

BigMan157
no u
103354 Posts
user info
edit post

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>


name the dropdowns team[#] where # is a number (ex. team[5])

<select name="team[0]">
<option value="1">1</option>
<option value="2">2</option>
...
<option value="25">25</option>
</select>


then put some php code above the actual html code of the page that says

if($sub) {
for($i=1; $i<=25; $i++) {
query="insert into votes
(uid, tid, pos) values
(1, $i, team[$i])";
//db code goes here
}
}


you may need to use $_POST['team[$i]'] instead of team[$i] tho

[Edited on December 23, 2006 at 9:26 PM. Reason : something like that]

12/23/2006 9:13:29 PM

agentlion
All American
13936 Posts
user info
edit post

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

bous
All American
11215 Posts
user info
edit post

<?php

for($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

mytwocents
All American
20654 Posts
user info
edit post

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:





seems to at least populate the drop down, but remember that it's going to be submitted as 1 user, choosing 25 places for 25 teams so I'm not sure if that should be populated differently.

and then for the 25 pulldowns...





OK, I can't even get the fucking page to load. I swear on all that is holy....I will paypal someone $20 right now if they can get this working for me

12/23/2006 11:33:30 PM

bous
All American
11215 Posts
user info
edit post

okay, you only connect and select the database once, no need to put that in a loop.

12/24/2006 8:48:27 AM

agentlion
All American
13936 Posts
user info
edit post

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

 Message Boards » Tech Talk » PHP/MYSQL question Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.