Jump to content

help with table


csabi_fl

Recommended Posts

Hi guys.
I need some help with my table.It looks something like this:
loginid|week1|total:
John|5|0
John|7|0
John|3|0
Now in the 'total' column (where all the 0's are)I want to have the sum of 'week1'(5+7+3).I think I need the UPDATE command but I don't know how to phrase it.
Something like:
UPDATE * SET total=???? WHERE loginid='John'.
Is that possible to have the total in only 1 row not in all 3 of them?
Thank you in advance.
Link to comment
https://forums.phpfreaks.com/topic/23858-help-with-table/
Share on other sites

It's trivial to get the sum for each login:
[code]SELECT SUM(total) FROM yourTable GROUP BY loginid[/code]

A summary table would simply be a place where you store the result of the above query; but in general, it's not necessary, at least for MyISAM tables.
Link to comment
https://forums.phpfreaks.com/topic/23858-help-with-table/#findComment-108557
Share on other sites

Hi again.
I am trying to output the results just like you said.
I use the following code:

<?php
mysql_pconnect("localhost","","");
mysql_select_db("");
$query  = "SELECT loginid,SUM(week1+week2) FROM submit1 GROUP BY loginid ORDER BY 'SUM(week1)+SUM(week2)' DESC";
$result = mysql_query($query);
while ($list = mysql_fetch_array($result)) {
      echo "{$list['loginid,SUM(week1)+SUM(week2) ']}<br>";
}
?>
I am struggling with 2 things.
1)The command DESC isn't working,numbers aren't showing up in descending order.
2)Inside the 'echo' statement something isn't right, I guess it is a syntax error,I want the output to be in a chart ,for example:
John|75
Mary|70
Mike|60 and so on.
Thank you very much for your help.
Link to comment
https://forums.phpfreaks.com/topic/23858-help-with-table/#findComment-109673
Share on other sites

It works great.Thank you.
I want to adjust the 'echo' statement so that it will show something like this:
1) John|75
2) Marienne|70
3) Michael|60
and so on.
How do I get the order nr 1,2,3 and how do I line up the points 75,70 and 60 to show up in a column nice and neat under one another?
Link to comment
https://forums.phpfreaks.com/topic/23858-help-with-table/#findComment-110182
Share on other sites

Just initialize a PHP variable to zero outside the loop, and increment it for each iteration of the loop; then you can echo this as well, with whatever formatting you desired.  As for padding, it's going to be easiest if you "cheat" and assume that you won't exceed a given length, and simply prepend/append with spaces the difference between this length N and the length your name field.  If you want details about PHP implementation, post another thread in the PHP help forum.  We've drift far away from MySQL, so I can't really be of any more help.
Link to comment
https://forums.phpfreaks.com/topic/23858-help-with-table/#findComment-111367
Share on other sites

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.