跳到主要內容

用PHP遞迴選單解指定頂層分類底下所有子分類中內容頁面的方法!!-教學撰寫:徐嘉裕Neil hsu

通常在首頁的商品分類選單都是顯示第一層,因為如果開太多層畫面會變得不好看,像是下面這樣的選單,只開一層感覺還不錯!!



如果選擇商品分類01,那基本上只會顯示商品分類01底下的商品內容,但如果商品分類01底下還有其他子分類跟商品內容就抓不到了!


這樣的區塊不是很奇怪,底層分類的商品永遠不會顯示在首頁的區塊中,只會顯示頂層的,所以解決辦法就是當USER選擇頂層分類時,連同頂層中全部的子分類中的商品內容頁通通抓出來,用隨機排列,這樣問題就解決了阿!!但是需要用到遞迴來解商品分類,再透過商品分類撈出商品頁面的ID。

方法如下

首先選單透過AJAX選單傳值過去的(分類id),這部分就不說明了,然後以下是在AJAX中遞迴解商品分類及商品內容的方法


$dbneme="productsort";
//第一層
$where="  where `sortid` = '".$selectid."' &&  `enable` = '1'  order by  sortidsort  DESC";  //where數值
$productsortArr=databasetablewhile($dbneme,$where);


//遞回開始

//分類指定sortid遞迴到底
function sortretorted($dbneme="",$sortid=""){ 
$where=" where `of_sortid` = '".$sortid."' order by sortidsort DESC";   //where數值
$sortArr=databasetablewhile($dbneme,$where);
foreach($sortArr as $key=> $val){
$sortvar.=$sortArr[$key]['sortid'].",";
$sortvar.=sortretorted($dbneme,$sortid="".$sortArr[$key]['sortid']."");
}
return $sortvar;
}

foreach($productsortArr as $key=> $val){
$sortvar.=$productsortArr[$key]['sortid'].","; 
$sortvar.=sortretorted($dbneme,$sortid="".$productsortArr[$key]['sortid']."");
}

//去除最後一個字元
$sortvar=substr($sortvar,0,-1);
//切割陣列
$sortvarArr=cuttingfunction($cutting=$sortvar,$sign=",");

//到這裡可以得到選單指定頂層分類以下的全部子分類ID(包括頂層分類),接下來再用分類ID來撈出商品ID

foreach($sortvarArr as $key=> $val){
//開啟商品資料表
$dbneme="productcontent";
$where="  where `sortid` = '".$val."' &&  `productstatus` != 'notshelves'";  //where數值
$productcontentArr=databasetablewhile($dbneme,$where);
foreach($productcontentArr as $i=> $val){
$productid.="`productid` = '".$productcontentArr[$i]['productid']."' ||";
}
}
//去除最後一個字元
$productid=substr($productid,0,-2);

//到這裡透過分類ID吧全部的商品ID都解出來了,然後再用商品ID來開啟頁顯示的資料內容!


$textdate.="<div class='row'>";
//圖片時間
$img_date=date("H:i:s");

//狀態
switch ($status){
case "0":    //最新
$order="order by  productid  DESC";
break; 
case "1":      //排序
$order="order by  totalsort  DESC";
break; 
case "2":      //隨機
$order="order by RAND()";
break; 
}


//開啟商品資料表
$dbneme="productcontent";
$where="  where ".$productid."  &&  `productstatus` != 'notshelves' ".$order." limit ".$total."";  //where數值
$productcontentArr=databasetablewhile($dbneme,$where);
foreach($productcontentArr as $key=> $val){

if(empty($productcontentArr[$key]['blockimg'])){
$blockimg="<img class='lazy' data-original='".XOOPS_URL."/modules/neilproduct/images/noimg.jpg'>";
}else{
$blockimg=" <img class='lazy' data-original='".XOOPS_URL."/uploads/neilproduct/image/.thumbs/".$productcontentArr[$key]['blockimg'].".jpg?state=".$img_date."'>";
}


$textdate.="<div  class='col-md-3'>
<div  id='neilproduct_blockbox'>

<div id='neilproduct_blockboximg'>
<div id='transform'>
<a title='".$productcontentArr[$key]['productidtitle']."' href='".XOOPS_URL."/modules/neilproduct/shopwcase.php?productid=".$productcontentArr[$key]['productid']."'>
".$blockimg." 
</a>
</div></div>

<div id='productidtitleblock'>
<a title='".$productcontentArr[$key]['productidtitle']."' href='".XOOPS_URL."/modules/neilproduct/shopwcase.php?productid=".$productcontentArr[$key]['productid']."'>
".$productcontentArr[$key]['productidtitle']."
</a>
</div>
<div id='brieflyblock'>".$productcontentArr[$key]['briefly']."</div>
</div>
</div>";

}

$textdate.="</div>";

$textdate.="<script type='text/javascript' src='".XOOPS_URL."/modules/neillibrary/js/lazyload/jquery.lazyload.js'></script> 
 <script>
$('img.lazy').lazyload({
effect : 'fadeIn'
});
</script>";
  
 echo $textdate; 

這樣AJAX輸出後就會顯示頂層分類底下全部分類的商品內容,而不是只有頂層分類的商品內容!





有需要的朋友參考看看

教學撰寫:徐嘉裕 Neil hsu

留言

這個網誌中的熱門文章

好用的windows備份檔案dos指令XCOPY-教學撰寫:徐嘉裕Neil hsu

要快速的備份某個硬碟全部資料到備份硬碟中,雖然是可以用windows內建的備份與還原,但如果重灌windows可會有key不同而無法還原的問題,還要改一堆設定煩死了,直接用dos指令XCOPY來做檔案的複製就很快了,資料也不會丟失,好朋友可以參考看看!! 首先開啟windows所有程式->附屬應用程式->開啟命令提示字元! 然後輸入以下指令 XCOPY C:\xxx   F:\xxx /s 藍字的 C:\xxx  為複製檔案來源位置例如要複製整個C槽就輸入 C:\ 綠色的   F:\xxx 為複製目的位置,例如要放到F槽的備份資料夾,就輸入  F:\ 備份 紅字的 S 為複製類型參數,可以自行修改為以下的參數設定: ================================================================= /A    只複製設定成保存屬性的檔案,不要改變屬性的設定。 /M    只複製設定成保存屬性的檔案,並清除保存屬性。 /D:m-d-y  複製指定日期當天或之後變更的檔案。如果沒給日期,只複製那些來源檔案日期比目的檔案日期為新的檔案。 /EXCLUDE:file1[+file2][+file3]...         指定檔案清單字串。每個字串應在不同行。如果有字串對應到要進行複製的檔案絕         對路徑的任何部分,這個檔案會被排除複製。例如,指定字串         \obj\ 或 .obj 的話,會排除所有在 obj 目錄下副檔名是.obj 的檔案複製。 /P    在建立每個目的檔案時顯示提示。 /S    複製每個目錄及其包含的子目錄,不複製空目錄。 /E    複製每個目錄及其包含的子目錄,也複製空目錄。/S 與 /E相同,能夠用來修改 /T。 /V    驗證每個新檔案。 /W    在複製之前提示您按鍵繼續。 /C    如果錯誤發生時也繼續複製。 /I    如果目的不存在且複製一個以上的檔案的話,就假設指定的目的一定是目錄。 /Q    在複製時不要顯示檔名。 /F    在複製時顯示來源及

jQuery取得下拉選單selected中數值與內容的方法

假設選單狀態為: <select id='selectname '> <option value='v1' data-id="d1">選單A</option> <option value='v2' data-id="d2">選單B</option> <option value='v3' data-id="d3">選單C</option> </select> 以jQuery取得選單數值與內容方法如下: 1、取得下拉選單 value 數值的方法 $selectname=$('#selectname').val(); alert($selectname); //顯示選單中 selected 狀態的value數值v1 or  v2  or  v3。 2、取得下拉選單中 data-id 數值的方法 $data-id= $(this).find(':selected').attr('data-id'); alert($data-id); //顯示選單中 selected 狀態的data-id數值d1 or  d2 or  d3。 3、取得下拉選單中 文字 的方法 $selecttext=$(this).find(':selected').text(); alert($selecttext); //顯示選單中 selected 狀態的文字內容,如選單A or  選單B or 選單C。 教學撰寫:徐嘉裕 Neil hsu

CentOS7啟用ssh的設定方法-教學撰寫:徐嘉裕Neil hsu

安裝好CentOS7後可以在圖形化介面中點選右鍵->開啟服務器修改設定後啟用SSH設定,方法如下: 1、先安裝openssh套件 sudo yum install openssh* 2、用 gedit 開啟sshd_config編輯 (不要用vi很難編輯的) sudo gedit /etc/ssh/sshd_config 3、增加可連線SSH帳號 AllowUsers admin (此欄位需要自行新增) 4、修改PORT吧 # 註解拿掉 (可以改成8022PORT或其他PORT都可以) Port 22 搜尋PermitRootLogin吧 # 註解拿掉 PermitRootLogin no 再來把這兩行的 # 註解拿掉 PermitEmptyPasswords no PasswordAuthentication yes 搜尋Protocol設定使用SSHv2連線 Protocol 2 sudo systemctl restart sshd.service    重新啟動service sudo systemctl enable sshd.service    設定開機啟動SSH sudo semanage port -a -t ssh_port_t -p tcp 22      防火牆允許22 PORT 或 sudo firewall-cmd --permanent --zone=public --add-port=22/tcp 重新載入防火牆設定 sudo firewall-cmd --reload sudo  netstat -ant | grep :22 查看 22PORT 查詢現在SELinux設定清單中的SSH服務有哪些Port sudo semanage port -l | grep ssh ssh_port_t tcp 8022, 22 怎麼測試? 格式:登入帳號@IP或主機名稱 指令:-p SSH服務Port號 sudo ssh -p 22 admin@192.168.1.100 如設定的Port在清單中 ,那就表示SSH設定完成防火牆也開啟列外了,之後再用遠端的PuTTY輸入 IP/Port 連線,然後輸入 帳號/密碼,就能用SSH連線進入主機了, 在透