使 if in PHP 无穷大
Make if in PHP infinite
我需要一些有关 php 代码的帮助。
我的 controller/product/product.php (opencart 1.5.6):
中有这段代码
$rsNext = $this->model_catalog_product->getProduct($product_id+1);
$rsNext2 = $this->model_catalog_product->getProduct($product_id+2);
$rsNext3 = $this->model_catalog_product->getProduct($product_id+3);
$rsLast = $this->model_catalog_product->getProduct($product_id-1);
$rsLast2 = $this->model_catalog_product->getProduct($product_id-2);
$rsLast3 = $this->model_catalog_product->getProduct($product_id-3);
if($rsNext):
$this->data['next_url'] = $this->url->link('product/product', 'product_id=' . $rsNext['product_id']);
$this->data['next_text']= $rsNext['name']." >>";
elseif($rsNext2):
$this->data['next_url'] = $this->url->link('product/product', 'product_id=' . $rsNext2['product_id']);
$this->data['next_text']= $rsNext2['name']." >>";
elseif($rsNext3):
$this->data['next_url'] = $this->url->link('product/product', 'product_id=' . $rsNext3['product_id']);
$this->data['next_text']= $rsNext3['name']." >>";
else:
$this->data['next_url'] = '';
$this->data['next_text']= '';
endif;
if($rsLast):
$this->data['prev_url'] = $this->url->link('product/product', 'product_id=' . $rsLast['product_id']);
$this->data['prev_text']= "<< ".$rsLast['name'];
elseif($rsLast2):
$this->data['prev_url'] = $this->url->link('product/product', 'product_id=' . $rsLast2['product_id']);
$this->data['prev_text']= "<< ".$rsLast2['name'];
elseif($rsLast3):
$this->data['prev_url'] = $this->url->link('product/product', 'product_id=' . $rsLast3['product_id']);
$this->data['prev_text']= "<< ".$rsLast3['name'];
else:
$this->data['prev_url'] = '';
$this->data['prev_text']= null;
endif;
我怎么能让这个无限大,因为在某些情况下需要 product_id+15 或更多。
谢谢!
假设$product_id
是一个整数,你可以这样循环:
$from = $product_id -10;
$to = $product_id + 10;
for($i = $from; $i<$to; $i++){
echo $i;
}
会不会影响加载速度,是的。如果你问多少,最少。也许几微秒。需要更多示例吗?看看这个非常容易构造的 Google 查询。
使用 foreach 创建一个循环
在 foreach 循环中使用 if 的基本示例
$number = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
echo '<ul>';
foreach($number as $num){
if($num > 5){
echo '<li>Greater than - '.$num.'</li>';
}else{
echo '<li>less than - '.$num.'</li>';
}
}
echo '</ul>';
我需要一些有关 php 代码的帮助。 我的 controller/product/product.php (opencart 1.5.6):
中有这段代码 $rsNext = $this->model_catalog_product->getProduct($product_id+1);
$rsNext2 = $this->model_catalog_product->getProduct($product_id+2);
$rsNext3 = $this->model_catalog_product->getProduct($product_id+3);
$rsLast = $this->model_catalog_product->getProduct($product_id-1);
$rsLast2 = $this->model_catalog_product->getProduct($product_id-2);
$rsLast3 = $this->model_catalog_product->getProduct($product_id-3);
if($rsNext):
$this->data['next_url'] = $this->url->link('product/product', 'product_id=' . $rsNext['product_id']);
$this->data['next_text']= $rsNext['name']." >>";
elseif($rsNext2):
$this->data['next_url'] = $this->url->link('product/product', 'product_id=' . $rsNext2['product_id']);
$this->data['next_text']= $rsNext2['name']." >>";
elseif($rsNext3):
$this->data['next_url'] = $this->url->link('product/product', 'product_id=' . $rsNext3['product_id']);
$this->data['next_text']= $rsNext3['name']." >>";
else:
$this->data['next_url'] = '';
$this->data['next_text']= '';
endif;
if($rsLast):
$this->data['prev_url'] = $this->url->link('product/product', 'product_id=' . $rsLast['product_id']);
$this->data['prev_text']= "<< ".$rsLast['name'];
elseif($rsLast2):
$this->data['prev_url'] = $this->url->link('product/product', 'product_id=' . $rsLast2['product_id']);
$this->data['prev_text']= "<< ".$rsLast2['name'];
elseif($rsLast3):
$this->data['prev_url'] = $this->url->link('product/product', 'product_id=' . $rsLast3['product_id']);
$this->data['prev_text']= "<< ".$rsLast3['name'];
else:
$this->data['prev_url'] = '';
$this->data['prev_text']= null;
endif;
我怎么能让这个无限大,因为在某些情况下需要 product_id+15 或更多。 谢谢!
假设$product_id
是一个整数,你可以这样循环:
$from = $product_id -10;
$to = $product_id + 10;
for($i = $from; $i<$to; $i++){
echo $i;
}
会不会影响加载速度,是的。如果你问多少,最少。也许几微秒。需要更多示例吗?看看这个非常容易构造的 Google 查询。
使用 foreach 创建一个循环
在 foreach 循环中使用 if 的基本示例
$number = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
echo '<ul>';
foreach($number as $num){
if($num > 5){
echo '<li>Greater than - '.$num.'</li>';
}else{
echo '<li>less than - '.$num.'</li>';
}
}
echo '</ul>';