dqayok7935 2018-06-08 23:29
浏览 68
已采纳

在php数组中查找值

I've been banging my head hard over this problem for the last 2-3 days trying to see the problem from as many different angles as possible but to no avail. I'm turning to the SO community for extra perspectives. Below is the code I have which prints all 9 product plans. I'm wanting to find and print the plan with pricing equals or closest to a given user input. How can I do this?

//arrays of productnames
$productnames=array(1=>"Beginner","Advanced","Expert");

//arrays of productlevels
$productlevels=array(1=>"Bronze","Silver","Gold");

//Get The Length of Product Name Array
$planname_array_length=count($productnames);

//Get The Length of Product Level Array
$planlevel_array_length=count($productlevels);

for ($prn=1; $prn <= $planname_array_length; $prn++) {//loop to create plan name indicators 
    for ($prl=1; $prl <= $planlevel_array_length; $prl++) {//loop to create plan level indicators 

        $getpoductsql = " SELECT name, level,productNameId,productLevelId,finalProductPrice
                        FROM ( 
                        SELECT wspn.productName AS name, wspl.productLevel AS level, wsp.productNameId AS productNameId, wsp.productPlanLevel AS productLevelId, 
                        ROUND(SUM(`Price`) * 1.12) AS finalProductPrice,
                        FROM `products` ws 
                        left join product_plan wsp on wsp.productId = ws.wsid 
                        left join product_plan_level wspl on wsp.productPlanLevel = wspl.wsplid 
                        left join product_plan_name wspn on wspn.wspnid = wsp.productNameId 
                        WHERE wspn.productName = '$planname_array_length[$pn]' AND wspl.productLevel = '$planlevel_array_length[$pl]'
                        )
                        AS x ORDER BY ABS(finalProductPrice - $compareprice)"
            $resultproducts = $conn->query($getpoductsql);
        $prodArray = mysqli_fetch_array($resultproducts);

        //print array of each plan
        $resultArr = array('planNameID' => $prodArray['planNameId'], 
                           'planName' => $prodArray['name'], 
                           'planLevelID' => $prodArray['planLevelId'],
                           'planLevelName' => $prodArray['level'],
                           'planPrice' => $prodArray['finalProductPrice'];

                           //print arrays of products
                           echo json_encode($resultArr);

    }
}

This will output 9 plans as follow :

{"planNameID":"1","productName":"Beginner","productLevelID":"1","productLevelName":"Bronze","productPrice":"15"}
  • 写回答

3条回答 默认 最新

  • doudilin1225 2018-06-09 00:34
    关注

    Rather than performing a separate query for each product name and product level, do them all in one query, and let MySQL find the one with the closest price.

       $getpoductsql = " SELECT name, level,productNameId,productLevelId,finalProductPrice
                        FROM ( 
                        SELECT wspn.productName AS name, wspl.productLevel AS level, wsp.productNameId AS productNameId, wsp.productPlanLevel AS productLevelId, 
                        ROUND(SUM(`Price`) * 1.12) AS finalProductPrice,
                        FROM `products` ws 
                        left join product_plan wsp on wsp.productId = ws.wsid 
                        left join product_plan_level wspl on wsp.productPlanLevel = wspl.wsplid 
                        left join product_plan_name wspn on wspn.wspnid = wsp.productNameId 
                        WHERE wspn.productName IN ('Beginner', 'Advanced', 'Expert') AND wspl.productLevel IN ('Bronze', 'Silver', 'Gold')
                        GROUP BY productNameId, productLevelId
                        )
                        AS x ORDER BY ABS(finalProductPrice - $compareprice)"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退