Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助使sql数据可用于数学函数php吗_Php_Mysql_Oop_Math_Web Applications - Fatal编程技术网

需要帮助使sql数据可用于数学函数php吗

需要帮助使sql数据可用于数学函数php吗,php,mysql,oop,math,web-applications,Php,Mysql,Oop,Math,Web Applications,好的,我正试图通过用户id从多行获取数据,然后将这些值实例化为单独的数组,并放入我的“Checks”类中。。首先,我将向您展示我的sql设置 id user_id date gross net federal state ssi other_pay other_deduct other_tax_deduct company check_no 1 1 2012-07-26 434.88 354.39 40.70 15.71 18.27 0.

好的,我正试图通过用户id从多行获取数据,然后将这些值实例化为单独的数组,并放入我的“Checks”类中。。首先,我将向您展示我的sql设置

id user_id date gross net federal state ssi other_pay other_deduct other_tax_deduct company check_no 1 1 2012-07-26 434.88 354.39 40.70 15.71 18.27 0.00 6.31 0.00 K-VA-TT 3209181 2 1 2012-08-16 433.11 353.09 39.44 15.61 18.19 0.00 6.28 0.00 K-VA-TT 3230201 正如你们很多人可能已经猜到的,我是如何从上面的函数接收数据的

Array ( [id] => 1 [user_id] => 1 [date] => 2012-07-26 [gross] => 434.88 [net] => 354.39 [federal] => 40.70 [state] => 15.71 [ssi] => 18.27 [other_pay] => 0.00 [other_deduct] => 6.31 [other_tax_deduct] => 0.00 [company] => K-VA-TT [check_no] => 3209181 ) Array ( [id] => 2 [user_id] => 1 [date] => 2012-08-16 [gross] => 433.11 [net] => 353.09 [federal] => 39.44 [state] => 15.61 [ssi] => 18.19 [other_pay] => 0.00 [other_deduct] => 6.28 [other_tax_deduct] => 0.00 [company] => K-VA-TT [check_no] => 3230201 ) 我不知道我是否需要以不同的方式检索数据,或者只是在之后格式化数据。我已经尝试过嵌入for循环,而loops、foreach和似乎无法正确实现。我还有一个实例化函数

private static function instantiate($record) {
    $class_name = get_called_class();
    $object     = new $class_name;

    foreach($record as $attribute=>$value) {
        if($object->has_attribute($attribute)) {
            $object->$attribute = $value;
        }
    }
    return $object;
}
哪个使用

private function has_attribute($attribute) {
    // get_object_vars returns an associative array with all attributes
    // (incl. private ones!) as the keys and their current values as the value
    $object_vars = get_object_vars($this);
    // we don't care about the value, we just want to know if the key exists
    // will return true or false
    return array_key_exists($attribute, $object_vars);  
}

,我试着用它们来处理数组,但也没有成功。我想这是因为如果这个拼图的开始部分有一块丢失了,那么其余的部分也不会拼在一起。我在这里不知所措。正如我之前所说的,我正在尝试将它们放入类变量中,这样我就可以对它们进行数学运算(例如,总计、平均值等)。这是我正在开发的预算应用程序。任何帮助都将不胜感激。即使这是一种完全不同的方式,对我来说可能不会那么混乱,我至少会尝试一下。如果您需要更多信息,请随时询问。我没有展示的两个函数几乎是不言自明的。再次感谢

这一代码更改应该会为您带来好处:

$date = array();
$gross= array();
// all the other columns.........like this

while($row = mysql_fetch_assoc($result)) {
    $date[] = $row['date'];
    $gross[] = $row['gross'];
    // other columns ...... like this
}

$filled_data =array ();
$filled_data[] =$date;
$filled_data[]=$gross;
// other arrays ...........

return $filled_data;

这一代码更改应该会为您带来好处:

$date = array();
$gross= array();
// all the other columns.........like this

while($row = mysql_fetch_assoc($result)) {
    $date[] = $row['date'];
    $gross[] = $row['gross'];
    // other columns ...... like this
}

$filled_data =array ();
$filled_data[] =$date;
$filled_data[]=$gross;
// other arrays ...........

return $filled_data;

提示:您不需要
$index++
破解。在PHP中,
$arr[]=$x
将在
$arr
中添加
$x
作为新元素。请不要使用
mysql.*
函数编写新代码。它们不再得到维护,社区已经开始。看到了吗?相反,你应该学习并使用或。如果你不能决定哪一个,我会帮你的。如果您选择PDO、.And,请停止在代码中使用全局状态。能否详细说明有关全局状态的注释?在本例中,它只是使用$database=newdatabaseobject的替代方法。不过,我对PHP还是相当陌生,所以我不知道其中的区别/风险。提示:您不需要
$index++
hack。在PHP中,
$arr[]=$x
将在
$arr
中添加
$x
作为新元素。请不要使用
mysql.*
函数编写新代码。它们不再得到维护,社区已经开始。看到了吗?相反,你应该学习并使用或。如果你不能决定哪一个,我会帮你的。如果您选择PDO、.And,请停止在代码中使用全局状态。能否详细说明有关全局状态的注释?在本例中,它只是使用$database=newdatabaseobject的替代方法。不过,我对PHP还是相当陌生,所以我不知道其中的区别/风险。在将其替换掉并将示例中的变量转换为类变量后,我一直得到一个“可捕获的致命错误:类检查的对象无法在第161行转换为字符串”。第161行恰好是“$filled_data[]=$this->gross;虽然我没有调用gross,但我调用的是net,我仍然在使用该函数。我不知道它为什么会抛出该错误。我看不到任何东西被放入具有该语法的字符串中,但我也不是专家。总之,我注释了该函数的整个$filter\u数据部分,并实际实例化了它这是我想要的所有值。老实说,我不确定函数的$filled_data部分的用途是什么,但如果您愿意详细说明,我相信我可以找到它的用途。顺便说一句,谢谢您的帮助,您是个救命恩人!!:)(抱歉,一条评论xD就太多了)@kyle填充的数据用于合并一个数组中的所有变量,以便从该函数轻松返回,仅此而已:)在将其交换并将示例中的变量转换为类变量后,我一直得到一个“可捕获的致命错误:类检查的对象无法在第161行转换为字符串”。第161行恰好是”$filled_data[]=$this->gross;尽管我不是在调用gross,而是在调用net,但我仍然在使用该函数。我不知道它为什么会说出那个错误。我看不出有任何东西是用这种语法放在字符串中的,但我也不是专家。总之,我注释掉了函数的整个$filter\u数据部分,实际上它以我想要的方式实例化了所有的值。老实说,我不确定函数的$filled_数据部分的用途是什么,但如果您愿意详细说明,我相信我可以找到它的用途。谢谢你的帮助顺便说一下,你是个救命恩人!!:)(抱歉,一条注释xD太多了)@kyle filled data用于合并一个数组中的所有变量,以便从该函数返回,仅此而已:)
$date = array();
$gross= array();
// all the other columns.........like this

while($row = mysql_fetch_assoc($result)) {
    $date[] = $row['date'];
    $gross[] = $row['gross'];
    // other columns ...... like this
}

$filled_data =array ();
$filled_data[] =$date;
$filled_data[]=$gross;
// other arrays ...........

return $filled_data;