PHP将行拆分为页面

时间:2012-06-09 22:24:59

标签: php split

目前,我有这段代码:

<?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 个答案:

答案 0 :(得分: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!";
}

?>