Jump to content


Photo

How to initialize a php variable


  • Please log in to reply
10 replies to this topic

#1 csabi_fl

csabi_fl
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 20 October 2006 - 08:41 PM

Hello there.
I use the following code to output data on one of my webpages:
<?php
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);
while ($list = mysql_fetch_array($result)) {
echo "{$list['loginid']}|{$list['weekSum']}<br>";
}
?>
It looks something like:
John|70
Michael|65
Bill|60
and 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|70
2)Michael|65
3)Bill|60....
I am a newbie so PLEASE explain.
Thank you in advance.


#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 20 October 2006 - 09:31 PM

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:

<?php

echo <<<_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>";

?>



#3 csabi_fl

csabi_fl
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 25 October 2006 - 02:34 PM

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?

#4 csabi_fl

csabi_fl
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 26 October 2006 - 01:55 PM

Rookie needs help here.PLEASE read previous posts.
Very urgent.

#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 26 October 2006 - 02:38 PM

<?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'];

?>

Just change the table and the column names to the ones in your database.

Orio.
Think you're smarty?

(Gone until 20 to November)

#6 csabi_fl

csabi_fl
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 26 October 2006 - 06:03 PM

Please take a look at this code:
[code<?php

echo <<<_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>";

?>
]
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?

#7 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 26 October 2006 - 06:06 PM

You need to add a condition between "...FROM submit1" to "GROUP BY...". Example:
SELECT loginid,SUM(week1+week2) AS weekSum FROM submit1 WHERE username='$username' GROUP BY loginid ORDER BY weekSUM DESC

See more here.

Orio.
Think you're smarty?

(Gone until 20 to November)

#8 csabi_fl

csabi_fl
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 26 October 2006 - 06:59 PM

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?

#9 csabi_fl

csabi_fl
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 October 2006 - 01:27 PM

Hi guys.
Please read my previous posts and help me out here.It is very URGENT.Thank you.

#10 csabi_fl

csabi_fl
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 October 2006 - 07:21 PM

Hi.

#11 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 27 October 2006 - 09:17 PM

use two queries
1st
SELECT loginid,SUM(week1+week2) AS weekSum FROM submit1 WHERE username='$username' GROUP BY loginid ORDER BY weekSUM DESC
2nd from position
SELECT COUNT(*)+1, SUM(week1+week2) AS weekSum FROM submit1 WHERE weekSum > $weeksum  GROUP BY loginid ORDER BY weekSUM DESC





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users