PHP填充数组与mysql列的总和

时间:2012-12-29 18:28:46

标签: php mysql arrays sum

我是编程方面的新手。我正在尝试从MySQL表创建一个数组,稍后我将用它来创建PHP / Javascript中的图形。

整个想法是,我想制作一个数组,填充代表一年中每周的数据。所以,一个包含52个条目的数组(让我们忘记有时会出现的第53周)。

我的数据库:

我的数据库中有一个表,我用它:


+-------------------------+
I       production        I
+-------------------------+
I  Shift     I  (number)  I <-ranging from 1 to 3 (different shifts people make)
I  Line      I  (number)  I <-ranging from 1-8 (different 'conveyor belts')
I  Products  I  (number)  I <-ranging from 0- a lot! (To be entered in a form)
I  Week      I  (number)  I <-ranging from 1 to 52
+-------------------------+

现在,我的想法是,我想要一个数组,填充所有产品的SUM。 SUM(产品)必须存在以下内容:

每班+所有产品的总和。

第1行,第1行,制作了10,000件产品 第1班,第2行,制作了15,000种产品 第1班,第3行,制作了20,000件产品(等等)

SUM(Products)将是从第1班到第3班以及第1到第8行的所有产品 所以:10,000 + 15,000 + 20,000等等。

我希望将此总数放入数组中,将“第1周”作为我的数组键。

因此,第一周你会得到:

$array (
    "1" => // SUM(products) of week 1 (which was the 10,000 + 15,000 + 20,000 etc)
    "2" => // SUM(products) of week 2
    "3" => // SUM(products) of week 3
// etc.

我可以通过添加52种不同的MySQL查询来实现这一点,唯一的区别是WHERE周='x'吗?

到目前为止,我只是在试验interwebz上的代码。

任何帮助将不胜感激! :d

来自荷兰的问候

2 个答案:

答案 0 :(得分:1)

只需取出WHERE子句,就可以获得每周的结果。

$result = mysql_query("SELECT week, SUM(products) AS total 
  FROM production GROUP BY week");

结果将作为结果集中的连续行返回,您可以将它们放入数组中:

$sum_by_week = array();
while ($row = mysql_fetch_assoc($result)) {
  $sum_by_week[$row["week"]] = $row["total"];
}

PS:您没有要求这样做,但您应该知道mysql_ *函数已被弃用。您应该开始将mysqli函数或PDO用于新的PHP应用程序。只有在维护现有应用程序时才值得继续使用旧的mysql_ *函数。

答案 1 :(得分:1)

使用mySQL而不是其他扩展程序提前道歉。 ; - )

<?php // RAY_temp_rowan.php
error_reporting(E_ALL);
echo "<pre>";


// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES


// CREATING A TABLE
$sql = "CREATE TEMPORARY TABLE my_table (
        _key INT         NOT NULL AUTO_INCREMENT,
        shift    INT NOT NULL DEFAULT 0,
        line     INT NOT NULL DEFAULT 0,
        products INT NOT NULL DEFAULT 0,
        week     INT NOT NULL DEFAULT 0,
        PRIMARY KEY(_key)  )";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}



// LOAD UP THE TABLE
mysql_query('INSERT INTO my_table (shift, line, products, week) VALUES (1,2,4000, 1)') or die(mysql_error());
mysql_query('INSERT INTO my_table (shift, line, products, week) VALUES (1,4,4000, 1)') or die(mysql_error());

mysql_query('INSERT INTO my_table (shift, line, products, week) VALUES (1,2,4000, 2)') or die(mysql_error());
mysql_query('INSERT INTO my_table (shift, line, products, week) VALUES (1,4,5000, 2)') or die(mysql_error());

mysql_query('INSERT INTO my_table (shift, line, products, week) VALUES (1,2,4000, 3)') or die(mysql_error());
mysql_query('INSERT INTO my_table (shift, line, products, week) VALUES (1,4,6000, 3)') or die(mysql_error());


// MAKING A SELECT QUERY AND TESTING THE RESULTS
$sql = "SELECT week, SUM(products) as sump FROM my_table GROUP BY week ORDER BY week ASC";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
} // IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS



// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE SELECTED
while ($row = mysql_fetch_assoc($res))
{
    $out[$row["week"]] = $row["sump"];
}

var_dump($out);