Jump to content

Archived

This topic is now archived and is closed to further replies.

taylorv

limit question

Recommended Posts

I have a big database of contest entrants and I have randomly selected 30 names from this database to be winners. Part of the rules of this contest were that you could enter as many times as you want, so when I picked a winner, I have updated all their entries as having won and given them a number based on what day they won. (ex. for the first winner - won=\'1\' for the second won=\'2\', etc.)

 

Now for the tricky part...

 

I want to create a page that pulls one record from each winner and displays their information. So how do I pull only one record from all the winners?

 

I thought about doing something like this:

 

$sql1 = \"SELECT * from tbl_name WHERE won=\'1\' LIMIT 1\";

$sql2 = \"SELECT * from tbl_name WHERE won=\'2\' LIMIT 1\";

$sql3 = \"SELECT * from tbl_name WHERE won=\'3\' LIMIT 1\";

Etc.

 

but it seems very ineffiecient.. especially since this contest is going to run for 30 days.

 

So is there a mySQL statement that I can use to make this an easier task? Or do I just have to hash each winner out one at a time?

 

Thanks in advance.

Share this post


Link to post
Share on other sites

I came up with this and it seems to work...

 

for ($row_count=1; $row_count < 31; $row_count++) {

$sql = \"SELECT * FROM tbl_name WHERE won=\'$row_count\' LIMIT 1\";

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_row($result)) {

$fname = $row [0];

$lname = $row [1];

$state = $row [5];

$city = $row [4];

 

/* Now we do this small line which is basically going to tell

PHP to alternate the colors between the two colors we defined above. */

 

$row_color = ( $row_count % 2) ? $color1 : $color2;

 

// Echo your table row and table data that you want to be looped over and over here.

 

print \"<tr bgcolor=\"$row_color\" class=\"answers\">\";

print \"<td nowrap>$row_count - </td>\";

print \"<td>$fname $lname</td> \";

print \"<td>$city, $state</td>\";

print \"</td> \";

print \"</tr>\" ;

}

}

Share this post


Link to post
Share on other sites

Fine.... good you only have 31 rows...

 

I think there might be a nice join solution but if it works...

 

P.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.