csabi_fl Posted October 20, 2006 Share Posted October 20, 2006 Hello there.I use the following code to output data on one of my webpages:<?phpmysql_pconnect("localhost","",""); mysql_select_db(""); $query = "SELECT loginid,SUM(week1+week2) AS weekSum FROM submit1 GROUP BY loginid ORDER BY weekSUM DESC";$result = mysql_query($query);while ($list = mysql_fetch_array($result)) { echo "{$list['loginid']}|{$list['weekSum']}<br>";}?>It looks something like:John|70Michael|65Bill|60and so on.I would like to align it so that the numbers are in 1 column as well.I know I need padding but I don't know the syntax.Also I want to add a column with order numbers:1)John|702)Michael|65 3)Bill|60....I am a newbie so PLEASE explain.Thank you in advance. Quote Link to comment Share on other sites More sharing options...
alpine Posted October 20, 2006 Share Posted October 20, 2006 I have to admit i'm not fully aware of what exactly you are asking for, but here is a example with a line-counter:[code]<?phpecho <<<_HTML<table cellpadding="5" cellspacing="0" border="1"><tr> <td>No.</td><td>Login ID</td><td>Weeksum</td></tr>_HTML;mysql_pconnect("localhost","","");mysql_select_db("");$query = "SELECT loginid,SUM(week1+week2) AS weekSum FROM submit1 GROUP BY loginid ORDER BY weekSUM DESC";$result = mysql_query($query);$counter = 1;while ($list = mysql_fetch_array($result)){$login_id = $list['loginid'];$weeksum = $list['weekSum'];echo <<<_HTML<tr> <td>$counter</td><td>$login_id</td><td>$weeksum</td></tr>_HTML;$counter++;}echo "</table>";?>[/code] Quote Link to comment Share on other sites More sharing options...
csabi_fl Posted October 25, 2006 Author Share Posted October 25, 2006 Hi.That is exactly what I needed it.Thank you.I have one more question.Out of this table how do I output a row on another page?For example:3| Peter|50 (this is one row in the table)How do I output on another page that Peter is in 3. position?Or that Peter has 50 points?Could you help me with the code? Quote Link to comment Share on other sites More sharing options...
csabi_fl Posted October 26, 2006 Author Share Posted October 26, 2006 Rookie needs help here.PLEASE read previous posts.Very urgent. Quote Link to comment Share on other sites More sharing options...
Orio Posted October 26, 2006 Share Posted October 26, 2006 [code]<?php//Connect to DB here$result= mysql_query("SELECT * FROM `TABLE_NAME` WHERE Col_name='Peter'");$row=mysql_fetch_array($result);echo $row['Col_Points']."<br>".$row['Col_Position'];?>[/code]Just change the table and the column names to the ones in your database.Orio. Quote Link to comment Share on other sites More sharing options...
csabi_fl Posted October 26, 2006 Author Share Posted October 26, 2006 [code]Please take a look at this code:[code<?phpecho <<<_HTML<table cellpadding="5" cellspacing="0" border="1"><tr> <td>No.</td><td>Login ID</td><td>Weeksum</td></tr>_HTML;mysql_pconnect("localhost","","");mysql_select_db("");$query = "SELECT loginid,SUM(week1+week2) AS weekSum FROM submit1 GROUP BY loginid ORDER BY weekSUM DESC";$result = mysql_query($query);$counter = 1;while ($list = mysql_fetch_array($result)){$login_id = $list['loginid'];$weeksum = $list['weekSum'];echo <<<_HTML<tr> <td>$counter</td><td>$login_id</td><td>$weeksum</td></tr>_HTML;$counter++;}echo "</table>";?>][/code]It outputs a table on one of my pages.It works great.My question is:How do I change this code so it will output only certain rows.For example one row in this table looks like:34|Paul|120 points.When Paul logs in I want to show that his position in the table is 34.How do I change the above code to do just that? Quote Link to comment Share on other sites More sharing options...
Orio Posted October 26, 2006 Share Posted October 26, 2006 You need to add a condition between "...FROM submit1" to "GROUP BY...". Example:SELECT loginid,SUM(week1+week2) AS weekSum FROM submit1 [b]WHERE username='$username'[/b] GROUP BY loginid ORDER BY weekSUM DESCSee more [url=http://www.w3schools.com/sql/sql_where.asp]here[/url].Orio. Quote Link to comment Share on other sites More sharing options...
csabi_fl Posted October 26, 2006 Author Share Posted October 26, 2006 I don't get it.I changed the code like you said and it pulls one row out of the table,BUT the first column(counter) is 1 for every member.So I believe there is something wrong in the 'echo' statement.Could you take a look at that,please? Quote Link to comment Share on other sites More sharing options...
csabi_fl Posted October 27, 2006 Author Share Posted October 27, 2006 Hi guys.Please read my previous posts and help me out here.It is very URGENT.Thank you. Quote Link to comment Share on other sites More sharing options...
csabi_fl Posted October 27, 2006 Author Share Posted October 27, 2006 Hi. Quote Link to comment Share on other sites More sharing options...
sasa Posted October 27, 2006 Share Posted October 27, 2006 use two queries1st [code]SELECT loginid,SUM(week1+week2) AS weekSum FROM submit1 WHERE username='$username' GROUP BY loginid ORDER BY weekSUM DESC[/code] 2nd from position[code]SELECT COUNT(*)+1, SUM(week1+week2) AS weekSum FROM submit1 WHERE weekSum > $weeksum GROUP BY loginid ORDER BY weekSUM DESC[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.