-3

目前,我有这个代码:

<?php
if (isset($_GET['s'])) {
    $itemid = $_GET['s'];
    $search = "$itemid";
    $query  = ucwords($search);
    echo "<title>Results for $query</title>";
    $string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
    if ($itemid == "") {
        echo "Please fill out the form.";
    } else {
        echo '<div id="content">';
        $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://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
                echo $matches[1];
                echo "</a><br>";
            }
        }
        echo '</div>';
    }
} else {
    echo "Item does not exist!";
}
?>

如果“$matches[1]”里面什么都没有,我希望我的代码回显“项目不存在!” 我该怎么做呢?请帮忙!

我以前尝试过类似的if ($matches[1]=="") { echo "Item does not exist!"; }方法,但是没有用。这就是我得到的:

http://img685.imageshack.us/img685/998/28990b2c12d0423292d3574.png

看看效果好吗?看看如果 $matches[1] 确实存在会发生什么:

http://img528.imageshack.us/img528/3690/71472c9de6ec49118ee8d48.png

它仍然出现!我怎样才能使我的代码只有在没有 $matches[1] 的情况下才回显错误?请帮我!

如果您想知道,这是我添加时的代码if ($matches[1]=="") { echo "Item does not exist!"; }

<?php
if (isset($_GET['s'])) {
    $itemid = $_GET['s'];
    $search = "$itemid";
    $query  = ucwords($search);
    echo "<title>Results for $query</title>";
    $string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
    if ($itemid == "") {
        echo "Please fill out the form.";
    } else {
        echo '<div id="content">';
        $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://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
                echo $matches[1];
                echo "</a><br>";
            }
        }
        echo '</div>';
        if ($matches[1] == "") {
        echo "Item does not exist!";
        }
    }
} else {
    echo "Item does not exist!";
}
?>

对我的问题的任何帮助将不胜感激!

4

2 回答 2

0

Look at PHP's comparison operators

if (empty($matches[1])) { echo "Item does not exist!"; }
于 2012-06-10T22:34:32.690 回答
0
else { 
    echo "Item does not exist!"; 
} 

This "else" is in regards to isset($_GET['s']). Is there a variable called s in your query string? If there isn't, then it's normal that you get the message.

Maybe you put that else block in the wrong place?

于 2012-06-10T22:38:23.640 回答