将Mysql查询结果放入一个类中的数组中 [英] Put Mysql query result in array inside a class
问题描述
大家好,我试着解决我在这个主题中的问题 - > 从Prestashop自定义字段获取数据
我已经在我的类之外创建了一个MySql查询,现在它可以工作,但我需要将结果放入类中的数组中。我粘贴下面的代码:
$ servername =localhost;
$ username =username;
$ password =password;
$ dbname =dbname;
//创建连接
$ conn = new mysqli($ servername,$ username,$ password,$ dbname);
//检查连接
if($ conn-> connect_error){
die(Connection failed:。$ conn-> connect_error);
}
$ sql =SELECT codice_target FROM customer;
$ result = $ conn-> query($ sql);
$ b $ if($ result-> num_rows> 0){
//每行的输出数据
while($ row = $ result-> fetch_assoc()){
echoid:。 $行[ id_customer。 - Codice目标:。 $行[ codice_target]。 <峰; br> 中;
}
} else {
echo0 results;
}
$ conn-> close();
然后是类和函数我需要把结果从查询
class AdvancedExport extends Module
{
//这里的其他方法...
public function fputToFile($ file,$ allexportfields,$ object,$ ae)
{
if($ allexportfields&& $ file&& $ object&& $ ae )
{
//一个准备出口产品
$ readyForExport = array();
//输入正确的排序顺序
foreach($ allexportfields as $ value)
{
$ object = $ this-> processDecimalSettings( $ object,$ ae,$ value);
$ readyForExport [$ value] = iconv(UTF-8,$ ae-> charset,$ object [$ value]);
}
//需要将mysql查询结果放在$ readyForExport ['codice_target'];
$ this-> counter [$ readyForExport ['id_order']] =(!empty($ this-> counter [$ readyForExport ['id_order']]))? ++ $ this->计数器[$ readyForExport ['id_order']]:1; //试试这里
$ readyForExport ['orderLine'] = $ this->计数器[$ readyForExport ['id_order']]; //并且在这里尝试
// print_r('id_order被添加:'。$ readyForExport ['orderLine']); //查看是否添加了
// echo'< pre>'。 var_dump($ readyForExport)。 < /预>;
//修改=== 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->格式('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->格式('d / m / Y');
// ===数据发票
$ newDateInvoice =克隆$ newDateAdd;
$ readyForExport ['invoice_date'] = $ newDateAdd->格式('d / m / Y');
// scambio l'id customer con il codice_target
// $ readyForExport ['codice_target'] = 8989;
$ textTarget =(string)$ readyForExport ['codice_target'];
$ readyForExport ['id_customer'] = $ textTarget;
//客户编号
$ 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;
$ b $ index_arr = array(id_customer,date_add,ORCL,product_name,causale,product_quantity,product_price,delivery_date,id_order J, K, L, M, N, 订单行, 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;
//逐行写入csv
fputcsv($ file,$ readyForExport,$ ae->定界符,$ ae->分隔符);
}
}
非常感谢,这个目标。
数组值初始化为 $ rtoclass
类,然后通过它的方法传递给 AdvancedExport
对象 - $ obj - > set_arr_needed($ rtoclass);
在 $ obj-> fputToFile()
调用时,所需的数组将自动通过变量 $ arr_needed_in
。
尝试:
$ servername =localhost;
$ username =username;
$ password =password;
$ dbname =dbname;
//创建连接
$ conn = new mysqli($ servername,$ username,$ password,$ dbname);
//检查连接
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){
//每行输出数据
while($ row = $ result-> fetch_assoc()) {
echoid:。 $行[ id_customer。 - Codice目标:。 $行[ codice_target]。 <峰; br> 中;
$ rtoclass [] = $ row;
}
} else {
echo0 results;
}
$ obj = new AdvancedExport();
$ obj-> set_arr_needed($ rtoclass);
$ conn-> close();
对于课程:
class AdvancedExport extends Module
{
//此处的其他方法...
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();如果($ allexportfields&& $ file&& $ object&& $ ae)
{
// array needed needed within inside ..
一个准备出口产品
$ readyForExport = array();
//输入正确的排序顺序
foreach($ allexportfields as $ value)
{
$ object = $ this-> processDecimalSettings( $ object,$ ae,$ value);
$ readyForExport [$ value] = iconv(UTF-8,$ ae-> charset,$ object [$ value]);
}
//需要将mysql查询结果放在$ readyForExport ['codice_target'];
$ this-> counter [$ readyForExport ['id_order']] =(!empty($ this-> counter [$ readyForExport ['id_order']]))? ++ $ this->计数器[$ readyForExport ['id_order']]:1; //试试这里
$ readyForExport ['orderLine'] = $ this->计数器[$ readyForExport ['id_order']]; //并且在这里尝试
// print_r('id_order被添加:'。$ readyForExport ['orderLine']); //查看是否添加了
// echo'< pre>'。 var_dump($ readyForExport)。 < /预>;
//修改=== 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->格式('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->格式('d / m / Y');
// ===数据发票
$ newDateInvoice =克隆$ newDateAdd;
$ readyForExport ['invoice_date'] = $ newDateAdd->格式('d / m / Y');
// scambio l'id customer con il codice_target
// $ readyForExport ['codice_target'] = 8989;
$ textTarget =(string)$ readyForExport ['codice_target'];
$ readyForExport ['id_customer'] = $ textTarget;
//客户编号
$ 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;
$ b $ index_arr = array(id_customer,date_add,ORCL,product_name,causale,product_quantity,product_price,delivery_date,id_order J, K, L, M, N, 订单行, 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;
//逐行写入csv
fputcsv($ file,$ readyForExport,$ ae->定界符,$ ae->分隔符);
}
}
}
Hello everybody again I'm tryin to resolve my issue poste in this topic -> Fetch data from Prestashop custom field I've have made a MySql query outside my class and now it works but i need to put the result in an array inside the class. I paste my code below:
$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();
Then there is the class and the function i need to put the result from the query
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);
}
}
Thanks far all done until now, Im really close to the goal.
Array values initialized to $rtoclass
variable outside the class, then passed to an AdvancedExport
object through its method - $obj->set_arr_needed( $rtoclass );
On $obj->fputToFile()
call, the needed array will automatically be available for use inside it via the variable $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();
For the class:
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);
}
}
}
这篇关于将Mysql查询结果放入一个类中的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!