0

だから私は若いいとこのためにピアノ図書館のウェブサイトを作っています。

現在、foreach 関数を使用して、データベースのすべてのデータを表示しています。さて、これはうまく機能し、いくつかの機能を機能させることができましたが、問題が発生したのは「カウンター」です。

エントリごとにカウンターが欲しいという事実を除けば、非常に簡単なコンセプトです。

「カウンター」とは、リンクをクリックした後、カウントに +1 を追加することを意味します。各リンクに「100回訪問」または「34回訪問」などがあります。

私は次のことを試しました:

if($mysqli){

    $result = mysqli_query($mysqli,"SELECT * FROM testtable ".$orderbyfilter);
    $rows = $result->fetch_all(MYSQLI_ASSOC);

    foreach($rows as $row) {
        echo "<tr id='entry'><td>";
        echo ucwords($row['name']);
        echo "</td><td align='center'>";
        echo '<a href="' . $row['url'] . '">url</a>';
        echo "add hit:";
        echo "<a href='?action=callfunction'>Click</a>";


        //current counter script

        if(isset($_GET['action']) && $_GET['action'] == 'callfunction'){
            $hitcount = $row['hitcount'] + 1;
            $id = $row['id'];

            // why doesn't this work?
            $sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='".$id."'"; 
            $result=mysqli_query($con,$sql); 
        }

        echo "</td><td align='center'>";
        echo $row['level'];
        echo "</td><td align='center'>";
        echo $row['hitcount'];
        echo "</td></tr>";

    }

        mysqli_close($mysqli);

    } else {
        echo "table did not correctly display!";
}

明らかに方法:

$sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='".$id."'";

リンクをクリックすると、すべてのエントリが同じヒット数で更新されるため、機能しません。ただし、次のように変更すると:

$sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='2'";

で行のヒットカウントのみを変更する場合、完全に機能しid=2ます。

明らかに、問題は"foreach"and を変数として設定することに関係して$row[id]いますが、正直なところ、助けが必要です。

変数の変数と関係がありますか?私は見当もつかない。どんな助けでも大歓迎です。

4

2 に答える 2

0

このようにクエリを変更してカウンターを更新するだけです

$sql="UPDATE testtable SET `hitcount`=hitcount+1 WHERE id=".$id;

カウンターの値を一重引用符で囲まないでください。integer

于 2015-02-04T05:25:11.920 に答える
0

これが私のために働いたものです。必要に応じて変更できます。

必要だったのは、URL で関連する「id」の追加の GET パラメータを渡し、その変数をWHERE句で渡すことでした。

関連する URL が特定の行にクリックされると自動的にリダイレクトして結果を表示し、次の警告を防ぐために、ヘッダーも追加されました。

警告: mysqli_fetch_array() は、パラメーター 1 が mysqli_result であると想定しています。

補足:追加情報については、以下のコードに残されたコメントをお読みください。

<?php 
ob_start(); // keep this, it's for the header

$DB_HOST = 'xxx'; // Replace
$DB_USER = 'xxx'; // with
$DB_PASS = 'xxx'; // your
$DB_NAME = 'xxx'; // own

$Link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($Link->connect_errno > 0) {
  die('Connection failed [' . $Link->connect_error . ']');
}

// Your original query. You can replace it if needed.
// $result = mysqli_query($Link,"SELECT * FROM testtable ".$orderbyfilter);

$result = mysqli_query($Link,"SELECT * FROM testtable");

 while ($row = mysqli_fetch_array($result)) {

    echo "<tr id='entry'><td>";
    echo "ID: " . $row['id'];

    $var = $row['id']; // used for the URL

    echo "<br>";

    echo $row['hitcount'];
    echo ucwords($row['name']);
    echo "</td><td align='center'>";
    echo '<a href="' . $row['url'] . '">url</a>';
    echo " Add hit: ";
    echo "<a href='?action=callfunction&id=$var'>Click</a> ";

    if(isset($_GET['action']) && $_GET['action'] == 'callfunction'){

// Used for the query and escape the URL
$var = mysqli_real_escape_string($Link, $_GET['id']); 

$sql="UPDATE testtable SET hitcount = hitcount +1 WHERE id='".$var."'"; 

$result=mysqli_query($Link,$sql); 

// Added a header, otherwise the old method will generate the following warning
// Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in...
header("Location: http://www.example.com/your_counter_file.php");
exit; // Keep this in order to stop further execution

// ^ Change the url in header above to reflect your Web site's address

    } // Closing brace for isset

    echo "</td><td align='center'>";
    echo $row['level'];
    echo "</td><td align='center'>";
    echo $row['hitcount'];
    echo "</td></tr>";

} // Closing brace for while loop

    mysqli_close($Link);
于 2015-02-04T16:35:58.733 に答える