繁体   English   中英

如何设置多个值来分配会话

[英]How to set the multiple values to assign the session

我将在一个表中编写一个代码,while 循环输出将显示两个值,但我会将这些值分配给会话最后一条记录,仅采用如何从 while 循环分配两个值。

代码看起来像这样

<?php  $sql="SELECT Add_Price,sub_category from `consumerprice` where Add_Size='8 Guage' and add_MM='4 MM'";
     $res=$conn->query($sql);
     $count=$res->num_rows;
     $i=1;

     while ($row = $res->fetch_assoc()){
              $totalchain = 'Hole Sizes: '.$subcar.'
                 <table style="width:100%">
                    <tr>
                    <th>2</th>
                    <th>2 1/2</th>
                    <th>3</th>
                    <th>4</th>
                    </tr>
                    <tr>
                    <td>'.$multi.' kgs</td>
                    <td>'.$multi2.' kgs</td>
                    <td>'.$multi3.' kgs</td>
                    <td>'.$multi4.' kgs</td>
                    </tr>
                    <tr>
                    <td><i class="fa fa-inr"> </i>'.$multi * $price .'</td>
                    <td><i class="fa fa-inr"> </i>'.$multi2 * $price .'</td>
                    <td><i class="fa fa-inr"> </i>'.$multi3 * $price .'</td>
                    <td><i class="fa fa-inr"> </i>'.$multi4 * $price .'</td>
                    </tr>
                 </table>';

$_SESSION['totalchain'] = $totalchain; 打印 $totalchain 最后一条记录时将显示。

输出看起来像这只是最后一条记录,但将显示所有记录。

方法一:

使用array_push您可以实现结果。

<?php  
     $sql="SELECT Add_Price,sub_category from `consumerprice` where Add_Size='8 Guage' and add_MM='4 MM'";
     $res=$conn->query($sql);
     $count=$res->num_rows;
     $i=1;
     $consumerprice_data_html=array();
     while ($row = $res->fetch_assoc()){
        $totalchain = 'Hole Sizes: '.$subcar.'
           <table style="width:100%">
              <tr>
              <th>2</th>
              <th>2 1/2</th>
              <th>3</th>
              <th>4</th>
              </tr>
              <tr>
              <td>'.$multi.' kgs</td>
              <td>'.$multi2.' kgs</td>
              <td>'.$multi3.' kgs</td>
              <td>'.$multi4.' kgs</td>
              </tr>
              <tr>
              <td><i class="fa fa-inr"> </i>'.$multi * $price .'</td>
              <td><i class="fa fa-inr"> </i>'.$multi2 * $price .'</td>
              <td><i class="fa fa-inr"> </i>'.$multi3 * $price .'</td>
              <td><i class="fa fa-inr"> </i>'.$multi4 * $price .'</td>
              </tr>
           </table>';
       array_push($consumerprice_data_html, $totalchain);
    }

   $_SESSION['consumerprice_data_html']=$consumerprice_data_html;
   foreach($_SESSION['consumerprice_data_html'] as $k => $data) {
      echo $data;
   }

方法二:

使用字符串连接-

<?php  
     $sql="SELECT Add_Price,sub_category from `consumerprice` where Add_Size='8 Guage' and add_MM='4 MM'";
     $res=$conn->query($sql);
     $count=$res->num_rows;
     $i=1;
     $consumerprice_data_html_string='';
     while ($row = $res->fetch_assoc()){
        $totalchain = 'Hole Sizes: '.$subcar.'
           <table style="width:100%">
              <tr>
              <th>2</th>
              <th>2 1/2</th>
              <th>3</th>
              <th>4</th>
              </tr>
              <tr>
              <td>'.$multi.' kgs</td>
              <td>'.$multi2.' kgs</td>
              <td>'.$multi3.' kgs</td>
              <td>'.$multi4.' kgs</td>
              </tr>
              <tr>
              <td><i class="fa fa-inr"> </i>'.$multi * $price .'</td>
              <td><i class="fa fa-inr"> </i>'.$multi2 * $price .'</td>
              <td><i class="fa fa-inr"> </i>'.$multi3 * $price .'</td>
              <td><i class="fa fa-inr"> </i>'.$multi4 * $price .'</td>
              </tr>
           </table>';
       $consumerprice_data_html_string=$consumerprice_data_html_string+$totalchain;
    }

  echo $consumerprice_data_html_string;

一旦检查这个会话值,就会使用 json 编码分配多个数据

$sql="SELECT Add_Price,sub_category from `consumerprice` where Add_Size='8 Gauge' and add_MM='4 MM'";
     $res=$conn->query($sql);
     $count=$res->num_rows;
     $i=1;
     while ($row = $res->fetch_assoc()){
            $price=$row['Add_Price'];
            if($price!='')
            {
            $category = $row['sub_category'];

            if($category!= '1' && $category!= '2' && $category!= '5' && $category!= '8')
            {
                 $sql2 = "SELECT * FROM `sub_cat` where sub_id = '$category'";
                 $res2= $conn->query($sql2);
                 while ($row2 = $res2->fetch_assoc()) {

                     $subcar = $row2['sub_category']; }

                     $output .= "<table style=\"width:100%\">
                 <tr>
                   <th>¾</th>
                    <th>1</th>
                    <th>1 ¼</th>
                    <th>1 ½</th>
                 </tr>";

                     $chainmulti1= ($height*$length*0.150);
                     $multi = round($chainmulti1);
                     $two = $multi * $price;
                     $two = number_format($two);

                     $chainmulti2= ($height*$length*0.134);
                     $multi2 = round($chainmulti2);
                     $twof = $multi2 * $price;
                     $twof = number_format($twof);

                     $chainmulti3= ($height*$length*0.109);
                     $multi3 = round($chainmulti3);
                     $three = $multi3 * $price;
                     $three = number_format($three);

                     $chainmulti4= ($height*$length*0.085);
                     $multi4 = round($chainmulti4);
                     $four = $multi4 * $price;
                     $four = number_format($four);

                     $output .= 'Hole Sizes: '.$subcar.'
                        <tr>
                        <td>'.$multi.' kgs</td>
                        <td>'.$multi2.' kgs</td>
                        <td>'.$multi3.' kgs</td>
                        <td>'.$multi4.' kgs</td>
                        </tr>
                        <tr>
                        <td><i class="fa fa-inr">&nbsp;</i>&nbsp;'.$two.'/-</td>
                        <td><i class="fa fa-inr">&nbsp;</i>&nbsp;'.$twof.'/-</td>
                        <td><i class="fa fa-inr">&nbsp;</i>&nbsp;'.$three.'/-</td>
                        <td><i class="fa fa-inr">&nbsp;</i>&nbsp;'.$four.'/-</td>
                        </tr>';

                    $point = array("Category" => $subcar, "3/4 inch" => $multi.'kgs',"Rs(3/4 inch)" => $two.'/-', "1 inch" => $multi2.'kgs',"Rs(1 inch)" => $twof.'/-', "1 1/4 inch" =>  $multi3.'kgs', "Rs(1 1/4 inch)" => $three.'/-', "1 1/2 inch" => $multi4.'kgs',"Rs (1 1/2 inch)" => $four.'/-');
                        array_push($data_points, $point); $i++; } } }
                    $output .= '</table>';   echo $output;  

  }
    $outputchainstar = str_replace(array('[{', '}]'), ' ', htmlentities(json_encode($data_points), JSON_PRETTY_PRINT));
    //print_r($outputchainstar);

     } } $_SESSION['outputchainstar'] = $outputchainstar; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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