@byte Posted February 10, 2020 Share Posted February 10, 2020 I got a template that shows a linklist in tabs. I would like to increase a integer field by 1 when the user clicks on the link. Is it possible to get the value of that field and add 1 to it and save it to the database? Link to comment Share on other sites More sharing options...
dragan Posted February 10, 2020 Share Posted February 10, 2020 That's a classic use-case for Google Tag Manager. But you can do it yourself with a bit of JS + PHP. Basically: assign a CSS class to these links, and write a litte function that posts the link href attribute to a PHP script, which in turn updates a field in PW. 1 Link to comment Share on other sites More sharing options...
dragan Posted February 10, 2020 Share Posted February 10, 2020 basic example / ideas: document.addEventListener("DOMContentLoaded", function (event) { const trackedLinks = document.querySelectorAll("#content a"); // adjust selector as needed trackedLinks.forEach(function (n) { n.addEventListener("click", function (e) { e.preventDefault(); const href = e.target.getAttribute('href'); console.log(href); // api = special page with tpl and URL-segments enabled: fetch(`/api/linktracker/?url=${href}` , { method: "GET" }) window.location = href; }); }); }); (That's vanilla JS / ES6) if ($input->urlSegment1 === 'linktracker' && isset($_GET['url'])) { $url = trim($_GET['url']; // save to a textarea inside PW: $pg = $pages->get("123"); $pg->of(false); $pg->linktracker = $pg->linktracker . "\n" . $url); $pg->save(); // save to a custom DB table: $myDB = new PDO("mysql:dbname=linktracker;host=localhost", "root", "******", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")); $trackLinkQuery = "UPDATE tablename SET click_count = click_count + 1 WHERE url=`$url`"; $query = $myDB->prepare($trackLinkQuery); $query->execute(); // udpate a text-file file_put_contents('path/to/file.log', $url); // if those links are saved inside repeaters or similar, add one int field that you can increment / update: $pg = $pages->get("123"); $pg->of(false); $pg->linktracker = $pg->linktracker +1; // or whatever / wherever your field is.... $pg->save(); } These are 4 basic ways how you could handle it - there are certainly more ? Hope that gives you some ideas. 2 Link to comment Share on other sites More sharing options...
@byte Posted February 10, 2020 Author Share Posted February 10, 2020 I got a repeater for the tabs witch has another repeater with all the links and a field 'link_ Count'. I would like to use the last example. How does your code know witch one to change? Link to comment Share on other sites More sharing options...
@byte Posted February 10, 2020 Author Share Posted February 10, 2020 This is my code: <?php include("./includes/head.inc"); ?> <div class="col-10-12"> <div class="toptitle"><?php echo $page->title; ?></div> <div id="content" class="content"> <?php $n = 0; $tabStyle = "<style type='text/css'>"; foreach($page->link_Top_Kategorie as $kategorie) { $n++; if ($n == 1){ echo "<input id='tab{$n}' type='radio' name='tabs' class='inputtab' checked>\n"; $tabStyle .= "#tab{$n}:checked ~ .tabcontent #content{$n}"; } else { echo "<input id='tab{$n}' type='radio' name='tabs' class='inputtab'>\n"; $tabStyle .= ",#tab{$n}:checked ~ .tabcontent #content{$n}"; } echo "<label for='tab{$n}' class='labeltab'>{$kategorie->link_Kategorie}</label>\n"; } $tabStyle .= "{display: block;}</style>"; echo $tabStyle; ?> <div id="tabcontent" class="tabcontent"> <?php $n = 0; foreach($page->link_Top_Kategorie as $kategorie) { $n++; echo "<div id='content{$n}'><p>\n"; foreach($kategorie->link_Repeater as $content) { $image = $content->link_Thumb; echo "<table class='tablelist'><tbody>"; echo "<tr><td rowspan='3' class='tablelist_Image'><img src='{$image->url}' alt='{$image->description}' width='80' /></td>"; echo "<td class='tablelist_Name'><strong>{$content->link_Name}</strong></td></tr>"; echo "<tr><td class='tablelist_Content'>{$content->link_Content}</td></tr>"; echo "<tr><td class='tablelist_Name'><span style='float:left;width:49%;text-align:left;'><a href='{$content->link_Link}' target='_blank' rel='noreferrer noopener'>Link besuchen</a></span><span style='float:right;width:49%;text-align:right;'>{$content->link_Count} mal besucht</span></td></tr>"; echo "</tbody></table>"; } echo "</p></div>"; } ?> </div> </div> </div> </div> <?php include("./includes/foot.inc"); ?> Link to comment Share on other sites More sharing options...
@byte Posted February 11, 2020 Author Share Posted February 11, 2020 Thanks a lot dragan for your help. I had to change one part $pg = $pages->get("link_Link=$url"); Link to comment Share on other sites More sharing options...
@byte Posted February 18, 2020 Author Share Posted February 18, 2020 Hi, i am using $config->ajax to change only the content of a page. Now the above solution is not working anymore because the page is not reloaded, so the Event Listener is not loaded. Any Idee how to solve it? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now