Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:20:31+00:00 2026-05-27T22:20:31+00:00

Probably a dead simple and idiotic question (I’m totally new to javascript): I have

  • 0

Probably a dead simple and idiotic question (I’m totally new to javascript):

I have this code that loads a new post by clicking on a “next” or “back”-link. The clicks variable is used to scroll up and down in the sql-limit-statement (using the swapContent function), means you move backward or forward in the database by clicking the links. It works easy and perfectly:

<script type="text/javascript">
var clicks = -1;
function increase()
{
    clicks++;
    return false;
}
function decrease()
{
  clicks--;
  return false;
}
</script>
<div id="<?php echo $post['id'].'-multipost'; ?>">
 <?php include('views/posts/_postmultipost.php'); ?>
</div>
<div id="<?php echo $post['id']; ?>-next" class="rightbutton" style="display:block;">
 <a href="#" onmousedown="increase(); javascript:swapContent('next', clicks, '<?php echo $post['id']; ?>', '<?php echo $post['title']; ?>', '<?php echo $_SESSION['user']['id']; ?>');">next</a>
</div>

<div id="<?php echo $post['id']; ?>-back" class="leftbutton" style="display:none;">
 <a href="#" onmousedown="decrease(); javascript:swapContent('back', clicks, '<?php echo $post['id']; ?>', '<?php echo $post['title']; ?>', '<?php echo $_SESSION['user']['id']; ?>');">back</a>
</div>

The only problem: As you see I have several posts (post-IDs). But the javascript var “clicks” is always the same. How can I add the post-id into the javascript variable name “clicks”, well, something like this :

var <?php echo $post['id']; ?>-clicks = -1;

Of course it doesn’t work this way, but I have no clue how to manage it. Any advice? Sorry for this stupid question…

Thanks for your help!

UPDATE

Ok, got the solution: Bryan was right!!!

Changed the code to:

<script type="text/javascript">
var clicks = {};
clicks['<?php echo $post['id']; ?>'] = -1;
function increase()
{
    clicks['<?php echo $post['id']; ?>']++;
    return false;
}
</script>

The javascript in html stays as it is:

<a href="#" onmousedown="increase(); swapContent('next', clicks, '<?php echo $post['id']; ?>', '<?php echo $post['title']; ?>', '<?php echo $_SESSION['user']['id']; ?>');">></a>

Clicks is now an object and will output the following in the swapContent-Function:

count: Array
(
    [80] => 0
)

In php you would access the value like this:

foreach($count as $key=>$value) { $count = $value }

In javascript it seems to work a bit different like this:

for(x in clicks)
{
   var clicks = clicks[x];
}

Seems to work perfectly now, thanks for your help!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:20:31+00:00Added an answer on May 27, 2026 at 10:20 pm

    I’m not incredibly familiar with PHP, so I don’t know about php echo. However, would using an object work?

    var postClicks = {};
    
    postClicks['<?php echo $post['id']; ?>'] = -1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

another fun (and probably really simple) question for you, that I have half worked
Probably a long question for a simple solution, but here goes... I have a
I think this is a namespace issue, and the answer is probably dead simple,
It's probably a dead simple question. Right now it uses the default application layout
This is probably dead simple, but I can't get it to work. How do
Okay, I know this is probably dead simple, but I can't seem to find
I know this is probably dead simple, but I've got some data such as
Probably a really simple one this - I'm starting out with C# and need
This is probably a very stupid math question, but i can't seem to figure
As similar to this question (there are more related entries, however as a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.