Generating Simple List from Results in DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Generating Simple List from Results in DB

Post 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!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

ahhh screw that i dont even understand it.. ill just do it in my head.. thanks anyway
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Okay, how about you look at the second link in Useful Posts then?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're using a single quote string, variables referenced in them will not be parsed.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Haven't we gone over this with you before?

read http://php.net/language.types.string for how to make strings.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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
Post Reply