2013-04-10 138 views
1

我的網站解析西班牙語字典,並允許您一次搜索多個單詞。如果你看到「hola」,你會得到第一個div)。有些話拿出建議,如 「卡薩」,而不是像 「HOLA」 的定義:如何打開在div中解析的鏈接?

enter image description here

什麼我期待的是這樣的:

enter image description here

所以,我希望:當您點擊建議(例如我發佈的示例中的CASAR)將結果打印在像HOLA這樣的div中時。下面是使用的代碼:

$words = array('word0','word-1'); 
    function url_decode($string){ 
    return urldecode(utf8_decode($string)); 
    } 

    $baseUrl = 'http://lema.rae.es/drae/srv/search?val='; 

    $cssReplace = <<<EOT 

    <style type="text/css"> 
    // I changed the style 
    </style> 
    </head> 
EOT; 

$resultIndex = 0; 

foreach($words as $word) { 
if(!isset($_REQUEST[$word])) 
    continue; 

$contents = file_get_contents($baseUrl . urldecode(utf8_decode($_REQUEST[$word]))); 

$contents = str_replace('</head>', $cssReplace, $contents); 
$contents = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/search', $contents); 


echo "<div style=' 
     //style 
    ", (++$resultIndex) ,"'>", $contents, 
     "</div>"; 
} 

我有嘗試:$contents .= '<a href="">' . $word . '</a><br/>';但它沒有工作,也不知道真的哪裏/如何使用它。

+1

首先,+1純粹是因爲我喜歡這個概念。其次,也許你應該研究javascript或jQuery以及ajax這種交互式用戶體驗。 – 2013-04-10 00:35:08

+0

@ francisco.preller感謝弗朗西斯科,但我只是開始編程ahaha。我不知道從哪裏開始解決這個問題,所以我期待着直接的迴應或者是一個例子。謝謝! – 2013-04-10 00:41:21

回答

1

好的,我將使用jQuery作爲例子,因爲如果你是編程新手,這將是最容易完成的工作。

注:我不建議使用jQuery -BEFORE-學習JavaScript - DO IT AT自負,但至少BACK快來學習JavaScript LATER

首先,如何下載閱讀起來安裝jquery here。其次,你會想要一些這樣的東西,讓我們假裝這是你的標記。

<div id="wrapper"> 
    <!-- This output div will contain the output of whatever the MAIN 
     word being displayed is, this is where HOLA would be from your first example --> 
    <div id="output"> 

    </div> 

    <!-- This is your suggestions box, assuming all anchor tags in here will result in 
     a new word being displayed in output --> 
    <div id="suggestions"> 

    </div> 
</div> 

<!-- Le javascript --> 
<script> 
    // Standard jQuery stuff, read about it on the jquery documentation 
    $(function() { 
     // The below line is a selector, it will select any anchor tag inside the div with 'suggestions' as identifier 
     $('#suggestions a').click(function(e) { 
      // First, stop the link going anywhere 
      e.preventDefault(); 

      // Secondly, we want to replace the content from output with new content, we will use AJAX here 
      // which you should also read about, basically we set a php page, and send a request to it 
      // and display the result without refreshing your page 
      $.ajax({ 
       url: 'phppage.php', 
       data: { word: 'casar' }, 
       success: function(output) { 
        // This code here will replace the contents of the output div with the output we brought back from your php page 
        $('#output').html(output); 
       } 
      }) 
     }); 
    }) 
</script> 

希望評論能夠說明一些問題,然後您需要設置您的php腳本,它將發送一個GET請求。 (例如,http://some.address.com/phppage.php/?word=casar

然後你只需回聲出產量從PHP

<?php 

    $word = $_GET['word']; 
    // Connect to some database, get the definitions, and store the results 
    $result = someDatabaseFunctionThatDoesSomething($word); 
    echo $result; 
?> 

希望這會有所幫助,我希望你有很多的閱讀做的!

+0

弗朗西斯科傑出的解釋,但問題不是專門用「casa」這個詞,而是用任何可能有建議的詞。 – 2013-04-10 02:09:45

+0

事實上,我發佈的代碼工作原理如下:在西班牙語這裏添加任何單詞:但唯一的問題是像「casa」這樣的詞,你真的認爲我必須從頭開始用你的代碼從頭開始嗎? – 2013-04-10 02:11:58

+1

很高興你在工作,在真實世界的情況下,而不是我的例子,你會用錨標籤的文本替代單詞'casa'或其他東西。例如$(「suggestions a」)。text()而不是ajax中的靜態值'casa'。 – 2013-04-10 02:14:46