gpt4 book ai didi

php - 更改字符串中的值而不是硬编码

转载 作者:行者123 更新时间:2023-11-30 01:16:22 25 4
gpt4 key购买 nike

专家是否有任何选项可以在category_level中自动增加+1

例如: $cond_cat="where Category_level='1' and ID='$sleads' ";$cond_cat="where Category_level='2' and Parent_id =' $sleads' ";

我的代码如下。

 <?php
include('classes/global.php');
$CT=new Product();
$AdminObj=new Admin();
$sleads=$_REQUEST['sleads'];
$cond_cat=" where category_level='1' and ID='$sleads' ";
$catResult_cond=$CT->getCategory($cond_cat);
$cond_cat=" where category_level='2' and parent_id ='$sleads' ";
$subCatResult_cond=$CT->getCategory($cond_cat);

?>

最佳答案

您可能想尝试递归地获取子类别作为您所在子类别的子类别。

<?php

function getSubCategories($level) {
// you should obviously get the $sleads variable in here, for this to work.
$cond_cat = "where category_level='%d' and ID='$sleads' ";
$subcategories = $CT->getCategory(sprintf($cond_cat, $level));
if (count($subcategories) > 0) {
foreach($subcategories as $subcategory) {
$subcategory->subcategories = $CT->getCategory(sprintf($cond_cat, $level+1));
}
}
return $subcategories;
}

$i = 1;
$categories = getSubCategories($i);

?>

这会给你一些类似的东西:

// $categories         // first level
// foreach($categories as $cat) {
// $cat->subcategories // second level categories
// foreach($cat->subcategories as $subcategory) {
// $subcategory->subcategories // third level
// }
// }
// and so on

请考虑到我假设您的 $CT->getCategory 函数返回一个类别记录数组。
如果情况并非如此(考虑到您还在查询中添加了 ID=""),您还应该更改该函数,或使用返回多个类别记录的函数。

关于php - 更改字符串中的值而不是硬编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19020578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com