So I'm creating a piano library website for my younger cousin.
Currently I use the foreach function to display all the data from my database. Now, this works great and I managed to get a few features working, but one I'm having trouble with is a "counter".
Super easy concept, except the fact that I'd like a counter for every single entry.
By "counter" I mean that after clicking a link, it would add a +1 to the count. So that each link would have "Visited 100 times" or "Visited 34 times", etc.
I have tried the following:
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!";
}
Obviously the method:
$sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='".$id."'";
Doesn't work, as when I click the link, it updates all entries with the same hit count. However when I change it to:
$sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='2'";
It works perfectly, where it only modifies hitcount for the row with id=2
.
Obviously the problem has to do with the "foreach"
and setting $row[id]
as a variable, but honestly I could use some help.
Does it have something to do with variable of variables? I have no clue. Any help is appreciated.
Here's what worked for me. You can modify it as per required.
What needed to be done was to pass an additional GET parameter for the related "id" in the URL, then pass that variable for it in the WHERE
clause.
A header was also added in order to automatically redirect once a related URL to the given row has been clicked and show the results and to prevent the following warning:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in...
Sidenote: Read the comments left in the code below for additional information.
<?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);