Jump to content

Increase counter field


@byte
 Share

Recommended Posts

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

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.

  • Like 2
Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...