2017-04-26 68 views
0

PHP 7 我想列出,在下拉菜單按鈕是動態的帶動下,天多用途陣列中的下拉菜單返回星期只有一次一週,如果它們出現在查詢中。不過,我只想要一個結果每天打印,即使會有更多。PHP的 - 使用從DB查詢

例如:如果在星期一有12個客戶被分配,我只希望在我的下拉菜單中打印一個'星期一'結果。 This is an example of下拉按鈕應該如何在執行查詢中列出的日期。 #note該Customer表對於週三2項作爲Customers按鈕指示降了下來,但我只想週三在下拉爲Routes列出一次。

*基本上查詢被從表中*其中一個USER_ID =「SESSION_ID」,這個訂單通過一個天列,它是在DB數字形式;週一至週日1-7。

$sql = " 
SELECT * 
FROM `customers` 
WHERE `users_id` = '1' 
ORDER BY `cust_route_day` ASC 
LIMIT 0 , 30"; 
$cust = new User; 
$results = $cust->db->query($sql); 

User來自連接到數據庫並執行各種查詢的用戶類。 定義和聲明。

$custName = array(); 
$custRouteDay = array(); 
$custID = array(); 
while ($row = $results->fetch_assoc()) 
{ 
    $custName[] = $row['cust_first_name'].' '.$row['cust_last_name']; 
    $custRouteDay[] = $row['cust_route_day']; 
    $custID[] = $row['cust_id']; 
} 
define("CUSTID", $custID); 
define("USER_TASKS", [ 
    'Task Info' => 'Task Info', 
    'Task Time' => 'Task Time', 
    'Customer Route Info' => 'Customer Route Info', 
    'Set Alert' => 'Set Alert' 
]); 
define("CUSTOMERS", $custName); 
define("USER_TASKS", $userTasks); 
// Assign the abbr value of day ex: 'Wed' to numerical value from DB ex: '2' 
foreach(CUST_ROUTE_DAY as $key => $value){ 
    $newKey .= ROUTEDAY[$value]." "; 
    $custRouteKeyArray = explode(" ",$newKey); 
} 

define("NAVICONS", [ 
    "glyphicon glyphicon-user", 
    "glyphicon glyphicon-road", 
    "glyphicon glyphicon-tasks", 
    "fa fa-sitemap", 
    "fa fa-globe" 
]); 
define("STYLE", [ 
    "fixed-bottom" => "navbar navbar-default navbar-fixed-bottom", 
    "fixed-top" => "navbar navbar-default navbar-fixed-top", 
    "default" => "navbar navbar-default" 
]); 
define("NAVBTNS", [ 
    "Customers"=>CUSTOMERS, 
    "Route Info"=>$custRouteKeyArray, 
    "Tasks"=>USER_TASKS, 
    "Networking", 
    "Maps" 
]); 

定義的陣列NAVBTNS

Array ([0] => John Banner [1] => Dale Landry [2] => Bill Childers [3] => Darren Little [4] => Gary Garland) Array ([0] => Mon [1] => Wed [2] => Wed [3] => Thur [4] => Fri [5] =>) Array ([Task Info] => Task Info [Task Time] => Task Time [Customer Route Info] => Customer Route Info [Set Alert] => Set Alert) NetworkingMaps 

創建導航菜單btns和下拉BTN功能。基本上,如果數組由子數組組成,請創建一個下拉按鈕並適當地分配鍵和值。

function construct_navbar_buttons(){ 
//because key values in the parent array are mixed numerical and char values 
//define and increment $i each time through the loop for NAVICONS class values and page number key 
$i = 0; 
//Start the post variable that will hold the result string $btns <ul> 
$btns = '<ul class="nav navbar-nav">'; 
foreach(NAVBTNS as $k => $v){//$k holds index or e.g. Customers 
    if(is_array($v)){//go over subarray else print button see //we have a string value 
     $filtered = array_filter($v);//array_filter to remove null values 
     if(empty($filtered)) { 
      $btns .= '<li><a href="?p='.$v.'"><i class="'.NAVICONS[$i].'"></i>&nbsp;&nbsp;'.$v.'</a></li>'; 
     }else{//start drop down here 
      $btns .= '<li class="dropdown"> 
         <a class="dropdown-toggle" data-toggle="dropdown" href=""> 
          <i class="'.NAVICONS[$i].'"></i>&nbsp;&nbsp;'.$k.'&nbsp;&nbsp;<span class="caret">&nbsp;</span> 
         </a> 
         <ul class="dropdown-menu dropdown-user">'; 
      foreach($v as $key => $value){//loop through results of child array 
       if(in_array($v[$key], $v)){ //locate instance where key exists and print list-item a tag button in drop-down 
        ################################################################ 
        ## need logic to go over first instance of a value within the ## 
        ## parent array to print only that first value in the results ## 
        ################################################################ 

        $btns .= '<li><a href="?p='.$k.'&n='.$v[$key].'">'.$v[$key].'</a></li>'; 
       } 
      } 

      $btns .= "</ul>\n</li>";//close the parent UL and LI tags 

     } 
    }else{//we have a string value 
     //do stuff example data: Tasks - key here seems aways a index number 
     $btns .= '<li><a href="?p='.$v.'"><i class="'.NAVICONS[$i].'"></i>&nbsp;&nbsp;'.$v.'</a></li>'; 
    } 
    $i++;print_r($v);  
    } 
    $btns .= '</ul>'; 
    return $btns; 
} 

使用array_search試過,但是這不是給我,我希望

$d = array_search($value, $v); 
if($d = ROUTEDAY[$i]){ 
    echo $value; 
} 

$day = array("Mon","Tue","Wed","Thur","Fri","Sat","Sun"); 
    if($d = $day[$i]){ 
     echo $day[$i]; 
    } 

ROUTEDAY的結果被定義的變量保存與鍵的值一週的日子裏,給了我下面的結果在一個簡單的回聲。 1 - MonWedWedThurFriTask InfoTask TimeCustomer Route InfoSet Alert 2 - MonTueWed 我以爲array_search只給了您在數組中第一次出現的值。

我已經嘗試爲數組的每個實例創建單獨的foreach循環,但這使事情變得複雜,我希望僅使用兩個foreach循環完成此操作。我非常接近這一點。如果有人能幫助,我會非常感激。

回答

0

好吧,爲了其他人可能正在尋找類似的答案,我想出瞭如何使這項工作。

我改變了重新定義的數組來指定縮寫。對於本週日的數值,在這裏:

foreach(CUST_ROUTE_DAY as $key => $value){ 
     $newKey .= ROUTEDAY[$value]." "; 
     $custRouteKeyArray = explode(" ",$newKey); 
    } 

以下幾點:

$day = array("Mon"=>0,"Tue"=>1,"Wed"=>2,"Thur"=>3,"Fri"=>4,"Sat"=>5,"Sun"=>6); 
$d = array(); 
foreach($day as $days=>$key){ 
    if(in_array($key, $custRouteDay)){ 
     $d[] = ROUTEDAY[$key]; 

    } 
} 

這給了我:Array ([0] => Mon [1] => Wed [2] => Thur [3] => Fri)

我在NAVBTNS陣列使用這個值:

define("NAVBTNS", [ 
    "Customers"=>CUSTOMERS, 
    "Route Info"=>$d, 
    "Tasks"=>USER_TASKS, 
    "Networking", 
    "Maps" 
]); 

*問題解決!