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 6161299
In Process

The Archive Base Latest Questions

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

Using PHP I am retrieving a list of videos. I am using jquery to

  • 0

Using PHP I am retrieving a list of videos. I am using jquery to display the video in an overlay. I have everything working, but the problem is the method of implementation seems a little brute force to me and I am looking for a way to cut out the redundant code.

Basically I get a list of videos from the database and output those using a foreach loop to the screen. I use the ID of the video to make the like link id for each result unique:

<a id=like<?php echo $video['fileID'] ?>" ...

I then have to add the jquery to each iteration of the loop. Its an ajax call that passes the information about the video, the current user, etc. If the current user votes for a video then the jquery ajax function is called and a new vote count is displayed:

//upVote for a discussion
    $('#like<?php echo $video['fileID'] ?>').click(function(){
    $.ajax({
        type:"POST",
        url:'/ajax/likeFile.php',
        data:"voter=" + <?php echo (isset($_SESSION['user_id']) ? $_SESSION['user_id'] : ''); ?>
        + "&poster=" + <?php echo $video['userID'] ?>
        + "&classID=" + <?php echo $_SESSION['class'] ?>
        + "&lessonID=" + <?php echo $_SESSION['lesson'] ?>
        + "&id=" + <?php echo $video['fileID'] ?>
        + "&type=like",
        success: function(data) {
        var $response=$(data);
        var votes = $response.filter('#voteCount').text();
        $('#fileVotes').text(votes);
        $('#like').attr('src', '/images/upvoteDisabled.png');
        $('#unlike').attr('src', '/images/downvote.png');
        }

    });
    });

Like I said, this is working fine. The problem is that I would like to cut down on repeating the jquery over and over for each link. Is there a way to call this and just have the item that gets selected pass its current loop iteration variables instead of having create a jquery function for each link?

This is a really convoluted question I know. Thanks in advance to anyone who wants to tackle this.

  • 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-23T21:27:21+00:00Added an answer on May 23, 2026 at 9:27 pm

    First, set an HTML5 DOCTYPE so you can take advantage of custom data attributes:

    <!DOCTYPE HTML>
    

    Then have PHP populate your data attributes for each anchor:

    <a class="like-btn" id="like<?=$video['fileID']?>" data-voter="<?php echo (isset($_SESSION['user_id']) ? $_SESSION['user_id'] : '');" data-poster="<?=$video['userID']?>" data-class="<?=$_SESSION['class']?>" data-lesson="<?=$_SESSION['lesson']?>" data-file="<?=$video['fileID']?>"> ... </a>
    

    Then use a single JQuery click handler:

    $('a.like-btn').click(function(){
        $.ajax({
            type:"POST",
            url:'/ajax/likeFile.php',
            data:"voter="  + $(this).data('voter')
            + "&poster="   + $(this).data('poster')
            + "&classID="  + $(this).data('class')
            + "&lessonID=" + $(this).data('lesson')
            + "&id="       + $(this).data('file')
            + "&type=like",
            success: function(data) {
                var $response=$(data);
                var votes = $response.filter('#voteCount').text();
                $('#fileVotes').text(votes);
                $(this).attr('src', '/images/upvoteDisabled.png');
                //$('#unlike').attr('src', '/images/downvote.png'); // fix in your DOM
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with retrieving data from a database using PHP statements. At
I have a list and i am adding a value using add(int,Object) method but
I have been trying to display image from database using php but i am
I have an issue retrieving ajax data using jquery 1.3.2 / 1.4.2: It works
With jQuery I'm retrieving positions of a sortable list using 'serialize', like this: var
I am retrieving values from a database and displaying it a table using php.
Using PHP and MySQL, I have a forum system I'm trying to build. What
Using PHP, I am trying to delete a record, but I want to check
using php and mysql I have two tables, a users table and a profiles
I'm sending MapMessages in Java to ActiveMQ and retrieving them using Stomp in PHP.

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.