PHP" for循环"收集数据

时间:2012-05-02 14:35:48

标签: php loops for-loop

如何循环这个东西来收集多个$ objectId的数据?例如从$ objectId 1038到1048。

在Im presentig可以收集数据的完整脚本。行$objectId = ""; //I want to gather info about IDs lets say from 1038 - 1048应该是我认为的循环,但我没有做好。

<form name="myForm" action="" method="post" id="myForm">
  <table cellpadding="2" border="1">
    <tr>
      <td width="264" rowspan="2" valign="top"><?php 

    function transformHTML($string)
    {
        $string = trim($string);
        $string = utf8_decode($string);
        $string = htmlentities($string, ENT_NOQUOTES);
        $string = str_replace("#", "&#35;", $string);
        $string = str_replace("%", "&#37;", $string);

        return $string;
    }

    function get_string_between($string, $start, $end){
        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0) return "";
        $ini += strlen($start);
        $len = strpos($string,$end,$ini) - $ini;
        return substr($string,$ini,$len);
    }

    if($_POST['submit']) { 

        $objectId = ""; //I want to gather info about IDs lets say from 1038 - 1048

            $error_B = "<span class=error>Error #2:<br> Does the item exist (ID: " . $objectId . ")?</span><br>";

            $lines = file('http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=' . $objectId);
            foreach ($lines as $line_num => $line) {
                $lineCrawl = htmlspecialchars($line);
                $StringSum = "$StringSum$lineCrawl";
            }
            $parsedGuidePrice = get_string_between($StringSum, "Current guide price", "Today");
            $parsedItemName = get_string_between($StringSum, "&lt;title&gt;", " - Grand Exchange - RuneScape&lt;/title&gt;");
            $parsedItemIconUrl  = get_string_between($StringSum, '&lt;meta property=&quot;og:image&quot; content=&quot;', '&quot; /&gt;');

            $guideprice = preg_replace("/[^\.kbm0-9]/", "", $parsedGuidePrice);

            if (strpos($guideprice,'k') !== false) {
                $guideprice = preg_replace("/[^0-9]/", "", $parsedGuidePrice);
                $guideprice *= 100;
            } else if (strpos($guideprice,'m') !== false) {
                $guideprice = preg_replace("/[^0-9]/", "", $parsedGuidePrice);
                $guideprice *= 100000;
            } else if (strpos($guideprice,'b') !== false) {
                $guideprice = preg_replace("/[^0-9]/", "", $parsedGuidePrice);
                $guideprice *= 100000000;
            } 
            if ($guideprice == "") { 
                echo $error_B; 
            } else { 
                echo "<img src='" . $parsedItemIconUrl . "' class='parsedItemIconUrl'>";
                echo "<br>";
                echo "<strong>" . $parsedItemName . "</strong> guide price: <strong>" . $guideprice . "</strong>"; 
                echo "<br>";
                echo "<small>(Item ID: " . $objectId . ")</small><br><br><br>";
            }
    }
    ?></td>
      <td width="172"></td>
      <td width="99"><input type="submit" name="submit" alt="Get yesterday's averge market price" title="Get yesterday's averge market price"></td>
    </tr>
    <tr valign="top">
      <td></td>
      <td><input value="Reset" type="button" name="reset" alt="Reset the form" title="Reset the form" onClick="javascript:window.location = '';"></td>
    </tr>
  </table>
</form>

1 个答案:

答案 0 :(得分:1)

<form name="myForm" action="" method="post" id="myForm">
  <table cellpadding="2" border="1">
    <tr>
      <td width="264" rowspan="2" valign="top"><?php 

    function transformHTML($string)
    {
        $string = trim($string);
        $string = utf8_decode($string);
        $string = htmlentities($string, ENT_NOQUOTES);
        $string = str_replace("#", "&#35;", $string);
        $string = str_replace("%", "&#37;", $string);

        return $string;
    }

    function get_string_between($string, $start, $end){
        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0) return "";
        $ini += strlen($start);
        $len = strpos($string,$end,$ini) - $ini;
        return substr($string,$ini,$len);
    }

    function echoObjectIdStuff($objectId) {
        $error_B = "<span class=error>Error #2:<br> Does the item exist (ID: " . $objectId . ")?</span><br>";

        $lines = file('http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=' . $objectId);
        foreach ($lines as $line_num => $line) {
            $lineCrawl = htmlspecialchars($line);
            $StringSum = "$StringSum$lineCrawl";
        }
        $parsedGuidePrice = get_string_between($StringSum, "Current guide price", "Today");
        $parsedItemName = get_string_between($StringSum, "&lt;title&gt;", " - Grand Exchange - RuneScape&lt;/title&gt;");
        $parsedItemIconUrl  = get_string_between($StringSum, '&lt;meta property=&quot;og:image&quot; content=&quot;', '&quot; /&gt;');

        $guideprice = preg_replace("/[^\.kbm0-9]/", "", $parsedGuidePrice);

        if (strpos($guideprice,'k') !== false) {
            $guideprice = preg_replace("/[^0-9]/", "", $parsedGuidePrice);
            $guideprice *= 100;
        } else if (strpos($guideprice,'m') !== false) {
            $guideprice = preg_replace("/[^0-9]/", "", $parsedGuidePrice);
            $guideprice *= 100000;
        } else if (strpos($guideprice,'b') !== false) {
            $guideprice = preg_replace("/[^0-9]/", "", $parsedGuidePrice);
            $guideprice *= 100000000;
        } 
        if ($guideprice == "") { 
            echo $error_B; 
        } else { 
            echo "<img src='" . $parsedItemIconUrl . "' class='parsedItemIconUrl'>";
            echo "<br>";
            echo "<strong>" . $parsedItemName . "</strong> guide price: <strong>" . $guideprice . "</strong>"; 
            echo "<br>";
            echo "<small>(Item ID: " . $objectId . ")</small><br><br><br>";
        }
    }

    if($_POST['submit']) { 
        for($id = 1038;  $id <= 1048; $id++) {
            echoObjectIdStuff($id);
        }
    }

    ?></td>
      <td width="172"></td>
      <td width="99"><input type="submit" name="submit" alt="Get yesterday's averge market price" title="Get yesterday's averge market price"></td>
    </tr>
    <tr valign="top">
      <td></td>
      <td><input value="Reset" type="button" name="reset" alt="Reset the form" title="Reset the form" onClick="javascript:window.location = '';"></td>
    </tr>
  </table>
</form>