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
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]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

[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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.