我正在使用phpexcel,但是从数据库中获取结果我遇到了问题。
我有下一个代码:
require_once 'PHPExcel/Classes/PHPExcel.php';
include "PHPExcel/Classes/PHPExcel/Writer/Excel2007.php";
session_start();
include("config.php");
global $kon;
ob_start();
$excel = new PHPExcel;
$excel->getProperties()->setCreator('Boris Jelic');
$excel->getProperties()->setLastModifiedBy('Boris Jelic');
$excel->getProperties()->setTitle('Orders');
$excel->removeSheetByIndex(0);
$cols = array('tijd' => 'A', 'shop' => 'B', 'products' => 'C', 'naam' => 'D', 'adres' => 'E', 'gemeente' => 'F', 'telefoonnummer' => 'G', 'email' => 'H', 'leeggoed' => 'I');
$list = $excel->createSheet();
$list->setTitle('Users');
$list->getColumnDimension('A')->setWidth(20);
$list->getColumnDimension('B')->setWidth(25);
$list->getColumnDimension('C')->setWidth(40);
$list->getColumnDimension('D')->setWidth(40);
$list->getColumnDimension('E')->setWidth(40);
$list->getColumnDimension('F')->setWidth(20);
$list->getColumnDimension('G')->setWidth(15);
$list->getColumnDimension('H')->setWidth(40);
$list->getColumnDimension('I')->setWidth(40);
$list->setCellValue('A1', 'Tijd');
$list->setCellValue('B1', 'Shop');
$list->setCellValue('C1', 'Products');
$list->setCellValue('D1', 'Naam en voornaam');
$list->setCellValue('E1', 'Adres');
$list->setCellValue('F1', 'Gemeente');
$list->setCellValue('G1', 'Telefoonnummer');
$list->setCellValue('H1', 'Email');
$list->setCellValue('I1', 'Leeggoed');
//za background
$excel->getActiveSheet()->getStyle('A1:I1')->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('FFE8E5E5');
//stavljamo naslove bold
$excel->getActiveSheet()->getStyle('A1:I1')->getFont()->setBold(true);
//povecavamo velicinu slova
$excel->getActiveSheet()->getStyle('A1:I1')->getFont()->setSize(13);
//moramo prvo uzeti sve orders sa statusom 1
$rezOrders = mysqli_query($kon, "SELECT
shops.naam as shopNaam,
GROUP_CONCAT(producten.naam SEPARATOR '\r') as prodNaam, GROUP_CONCAT(producten.id SEPARATOR '\r') as prodID,
order_details.aantal as kolicina, order_details.leeggoed as leeggoed, order_details.id as ordDetId, SUM(order_details.aantal) as prodCount,
korisnici.ime as korNaam, korisnici.email as korMail, korisnici.id as korId, korisnici.prezime as korPrez, korisnici.mobitelBroj as broj, korisnici.mjesto as adres, korisnici.huisnummer as hn,
korisnici.gemeente as gemeente,
orders.besteld_op as tijd
FROM orders
INNER JOIN korisnici ON korisnici.id = orders.user_id
INNER JOIN order_details ON order_details.order_id = orders.id
INNER JOIN product_shop_tt ON order_details.product_shop_tt_id = product_shop_tt.id
INNER JOIN producten ON producten.id = product_shop_tt.product_id
INNER JOIN shops ON shops.id = product_shop_tt.shop_id
WHERE orders.status = 1 GROUP BY korisnici.id");
$rowcounter = 2;
while ($row = mysqli_fetch_assoc($rezOrders)){
$prod_id = $row['prodID'];
$rez = mysqli_query(
$kon,
"
SELECT producten.naam as prodNaam
FROM producten
INNER JOIN product_shop_tt ON product_shop_tt.product_id = producten.id
INNER JOIN order_details ON order_details.product_shop_tt_id = product_shop_tt.id
WHERE producten.id IN (" . str_replace("\r", ", ", $prod_id) . ")
"
);
/*echo "Product i kolicina : " . $redAan["productNaam"] . " - " . $redAan["kolicina"] . "<br />";*/
$tijd = $row["tijd"];
$ime = $row["korNaam"] . " " . $row["korPrez"];
$email = $row["korMail"];
$telNummer = $row["broj"];
$gemeente = $row["gemeente"];
$adresa = $row["adres"] . " " . $row["hn"];
$leegoed = $row["leeggoed"];
$list->setCellValue('A'.$rowcounter, $tijd);
$list->setCellValue('B'.$rowcounter, $row['shopNaam']);
while($red = mysqli_fetch_assoc($rez)){
echo $red["prodNaam"] . "<br />";
$list->setCellValue('C'.$rowcounter, $red["prodNaam"]."\r");
$list->getStyle('C'.$rowcounter)->getAlignment()->setWrapText(true);
}
$list->setCellValue('D'.$rowcounter, $ime);
$list->setCellValue('E'.$rowcounter, $adresa);
$list->setCellValue('F'.$rowcounter, $gemeente);
$list->setCellValue('G'.$rowcounter, $telNummer);
$list->setCellValue('H'.$rowcounter, $email);
$list->setCellValue('I'.$rowcounter, $leegoed);
$rowcounter++;
}
$writer = new PHPExcel_Writer_Excel2007($excel);
$writer->save('files/users.xlsx');
在while循环中的$ prod_id中,我得到了产品的所有ID,但因为我在查询中使用GROUP_CONCAT(我使用它,因为我需要在我的excel文件中显示)我不知道单独获取它们,但分组。而现在我无法获得这些产品的数量。
我在数据库中的表格如下:
Order_details表(我需要的数量( aantal ))
+---+----------+--------------------+----------+--------+
|id | order_id | product_shop_tt_id | leeggoed | aantal |
+---+----------+--------------------+----------+--------+
|180| 87 | 51 | | 2 |
|181| 87 | 52 | | 3 |
|182| 88 | 32 | | 2 |
|183| 88 | 35 | | 5 |
|184| 88 | 55 | | 3 |
+---+----------+--------------------+----------+--------+
然后我的product_shop_tt表与order_details表和products表连接:
+--+------------+---------+-------+
|id| product_id | shop_id | price |
+--+------------+---------+-------+
|32| 1 | 20 | 25.8 |
|33| 5 | 20 | 13.6 |
|34| 4 | 18 | |
|35| 26 | 20 | |
|36| 27 | 18 | |
+--+------------+---------+-------+
最后我的产品表:
+--+-------------------------+-------------------------+-----------------+---------+--------+
|id| naam | details | keywords | type_id | active |
+--+-------------------------+-------------------------+-----------------+---------+--------+
| 1| Test promjena | Ovdje idu detalji | Ovo su keywords | 2 | 1 |
| 4| Test | Ovo je opis proizvoda:) | rijec. rijec1 | 2 | 1 |
| 5| ime_proizvoda | detalji | keywords | 2 | 1 |
|26| neko meso recimo (50gr) | | | 2 | 0 |
+--+-------------------------+-------------------------+-----------------+---------+--------+
在$ red中我没有取任何东西,因为我的$ prod_id被分组了。如何单独获取产品的所有ID,但我的$ rezOrders查询必须给出相同的结果?
我在excel文件中得到的结果如下(这是我想要的结果):
+---------------------+-------------------------+-------------------------+------------------+-------------------+----------+----------------+
| Tijd | Shop | Products | Naam en voornaam | Adres | Gemeente | Telefoonnummer |
+---------------------+-------------------------+-------------------------+------------------+-------------------+----------+----------------+
| 2015-08-15 14:11:56 | Bakkerìj Van Hecke | Test promjena | Borís Jelic | Krijgsgraat 130 | Gent | 486386069 |
| | neko meso recimo (50gr) | | | | | |
| | buffon | | | | | |
+---------------------+-------------------------+-------------------------+------------------+-------------------+----------+----------------+
| 2015-08-15 14:11:14 | Lidl | prod1 | Mesanovic Maida | Krihuisstraat 130 | Brussel | 486742185 |
| | | prod2 | | | | |
+---------------------+-------------------------+-------------------------+------------------+-------------------+----------+----------------+
我要做的是在每个产品旁边写下他的数量,该数量位于order_details表中。
当我更改我的第一个查询GROUB BY order_details.id然后我可以分别从每个产品的order_details获取数量值,但现在我没有在我的excel文件中得到正确的显示。我为每个产品添加了一个新行,但我想在第一张照片上得到它。现在我把它作为休耕
答案 0 :(得分:1)
您的prodId是一个包含"id1\rid2\rid3"
的字符串。如果替换所有"\r"
的智慧", "
并使用SQL IN ()
语句而不是=
。
替换:
WHERE producten.id = " . $prod_id . "
使用:
WHERE producten.id IN (" . str_replace("\r", ", ", $prod_id) . ")