gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:42:16 24 4
gpt4 key购买 nike

<分区>

好的,我正在尝试通过 user_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   32091812   1   2012-08-16  433.11  353.09  39.44   15.61   18.19   0.00    6.28    0.00    K-VA-TT 3230201

(kind of clustered, but you get the point. I had a picture, but it wouldn't let me post it..)

Okay, now the code I'm using to call the data into PHP.

public function get_field($user) {
global $database;
$result = $database->query("SELECT * FROM ".static::$tablename." WHERE user_id=".$user);
$field_data = array();
$index = 0;
while($row = mysql_fetch_assoc($result)) {
$field_data[$index] = $row;
$index++;
}
return $field_data;
}

正如你们中的许多人可能已经猜到的那样,我是如何从上述函数接收数据的。

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 ) 

I would like that to end up in my class like

public date = array(2012-07-26, 2012-08-16);
public $gross = array(434.88, 433.11)
ect..

我不知道我是否需要以不同的方式检索数据,或者只是在之后对其进行格式化。我试过嵌入 for 循环、while 循环、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);
}

,我试过弄乱那些让它们与数组一起工作,但也没有运气。我想那是因为如果这个拼图的开始部分丢失了,其余部分也不会拼在一起。我在这里不知所措。正如我之前所说,我正在尝试将它们放入类变量中,以便我可以对它们执行数学运算(即总计、平均值等)。这是我正在开发的预算应用程序。任何帮助将不胜感激。即使这是一种完全不同的方法,可能不会让我感到困惑,但我至少会试一试。如果您需要更多信息,请随时询问。我没有展示的两个功能几乎是不言自明的。再次感谢!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com