2017-06-01 85 views
0

我想打印的第一個值219的情況下截圖中提到:downloadl然後我需要打印第二個值id = 219的情況下:downloadp.but,數組$ value ['printp']得到兩個值放入表格中(例如:選擇usera選擇用戶b)。 但我需要打印這個$ value ['printp']只有一個和第一個值。 enter image description here如何從數組中打印特定值?

case 'downloadl': 


$sql = "SELECT post_id,printprocess,printsupply,printesb,printwork,printpri,printmate FROM printtable WHERE post_id=" .$post_id; 

     $query = $db->sql_query($sql); 
     $print_data = array(); 
     while($roww = mysqli_fetch_array($query)){ 

       $print_data[] = array(
       'printp' => $roww['printprocess'], 
       'prints' => $roww['printsupply'], 
       'printsb' => $roww['printesb'], 
       'printwrk' => $roww['printwork'], 
       'printpr' => $roww['printpri'], 
       'printmat' => $roww['printmate'], 
       ); 
     } 

       foreach($print_data as $value) { 

        $objPHPExcel->setActiveSheetIndex(0) 
         ->setCellValue('A'.$j, ''); 
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, ''); 
        $j++; 
        $objPHPExcel->setActiveSheetIndex(0) 
         ->setCellValue('A'.$j, 'Printing Process'); 
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printp']); 
        $j++; 
        $objPHPExcel->setActiveSheetIndex(0) 
         ->setCellValue('A'.$j, 'Supplier'); 
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['prints']); 
        $j++; 
        $objPHPExcel->setActiveSheetIndex(0) 
         ->setCellValue('A'.$j, 'Espon Sub'); 
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printsb']); 
        $j++; 
        $objPHPExcel->setActiveSheetIndex(0) 
         ->setCellValue('A'.$j, 'WorkFlow ID'); 
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printpr']); 
        $j++; 
        $objPHPExcel->setActiveSheetIndex(0) 
         ->setCellValue('A'.$j, 'Printer'); 
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printwrk']); 
        $j++; 
        $objPHPExcel->setActiveSheetIndex(0) 
         ->setCellValue('A'.$j, 'Printing Material'); 
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printmat']); 
        $i++; 
        $j++; 
       } 
break; 
       case:downloadp: 

      foreach($print_data as $value) { 

          $objPHPExcel->setActiveSheetIndex(0) 
           ->setCellValue('A'.$j, ''); 
          $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, ''); 
          $j++; 
          $objPHPExcel->setActiveSheetIndex(0) 
           ->setCellValue('A'.$j, 'Printing Process'); 
          $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printp']); 
          $j++; 
          $objPHPExcel->setActiveSheetIndex(0) 
           ->setCellValue('A'.$j, 'Supplier'); 
          $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['prints']); 
          $j++; 
          $objPHPExcel->setActiveSheetIndex(0) 
           ->setCellValue('A'.$j, 'Espon Sub'); 
          $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printsb']); 
          $j++; 
          $objPHPExcel->setActiveSheetIndex(0) 
           ->setCellValue('A'.$j, 'WorkFlow ID'); 
          $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printpr']); 
          $j++; 
          $objPHPExcel->setActiveSheetIndex(0) 
           ->setCellValue('A'.$j, 'Printer'); 
          $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printwrk']); 
          $j++; 
          $objPHPExcel->setActiveSheetIndex(0) 
           ->setCellValue('A'.$j, 'Printing Material'); 
          $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printmat']); 
          $i++; 
          $j++; 
         } 
     break; 
+0

您的查詢中沒有'ORDER BY',因此無法預測哪一個會優先。 – Barmar

+0

如果您只想打印一行,請去掉while循環。 – Barmar

+0

我編輯了我的問題@Barmar可以請你再讀一遍......! –

回答

1

在不同情況下更改您的查詢。

switch ($something) { 
    case 'downloadl': 
     $sql = "SELECT post_id,printprocess,printsupply,printesb,printwork,printpri,printmate FROM printtable WHERE post_id=" .$post_id . " AND printprocess = 'Select UserA'"; 
     break; 
    case 'downloadp': 
     $sql = "SELECT post_id,printprocess,printsupply,printesb,printwork,printpri,printmate FROM printtable WHERE post_id=" .$post_id . " AND printprocess = 'Select UserB'"; 
     break; 
} 

$query = $db->sql_query($sql); 
$print_data = array(); 
while($roww = mysqli_fetch_array($query)){ 

    $print_data[] = array(
    'printp' => $roww['printprocess'], 
    'prints' => $roww['printsupply'], 
    'printsb' => $roww['printesb'], 
    'printwrk' => $roww['printwork'], 
    'printpr' => $roww['printpri'], 
    'printmat' => $roww['printmate'], 
    ); 
} 

foreach($print_data as $value) { 

    $objPHPExcel->setActiveSheetIndex(0) 
     ->setCellValue('A'.$j, ''); 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, ''); 
    $j++; 
    $objPHPExcel->setActiveSheetIndex(0) 
     ->setCellValue('A'.$j, 'Printing Process'); 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printp']); 
    $j++; 
    $objPHPExcel->setActiveSheetIndex(0) 
     ->setCellValue('A'.$j, 'Supplier'); 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['prints']); 
    $j++; 
    $objPHPExcel->setActiveSheetIndex(0) 
     ->setCellValue('A'.$j, 'Espon Sub'); 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printsb']); 
    $j++; 
    $objPHPExcel->setActiveSheetIndex(0) 
     ->setCellValue('A'.$j, 'WorkFlow ID'); 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printpr']); 
    $j++; 
    $objPHPExcel->setActiveSheetIndex(0) 
     ->setCellValue('A'.$j, 'Printer'); 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printwrk']); 
    $j++; 
    $objPHPExcel->setActiveSheetIndex(0) 
     ->setCellValue('A'.$j, 'Printing Material'); 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $value['printmat']); 
    $i++; 
    $j++; 
} 
+0

你的情況2應該是'downloadp' – mrid

+0

感謝您的努力@Barmar,但我想打印這個printprocess(選擇UserA)動態,因爲它可能會改變任何時候當我改變數據庫。 ..我可以使用任何數組函數這是可能的..?? –

+0

@mrid ya downloadp是case2我提到case2而不是downloadp sry我的錯誤\ –

1

使用LIMIT 1

SELECT post_id,printprocess,printsupply,printesb,printwork,printpri,printmate FROM printtable WHERE post_id=" .$post_id." LIMIT 1"; 
+1

MySQL沒有'TOP 1'它使用'LIMIT 1' – Barmar

+0

謝謝。 @Barmar –

+0

他改變了這個問題。他希望在不同的'case'塊中有不同的行。 – Barmar

1

使用此查詢:

$sql = "SELECT post_id,printprocess,printsupply,printesb,printwork,printpri,printmate FROM printtable WHERE post_id=" .$post_id . " ORDER BY pid LIMIT 1"; 

這裏ORDER BY post_id將返回在POST_ID的升序(默認)輸出

LIMIT 1將限制行數返回到1.

+0

他只選擇一個'post_id',爲什麼你需要按同一個列排序? – Barmar

+0

謝謝你指出,我的壞。必須通過pid(似乎是自動生成的主鍵)而不是post_id進行排序。更新了我的答案。 – mrid

+0

你見過這個問題的編輯嗎?他希望在'case'下載''中選擇UserA'並在'case'下載''中選擇UserB'。 – Barmar

1

我認爲問題出在數據庫中,因爲您的post_id不是唯一值,所以您需要從表中提取2個或更多值。我會重新設計數據庫,或者您可以使用:

$sql = "SELECT post_id,printprocess,printsupply,printesb,printwork,printpri,printmate FROM printtable WHERE post_id=" .$post_id LIMIT 1; 

從資料庫拉一個值,或者從你的代碼中刪除

foreach($print as $value) 

打印的第一個值。