2017-05-08 43 views
0

我需要一些幫助與PHP代碼。 我有這樣的代碼在我的控制器/產品/ product.php(Opencart的1.5.6):如果在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_id + 15的情況。 謝謝!

+2

無限?這將需要無限的時間來執行代碼。你如何使用循環? – 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

簡單的例子做一個loopp如果裏面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