0

データベースからURLとクリック数を出力するこの作業クエリがあります。

<?php
$query = "SELECT *,count(*) FROM db_data WHERE ip GROUP BY data_url";
    $sampleCount = mysql_query($query);
    $result = mysql_query($query);
    if($sampleCount > 0) {
        while ($row =mysql_fetch_array($result)) {
            $dataList_br .= '<div class="info_table">
                                 <div class="info_table1">
                                     <p>' .$row['data_url']. '</p>
                                 </div>
                                 <div class="info_table2">
                                     <p>' .$row['count(*)']. '</p>
                                 </div>
                             </div>';
        }
    } else {
        $dataList_br .= '<p class="warning">No data found in database. Please contact admin.</p>';
    }

これにより、「/somepath/index.php」と合計数 (「24」など)、および合計ヒット数を含むその他の URL のセットが出力されます。

質問: URL を表示する代わりに、URL 出力 "/somepath/index.php" を "Page Name" のような文字列名に変更するにはどうすればよいですか。

どんな助けでも大歓迎です。

4

2 に答える 2

1

これを行うには3つの方法があります。

  1. URLのページ名を含むもう1つの列をテーブルに配置できます。あなたは単にそれを使用することができます。
  2. マッピングを連想配列として定数ファイルに入れることができます。そのURLのページ名の値を取得できる場所から。
  3. このケースを次のようにコードに入れることができます:

    $query = "SELECT *,count(*) FROM db_data WHERE ip GROUP BY data_url";
    $sampleCount = mysql_query($query);
    $result = mysql_query($query);
    if($sampleCount > 0) {
        while ($row =mysql_fetch_array($result)) {
            $dataList_br .= '<div class="info_table">
                                 <div class="info_table1">
                                     <p>' .
    if($row['data_url'] == "/somepath/index.php"){
         $html = file($row['data_url']);
         //or file_get_contents() method can be used.
         print_r($test['title']); // put index as per the html
    }else{
        echo $row['data_url']
    }. '</p>
                                     </div>
                                     <div class="info_table2">
                                         <p>' .$row['count(*)']. '</p>
                                     </div>
                                 </div>';
            }
        } else {
            $dataList_br .= '<p class="warning">No data found in database. Please contact admin.</p>';
        }
    

file()またはfile_get_contentsのいずれかを使用できます。違いは、file()が配列を返すのに対し、他のファイルは文字列を返すことです。

于 2012-06-27T17:08:52.660 に答える
1

これを使用すると、URL の代わりにページ タイトルを表示できます。

//let a url be so 
$url = 'http://google.com';

function file_get_contents_curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $data = curl_exec($ch);
    $info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    return $data;
}

//fetching url data via curl
$html = file_get_contents_curl($url);

if($html) {
    //parsing begins here
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $nodes = $doc->getElementsByTagName('title');
    $title = $nodes->item(0)->nodeValue;
    echo $title;
}
于 2012-06-27T16:57:26.257 に答える