2012-06-09 61 views
2

網頁目前,我有這樣的代碼:PHP分割線爲

<?php 

    if (isset($_GET['id'])) { 
    $itemid = $_GET['id']; 
    $search = "$itemid"; 
    $query = ucwords($search); 
    $string = file_get_contents('http://example.com/tools/newitemdatabase/items.php'); 
    if ($itemid == "") { 
     echo "Please fill out the form."; 
    } else { 
     $string = explode('<br>', $string); 
     foreach ($string as $row) { 
     preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches); 
     if (preg_match("/$query/i", "$matches[1]")) { 
      echo "<a href='http://example.com/tools/newitemdatabase/info.php?id=$matches[2]'>"; 
      echo $matches[1]; 
      echo "</a><br>"; 
     } 
     } 
    } 
    } else { 
    echo "Item does not exist!"; 
    } 
?> 

我想要做的就是採取一切其結果是在該行echo $matches[1];和只有5條線的頁面之間拆分它的每頁。

這是什麼是目前發生的事情爲例:
http://clubpenguincheatsnow.com/tools/newitemdatabase/search.php?id=blue

所以我想要做的是分裂的線成單獨的頁面只有五各線。

例如:

  • http://example.com/tools/newitemdatabase/search.php?id=blue&page=1
  • http://example.com/tools/newitemdatabase/search.php?id=blue&page=2
+1

我同意,indent所以它的可讀性被高估了。 – DaveRandom

+0

不要在preg_match中使用'「$ matches [1]」''。是的,它需要一個字符串,但是如果'「匹配[1]」'是一個字符串,那麼它是可以的。 –

回答

0

您可以使用索引變量做到這一點,然後打印只有5個結果,這樣的:

<?php 
if (isset($_GET['id'])) { 
    $itemid = $_GET['id']; 
    $search = "$itemid"; 
    $query = ucwords($search); 
    $string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php'); 
    if ($itemid == "") { 
    echo "Please fill out the form."; 
    } else { 
    $string = explode('<br>', $string); 

    // Define how much result are going to show 
    $numberToShow = 5; 

    // Detect page number (from 1 to infinite) 
    if (isset($_GET['page'])) { 
     $page = (int) $_GET['page']; 
     if ($page < 1) { 
     $page = 1; 
     } 
    } else { 
     $page = 1; 
    } 
    // Calculate start row. 
    $startRow = ($page - 1) * $numberToShow; 

    // For index use 
    $i = 0; 
    foreach ($string as $row) { 
     preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches); 
     if (preg_match("/$query/i", "$matches[1]")) { 
     // If the start row is current row 
     // and this current row is not more than number to show after the start row 
     if ($startRow >= $i && $i < $startRow + $numberToShow) { 
      echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>"; 
      echo $matches[1]; 
      echo "</a><br>"; 
     } 
     // Acumulate index 
     $i++; 
     } 
    } 
    } 
} else { 
    echo "Item does not exist!"; 
} 

?>