php - How to Saveing page views in database -
i have page show 50 images user , user can click on image , load image in page. want save every image view database i.e. counter clicked imageid. i've made far add row table called hits
. here show images:
$result = $pdo->prepare("select * images order id asc limit $start_from, 50"); $result->execute(); for($i=0; $row = $result->fetch(); $i++) { echo ' <h1><a href="post.php?id='.$row['id'].'">'.$row['title'].'</a></h1> <img src="../upload/'.$row['name'].'" alt=""/>'; }
in post.php
user see image clicked on have this:
if(isset($_get['id']) && is_numeric($_get['id'])) { $id = $_get['id']; $result = $pdo->prepare("select * images id= ?"); if ($result->execute(array($_get['id']))) { for($i=0; $row = $result->fetch(); $i++) { echo ' // source show image '; } } }
what understand must update hits
row this
update images set hits = hits + 1 id = idofimage
what can't understand how implement update in current code. where?
any appreciated.
you have solved
just add update code in same place displaying image
if(isset($_get['id']) && is_numeric($_get['id'])) { $id = $_get['id']; $result = $pdo->prepare("select * images id= ?"); if ($result->execute(array($_get['id']))) { //replace if because getting 1 result if($row = $result->fetch()) { //update hits $update = $pdo->prepare("update images set hits = hits + 1 id = ?"); //i prefer binding values , have call execute() //but both options ok //$update->bindparam(1, $_get['id'], pdo::param_int); //$update->execute(); $update->execute(array($_get['id'])); echo ' // source show image '; } } }
Comments
Post a Comment