2017-05-08 6 views
0

私はPHPコードでいくつかの助けが必要です。例えば、以上のproduct_id + 15を必要とする状況があるので、私は、これは無限作ることができるどのようPHPで無限にする場合

  $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.phpに(opencart 1.5.6)にこのコードを持っています。 ありがとう!

+2

無限を使用してforeachの

基本的な例を使ってlooppを作りますか?これは、コードを実行するのに無限の時間を要します。ループを使うのはどうですか? – Xorifelse

+0

これは私が欲しいものですが、どのようにループを作りますか?ループを作れば、これは私のロードスピードに影響しますか? –

+0

whileやforeachを使ってループを作る –

答えて

0

$product_idが整数であると仮定すると、あなたはそうのようなループを行うことができます。

$from = $product_id -10; 
$to = $product_id + 10; 

for($i = $from; $i<$to; $i++){ 
    echo $i; 
} 

それはそう、あなたの負荷速度に影響を及ぼします。あなたがどれくらい尋ねるのであれば、最低限。おそらく数マイクロ秒。もっと例が必要ですか?この非常に簡単に構築されたGoogleクエリを見てください。

0

内部のforeachループであれば

$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>'; 
+1

'foreach'は6つの変数になりますが、すべての整数は?それは論理的ではないようです〜バルカン – Xorifelse

関連する問題