Page 1 of 1

Generating Simple List from Results in DB

Posted: Sun Dec 10, 2006 3:48 pm
by nickman013
Hello,

I have sucessfully created a simple voting system with PHP / MYSQL thanks to the help of a couple of people on this forum..

However, I have one more question, I was wondering if somebody could give me a tutorial or somthing on how to do this.

I am trying to make a list generated by PHP / MYSQL that shows the number of votes for each of the things I have voted.. But I want it set up a specific way that if the # of votes are the same for two or however many things they are at the same order number.

For Ex.

Bob was voted 0 times.
Billy was voted 3 times.
Mike was voted 5 times.
Paul was voted 2 times.
Joe was voted 0 times.
John was voted 5 times.
Scott was voted 1 time.
Barry was voted 2 times.
Phil was voted 0 times.


The list that would be generated is

1. John
1. Mike
2. Billy
3. Paul
3. Barry
4. Scott
5. Joe
5. Phil
5. Bob

Do you understand what I mean?

Thanks Alot!!!!!!

Any help would be greaT!!!

Posted: Sun Dec 10, 2006 4:08 pm
by feyd
You'll need a few checks. Specifically, while iterating over the result data, you will need to determine whether the current person's votes is the same as the previous person's votes. In these situations you would not increment the counter.

Posted: Sun Dec 10, 2006 4:16 pm
by nickman013
ahhh screw that i dont even understand it.. ill just do it in my head.. thanks anyway

Posted: Sun Dec 10, 2006 4:20 pm
by feyd
Okay, how about you look at the second link in Useful Posts then?

Posted: Sun Dec 10, 2006 5:37 pm
by nickman013
Thanks for the tip Feyd...

I checked it out and created a script, I thought would work...

But... I got a little problem with it.

The Code:

Code: Select all

<?
$username2= "muot_report";  
$password2= "apssswrod";  
$database2= "muot_report";  
$connection2 = mysql_connect('localhost',$username2,$password2);  
mysql_select_db($database2); 
$sql4 = "SELECT * FROM `report` WHERE moty =1 ORDER BY `votes` DESC";
$result = mysql_query($sql4) or die(mysql_error()); 
while($row = mysql_fetch_array($result)) { 
echo '<tr><td align=center>$row["prevname"]</td><td align=center>$row["votes"]</td></tr>';
}
?>
Outputs:
NAME # OF VOTES
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
$row["prevname"] $row["votes"]
Any idea why it isnt putting in the correct info.. It outputs the correct number of rows...

THANKS ALOT :D :D :D :D

Posted: Sun Dec 10, 2006 5:42 pm
by feyd
You're using a single quote string, variables referenced in them will not be parsed.

Posted: Sun Dec 10, 2006 5:43 pm
by John Cartwright
single quotes will parse the strings as is, double quotes will parse the variables.

Code: Select all

$foobar = 'Hello World';

echo '$foobar';
//prints $foobar

echo "$foobar";
//prints Hello World
Or simply escape your single quotes

Code: Select all

echo 'Lets escape '. $foobar .' so it will parse';
//prints Lets escape Hello World so it will parse

Posted: Sun Dec 10, 2006 5:43 pm
by nickman013
Oh, But when I reverse them.. I get... a parse error

The error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/muot/public_html/moty_standings.php on line 12

Posted: Sun Dec 10, 2006 5:44 pm
by John Cartwright
nickman013 wrote:Oh, But when I reverse them.. I get... a parse error

The error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/muot/public_html/moty_standings.php on line 12
.... because you will end up with something like

Code: Select all

echo "<tr><td align=center>$row["prevname"]</td><td align=center>$row["votes"]</td></tr>";
Notice what the syntax highlighter shows?

Posted: Sun Dec 10, 2006 5:45 pm
by feyd
Haven't we gone over this with you before?

read http://php.net/language.types.string for how to make strings.

Posted: Sun Dec 10, 2006 5:51 pm
by nickman013
ahh ... I have made a lot of scripts similar to getting info out of a DB.. But today I dont know whats wrong with me I make the stupidest mistakes..

THANK YOU BOTH!!! :D :D :D :D