1

I want to write simple counter for links/tags. So I have tags and random displayed them to the website

$zmienna = "SELECT name, link FROM tag_content ORDER BY RAND() LIMIT 4";
$result2 = mysql_query($zmienna);
echo $result2;
while($row=mysql_fetch_array($result2)){
    echo "<a href='http://www.simplelink.xx/tag/".$row['link']."'>".$row['name']."</a><br>";
}

And now I want to count how many users clicked tags. I created another row named "wys" and tried to write SQL stuff

$wtf = "UPDATE tag_content SET wys=wys+1 WHERE id=2";
$result3=mysql_query($wtf);

As u can see it only works for tag id = 2. And now the problem: how to make it work for all tags?

For example: I have 4 tags with different id. How to make counter "read" the actual clicked tag and make it add "1" to the "wys"?

Thx for help and let me know if u need more information (like code etc.)

3 Answers 3

2

As the link field is unique, you can simply use it as an identifier instead of id.

$wtf = "UPDATE tag_content SET wys=wys+1 WHERE link='".$something."";

where $something is a last part of page URL (you should parse it). Of course you also need to check this variable before you use it, because you got it from client side and it could have code for SQL-injection.

Sign up to request clarification or add additional context in comments.

6 Comments

Are you sure the link is unique? If so, why would he also provide a name? I guess there can be more tag names for one link, so the unique identifier would be name and link combined.
The id of the links is the id_tag. And the idea of yours is good (working also with id=""), but it will work for only one tag. How to make it "read" what id_tag user clicked?
@Johan Yes, I sure, because echo "<a href='http://www.simplelink.xx/tag/".$row['link']."'>- it must be unique
@Amay I mean that my code will be placed in the destination script that handles and shows various tag-pages. So it receives URL, extracts tag-link and updates the database row using it as unique identiifier.
So script should take from php id_tag, do SQL command to update database and send the result back to php? Then use <a onclick="something()" ...> option? If yes, could you tell me how the js script should look like?
|
1

You need a way to get the id of all links. Either provide it as a query parameter for the link, or use parse out the name and url from the clicked link and make a relevant SELECT to get the id to loop over.

Comments

-2

Use jquery :eq() selector to find out the exact link and then make an ajax request for updating the count

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.