2

目前,我有这个代码:

<?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
  • 等等
4

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!";
}

?>
于 2012-06-09T22:45:47.793 回答