Jump to content

How to initialize a php variable


csabi_fl

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/24594-how-to-initialize-a-php-variable/
Share on other sites

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]

<?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>";

?>

[/code]
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?
[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.
[code]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>";

?>
][/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?
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 DESC

See more [url=http://www.w3schools.com/sql/sql_where.asp]here[/url].

Orio.
use two queries
1st [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]

Archived

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

×
×
  • Create New...

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.