如何从数组中打印特定值?

时间:2017-06-01 10:12:04

标签: php mysql arrays

我想打印第一个id为219的值在屏幕截图中提到了case:downloadl然后我需要打印第二个值id = 219的情况:downloadp.but,数组$ value ['printp']得到两个表中的值(例如:选择usera选择userb)。            但是我需要为这个$ 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;

4 个答案:

答案 0 :(得分:1)

使用 LIMIT 1

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

答案 1 :(得分: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。

答案 2 :(得分:1)

我认为问题出在数据库中,因为你的post_id不是唯一值,所以你从表中提取了2个或更多的值。我会重新设计数据库,或者您可以使用:

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

从db中提取单个值,或删除

foreach($print as $value) 

从您的代码中打印第一个值。

答案 3 :(得分: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++;
}