2015-08-16 152 views
2

我正在使用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'); 

在$ PROD_ID在while循環,我得到一個產品的所有的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 | 
+---+----------+--------------------+----------+--------+ 

這與ORDER_DETAILS表連接的,那麼我product_shop_tt表和產品表:

+--+------------+---------+-------+ 
|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 | 
+--+-------------------------+-------------------------+-----------------+---------+--------+ 

In $ 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文件中獲得正確的顯示。我爲每個產品獲得一個新的行,但我想要像第一張照片一樣。現在我將它作爲休耕地 enter image description here

+0

是否'$行[ 'PRODID']'包含正確的價值?您在SELECT語句中未選擇字段「prodID」。 –

+0

@ mario.klump我的不好。是的是的。在代碼中,我只是沒有複製它。問題是一樣的。 – Boky

+0

嘗試''echo出所有的SQL查詢並通過phpMyAdmin運行它們。如果他們工作,那麼問題可能在於您設置或連接變量的方式。 – Tim

回答

1

您的prodId是一個包含"id1\rid2\rid3"的字符串。如果全部替換"\r"的智慧", "並使用SQL IN()語句而不是=

替換:

WHERE producten.id = " . $prod_id . " 

有:

WHERE producten.id IN (" . str_replace("\r", ", ", $prod_id) . ") 
+0

我正在嘗試將所有內容導出到excel文件。我更新了我的問題。我添加了你的代碼,但現在我只在Excel文件中爲用戶提供了一個產品,而不必介意他有幾個。現在我在第二個while循環的** $ rowcounter **中遇到了問題。因爲看起來每個新產品都存儲在前一個產品中。 – Boky

+0

所以你只是改變你的問題,即使我給你一個解決方案?這個網站的作品是因爲人們得到正確答案如果您不斷更改問題而不是詢問新問題,則該網站無法按預期工作。至於你的新問題:祝你好運,但我只回答工作系統的人的問題,並讓我得到我應得的分數。 – Matijs

+0

但是你的代碼不工作。我更新了你的問題,因爲我想讓你看看我做了什麼。我沒有壞意圖。 – Boky