doujiebo9849 2013-04-27 18:13
浏览 169
已采纳

用GET方法改变变量

I have a page test.php in which I have a list of names:

name1: 992345
name2: 332345
name3: 558645
name4: 434544

In another page test1.php?id=name2 and the result should be:

332345

I've tried this PHP code:

<?php 
libxml_use_internal_errors(true); 
$doc = new DOMDocument(); 
$doc->loadHTMLFile("/test.php"); 
$xpath = new DOMXpath($doc); 
$elements = $xpath->query("//*@".$_GET["id"]."");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "
";
}
}
}
?>

I need to be able to change the name with GET PHP method in test1.pdp?id=name4

The result should be different now.

434544

is there another way, becose mine won't work?

  • 写回答

2条回答 默认 最新

  • douhai5835 2013-04-27 18:35
    关注

    Here is another way to do it.

    <?php 
    libxml_use_internal_errors(true); 
    
    /* file function reads your text file into an array. */
    $doc = file("test.php"); 
    
    $id = $_GET["id"];
    
    /* Show your array. You can remove this part after you 
     * are sure your text file is read correct.*/
    
    echo "Seeking id: $id<br>";
    echo "Elements:<pre>";
    print_r($doc);
    echo "</pre>";
    
    /* this part is searching for the get variable. */
    
    if (!is_null($doc)) {
        foreach ($doc as $line) {
            if(strpos($line,$id) !== false){
                $search = $id.": ";
                $replace = '';
                echo str_replace($search, $replace, $line);
            }
        }
    } else {
        echo "No elements.";    
        }
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?