将Mysql查询结果放入类内的数组中

2024-02-10

大家好,我正在尝试解决本主题中的问题 ->从 Prestashop 自定义字段获取数据 https://stackoverflow.com/questions/49487896/fetch-data-from-prestashop-custom-field我已经在类外进行了 MySql 查询,现在它可以工作,但我需要将结果放入类内的数组中。我将我的代码粘贴到下面:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT codice_target FROM customer";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id_customer"]. " - Codice target: " . $row["codice_target"]."<br>";
    }
} else {
    echo "0 results";
}
$conn->close();

然后是我需要放置查询结果的类和函数

class AdvancedExport extends Module
{

//other methods here...

        public function fputToFile($file, $allexportfields, $object, $ae)
        {
            if($allexportfields && $file && $object && $ae)
            {
                //one ready for export product
                $readyForExport = array();


                //put in correct sort order
                foreach ($allexportfields as $value)
                {
                    $object = $this->processDecimalSettings($object, $ae, $value);
                    $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);

                }

                // need to put mysql query result here inside $readyForExport['codice_target'];

                $this->counter[$readyForExport['id_order']] = (!empty($this->counter[$readyForExport['id_order']])) ? ++$this->counter[$readyForExport['id_order']] : 1; // try here
                $readyForExport['orderLine'] = $this->counter[$readyForExport['id_order']]; // and try here

                //print_r('The id_order is added: ' . $readyForExport['orderLine']); // see if it is added

                //echo '<pre>' . var_dump($readyForExport) . '</pre>';

                // modifiche === Dario === prezzo
                $newPrice = substr($readyForExport['product_price'], 0, strpos($readyForExport['product_price'], "."));
                $readyForExport['product_price'] = $newPrice;

                // === data

                $newDateAdd = new DateTime($readyForExport['date_add']);
                $readyForExport['date_add'] = $newDateAdd->format('d/m/Y');


                // aggiungo 21 giorni - 3 settimane - alla data di acquisto
                $date_mod = clone $newDateAdd;
                $date_mod->add(new DateInterval('P21D'));
                $readyForExport['delivery_date'] = $date_mod->format('d/m/Y');

                // === data invoice
                $newDateInvoice = clone $newDateAdd;
                $readyForExport['invoice_date'] = $newDateAdd->format('d/m/Y');

                //scambio l'id customer con il codice_target

                //$readyForExport['codice_target'] = 8989;

                $textTarget = (string)$readyForExport['codice_target']; 

                $readyForExport['id_customer'] = $textTarget;

                // aggiungo gli zeri davanti al customer id
                $id_count = strlen($readyForExport['id_customer']);            
                if ($id_count == 1) {

                    $newCustomer = "0000000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 2) {

                    $newCustomer = "000000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 3) {

                    $newCustomer = "00000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 4) {
                    $newCustomer = "0000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 5) {
                    $newCustomer = "000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 6) {
                    $newCustomer = "00".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }

                // elaboro lo SKU

                $textSku = (string)$readyForExport['product_name']; 

                $newSku_1 = $readyForExport['product_name'];

                $newSku_1 = substr($newSku_1,0,4);
                $newSku_2 = "/".substr($textSku,-4,4);

                $newSku_tot = $newSku_1.$newSku_2; 

                $newSku_tot = str_replace(' ', '', $newSku_tot);
                $newSku_tot = str_replace('-', '', $newSku_tot);
                $newSku_tot = str_replace('co', '', $newSku_tot);

                $newSku_tot = str_replace('e', '', $newSku_tot);
                $newSku_tot = str_replace('r', '', $newSku_tot);

                $readyForExport['product_name'] = $newSku_tot;



                // aggiungo un campo fisso
                $readyForExport['causale'] = "NR";

                // aggiungo un campo fisso
                $readyForExport['ORCL'] = "ORCL";

                //$readyForExport['G'] = "";
                $readyForExport['J'] = "";
                $readyForExport['K'] = "";
                $readyForExport['L'] = "";
                $readyForExport['M'] = "";
                $readyForExport['N'] = "";
                $readyForExport['P'] = "";
                $readyForExport['Q'] = "";
                $readyForExport['R'] = "30";

                $index_arr=array("id_customer","date_add","ORCL","product_name","causale","product_quantity","product_price","delivery_date","id_order","J","K","L","M","N","orderLine","P","Q","R");

                //riordino i campi in base a come li dispongo nella variabile $index_arr
                $arr_t=array();
                foreach($index_arr as $i=>$v) {
                    foreach($readyForExport as $k=>$b) {
                        if ($k==$v) $arr_t[$k]=$b;
                    }
                }
                $readyForExport=$arr_t;

                //write into csv line by line
                fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator);
            }
        }

感谢到目前为止所做的一切,我已经非常接近目标了。


数组值初始化为$rtoclass类外部的变量,然后传递给AdvancedExport对象通过其方法 - $obj->set_arr_needed( $rtoclass );

On $obj->fputToFile()调用时,所需的数组将自动可通过变量在其中使用$arr_needed_in.

Try:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT codice_target FROM customer";
$result = $conn->query($sql);
$rtoclass = array();

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id_customer"]. " - Codice target: " . $row["codice_target"]."<br>";
        $rtoclass[] = $row;
    }
} else {
    echo "0 results";
}

$obj = new AdvancedExport();
$obj->set_arr_needed( $rtoclass );

$conn->close();

对于班级:

class AdvancedExport extends Module
{

        //other methods here...

        private $arr_needed = array(); 

        public function set_arr_needed( $arr ) {    
            $this->arr_needed = $arr;
        }

        public function get_arr_needed() {  
            return $this->arr_needed;
        }

        public function fputToFile($file, $allexportfields, $object, $ae)
        {
            $arr_needed_in = $this->get_arr_needed(); // array needed already inside..

            if($allexportfields && $file && $object && $ae)
            {
                //one ready for export product
                $readyForExport = array();


                //put in correct sort order
                foreach ($allexportfields as $value)
                {
                    $object = $this->processDecimalSettings($object, $ae, $value);
                    $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);

                }

                // need to put mysql query result here inside $readyForExport['codice_target'];

                $this->counter[$readyForExport['id_order']] = (!empty($this->counter[$readyForExport['id_order']])) ? ++$this->counter[$readyForExport['id_order']] : 1; // try here
                $readyForExport['orderLine'] = $this->counter[$readyForExport['id_order']]; // and try here

                //print_r('The id_order is added: ' . $readyForExport['orderLine']); // see if it is added

                //echo '<pre>' . var_dump($readyForExport) . '</pre>';

                // modifiche === Dario === prezzo
                $newPrice = substr($readyForExport['product_price'], 0, strpos($readyForExport['product_price'], "."));
                $readyForExport['product_price'] = $newPrice;

                // === data

                $newDateAdd = new DateTime($readyForExport['date_add']);
                $readyForExport['date_add'] = $newDateAdd->format('d/m/Y');


                // aggiungo 21 giorni - 3 settimane - alla data di acquisto
                $date_mod = clone $newDateAdd;
                $date_mod->add(new DateInterval('P21D'));
                $readyForExport['delivery_date'] = $date_mod->format('d/m/Y');

                // === data invoice
                $newDateInvoice = clone $newDateAdd;
                $readyForExport['invoice_date'] = $newDateAdd->format('d/m/Y');

                //scambio l'id customer con il codice_target

                //$readyForExport['codice_target'] = 8989;

                $textTarget = (string)$readyForExport['codice_target']; 

                $readyForExport['id_customer'] = $textTarget;

                // aggiungo gli zeri davanti al customer id
                $id_count = strlen($readyForExport['id_customer']);            
                if ($id_count == 1) {

                    $newCustomer = "0000000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 2) {

                    $newCustomer = "000000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 3) {

                    $newCustomer = "00000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 4) {
                    $newCustomer = "0000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 5) {
                    $newCustomer = "000".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }elseif ($id_count == 6) {
                    $newCustomer = "00".$readyForExport['id_customer'];
                    $readyForExport['id_customer'] = $newCustomer;

                }

                // elaboro lo SKU

                $textSku = (string)$readyForExport['product_name']; 

                $newSku_1 = $readyForExport['product_name'];

                $newSku_1 = substr($newSku_1,0,4);
                $newSku_2 = "/".substr($textSku,-4,4);

                $newSku_tot = $newSku_1.$newSku_2; 

                $newSku_tot = str_replace(' ', '', $newSku_tot);
                $newSku_tot = str_replace('-', '', $newSku_tot);
                $newSku_tot = str_replace('co', '', $newSku_tot);

                $newSku_tot = str_replace('e', '', $newSku_tot);
                $newSku_tot = str_replace('r', '', $newSku_tot);

                $readyForExport['product_name'] = $newSku_tot;



                // aggiungo un campo fisso
                $readyForExport['causale'] = "NR";

                // aggiungo un campo fisso
                $readyForExport['ORCL'] = "ORCL";

                //$readyForExport['G'] = "";
                $readyForExport['J'] = "";
                $readyForExport['K'] = "";
                $readyForExport['L'] = "";
                $readyForExport['M'] = "";
                $readyForExport['N'] = "";
                $readyForExport['P'] = "";
                $readyForExport['Q'] = "";
                $readyForExport['R'] = "30";

                $index_arr=array("id_customer","date_add","ORCL","product_name","causale","product_quantity","product_price","delivery_date","id_order","J","K","L","M","N","orderLine","P","Q","R");

                //riordino i campi in base a come li dispongo nella variabile $index_arr
                $arr_t=array();
                foreach($index_arr as $i=>$v) {
                    foreach($readyForExport as $k=>$b) {
                        if ($k==$v) $arr_t[$k]=$b;
                    }
                }
                $readyForExport=$arr_t;

                //write into csv line by line
                fputcsv($file, $readyForExport, $ae->delimiter, $ae->separator);
            }
        }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将Mysql查询结果放入类内的数组中 的相关文章

随机推荐

  • 使用 stat_summary_hex 以离散色标显示最常见的值

    我有一个包含 10k 行和 3 列的数据框 xpos ypos 和簇 簇是从 0 到 9 的数字 http pastebin com NyQw29tb http pastebin com NyQw29tb 我想显示一个六边形图 其中每个六边
  • @Async不会通过@ControllerAdvice调用全局异常

    我有一个带有 Async 方法的服务类 如果它调用方法抛出任何异常 那么 ControllerAdvice 将不会调用全局异常处理 但对于其他课程和服务 它会正确拨打建议并发送电子邮件 Service public class FileSc
  • 如何在 CentOS 中向 PHP 5 添加curl 支持

    如何在 CentOS 中向 PHP 5 添加curl 支持 安装curl和curl devel后 我需要做哪些事情才能在PHP 5中设置curl 有同样的问题 安装 php common 对我有用 yum install php commo
  • 如何在 Internet 上托管 wcf 服务?

    这可能是一个基本的网络问题 但我对这个东西很陌生 只是不知道答案 我写了一个wcf服务和客户端 当我将计算机的网络 IP 地址作为端点地址并从同一台计算机运行客户端和服务器时 我可以使用 http 绑定之一并使服务正常工作 现在 我希望能够
  • Python GUI (glade) 显示 shell 进程的输出

    我正在编写一个 python 应用程序 它使用 subprocess Popen 对象运行多个子进程 我有一个 Glade GUI 想要在 GUI 中实时显示这些命令的输出 在 subprocess Popen 中运行 谁能建议一种方法来做
  • 使用await时线程返回线程池

    但是 使用 ASP NET Web Api 如果您的请求来自某一 线程 然后您等待某个函数并调用ConfigureAwait false 这可能会让你在不同的线程上 返回 ApiController 函数的最终结果 其实 只是做一个awai
  • 如果输入和复选框不为空,则启用提交按钮

    下面我试图确保在允许用户提交之前我的复选框和输入都已填充 它忽略我对复选框的检查 并仅在填写字段后打开提交 我究竟做错了什么 first last pass bind keyup function if allFilled register
  • 如何修复 Android 应用中 X509TrustManager 的不安全实现

    Google 通知我的 Android 应用程序中的 X509TrustManager 接口实现不安全 需要按如下方式更改代码 要正确处理 SSL 证书验证 请更改中的代码 自定义 X509TrustManager 接口的 checkSer
  • “ValueError:期望来自 tf.keras.Input() 的 KerasTensor”。使用 dropout 函数进行预测时出现错误

    我试图在测试期间使用 Dropout 来预测回归问题的不确定性亚林 加尔的文章 https www cs ox ac uk people yarin gal website blog 3d801aa532c1ce html 我使用 Kera
  • Google Cloud Dataprep 可以监控新文件的 GCS 路径吗?

    Google Cloud Dataprep 看起来很棒 我们已经用它来手动导入静态数据集 但是我想多次执行它 以便它可以使用上传到 GCS 路径的新文件 我可以看到您可以为 Dataprep 设置计划 但我在导入设置中看不到它将如何处理新文
  • 为什么用具有共同祖先的菱形案例来解释Java多重继承问题,而不是两个不相关的父类?

    这个问题对于 Java 人来说可能听起来很奇怪 但如果你尝试解释一下 那就太好了 这几天我正在理清Java的一些非常基础的概念 所以我来到了Java的继承和接口主题 在阅读本文时 我发现Java不支持多重继承 并且也理解了这一点 我无法理解
  • Slack 支持 Markdown 表格吗?

    我想将 Markdown 表发送到 SlackpostMessageAPI 但我在 Slack 中获得了原始内容 而不是渲染的表格 Slack 支持 Markdown 表格吗 还有其他方法可以在 Slack 中呈现表格数据吗 我知道 Sla
  • 选择要使用 Hspec 和堆栈运行的测试

    我编写了一系列测试 使用自动规格发现 http hspec github io hspec discover htmlHspec 的特点 我也在用stack https docs haskellstack org en stable REA
  • App Engine 忽略目录的符号链接

    我正在创建一个在 Google App Engine 上使用自定义 Flex 环境运行的应用程序 该应用程序使用多个 相对 符号链接指向项目中的其他目录 但不知何故 当我部署应用程序时 这些符号链接被忽略 看来gcloud工具在构建和部署应
  • 在 Android 中为 startActivityForResult() 生成 16 位唯一 ID

    我计划将生成的资源 ID 用于我的所有资源startActivityForResult 代码 以便我可以使用onActivityResult 在基类中 不必担心派生类是否使用相同的代码 不幸的是 代码似乎被限制为 16 位 而资源 ID 为
  • 如何在ionic 1中输入数字类型时只允许一位小数点

    我正在使用数字类型的输入 其中允许多个小数点 因此我尝试使用正则表达式不允许超过一个小数点 但即使在使用正则表达式之后 我也面临同样的问题 任何人都可以告诉我如何只允许ionic1 中数字类型输入中的一位小数 Html
  • 停止在提交表单时添加 URL 参数

    好吧 这可能是一个愚蠢的问题 但我正在尝试编写一个简单的 JavaScript 应用程序 请原谅我的术语 我对此很陌生 其中包含一个表单 但我只使用 HTML 来完成它和 JS 和 CSS 所以我使用 document getElement
  • didUpdateLocations 方法从未被调用

    我正在 iphone sdk4 0 上制作一个应用程序 其中更新位置方法从未被调用 我在下面给出了我的代码 请帮忙 提前致谢 id init super init obj UIApplication sharedApplication de
  • 检查 SQL 中的 GUID 是否为空

    如何检查存储过程中的参数是否为空 GUID SELECT CAST CAST 0 AS BINARY AS UNIQUEIDENTIFIER 那应该返回你的空指南 或者更短 节省一个演员 SELECT CAST 0x0 AS UNIQUEI
  • 将Mysql查询结果放入类内的数组中

    大家好 我正在尝试解决本主题中的问题 gt 从 Prestashop 自定义字段获取数据 https stackoverflow com questions 49487896 fetch data from prestashop custo