简体   繁体   中英

how to print a particular value from an array?

i want to print first value of id 219 is mentioned in the screenshot for case:downloadl then i need to print 2nd value id=219 for case :downloadp.but,the array $value['printp'] getting both the values into the table(ex:Select usera Select userb). but i need to print only one and first value for this $value['printp']. 在此处输入图片说明

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;

使用LIMIT 1

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

Use this query:

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

Here ORDER BY post_id will return you the output in increasing order (by default) of post_id

and LIMIT 1 will limit the number of rows returned to 1.

i think the problem is in the database,because your post_id is not an unique value,so you are pulling 2 or more values from the table. i would redesign the db, or you can use:

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

to pull a single value from db, or remove the

foreach($print as $value) 

from your code to print the first value.

Change your query in the different cases.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM