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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:00:07+00:00 2026-06-02T21:00:07+00:00

I have developed a fairly ramshackle PHP/JavaScript Reward system for my school’s VLE. The

  • 0

I have developed a fairly ramshackle PHP/JavaScript Reward system for my school’s VLE.

The main bulk of the work is done on a transactions table which has the following fields:

Transaction_ID, Datetime, Giver_ID, Recipient_ID, Points, Category_ID, Reason

The idea is that if I, as a member of staff, give a student some Reward Points, an entry such as this is inserted into the database:

INSERT INTO `transactions` (`Transaction_ID`, `Datetime`, `Giver_ID`, `Recipient_ID`, `Points`, `Category_ID`, `Reason`) VALUES
(60332, '2012-02-22', 34985, 137426, 5, 5, 'Excellent volcano homework.');

This worked fine – but I didn’t really consider just how much the system would be used. I now have over 72,000 transactions in this table.

As such, a few of my pages are starting to slow down. For instance, when staff try to allocate points, my system runs a command to get the member of staff’s total point allocation and other snippets of information. This appears to be displaying rather slowly, and looking at the MySQL statement/accompanying PHP code, I think this could be much more efficient.

function getLimitsAndTotals($User_ID) {

    $return["SpentTotal"] = 0;
    $return["SpentWeekly"] = 0;

    $sql = "SELECT * 
        FROM `transactions` 
        WHERE `Giver_ID` =$User_ID";
    $res = mysql_query($sql);

    if (mysql_num_rows($res) > 0) {
        while ($row = mysql_fetch_assoc($res)) {
            $return["SpentTotal"] += $row["Points"];

            $transaction_date = strtotime ($row["Datetime"]);

            if ($transaction_date > strtotime( "last sunday" )) {
                $return["SpentWeekly"] += $row["Points"];
            }
        }
    }

    return $return;
}

As such, my question is twofold.

  1. Can this specific code be optimised?
  2. Can I employ any database techniques – full text indexing or the like – to further optimise my system?

EDIT: RE Indexing

I don’t know anything about indexing, but it looks like my transactions table does actually have an index in place?

Transaction table index

Is this the correct type of index?

Here is the code for table-creation:

CREATE TABLE IF NOT EXISTS `transactions` (
  `Transaction_ID` int(9) NOT NULL auto_increment,
  `Datetime` date NOT NULL,
  `Giver_ID` int(9) NOT NULL,
  `Recipient_ID` int(9) NOT NULL,
  `Points` int(4) NOT NULL,
  `Category_ID` int(3) NOT NULL,
  `Reason` text NOT NULL,
  PRIMARY KEY  (`Transaction_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=74927 ;

Thanks in advance,

  • 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-06-02T21:00:09+00:00Added an answer on June 2, 2026 at 9:00 pm

    Make sure Giver_ID is indexed. Try also running the strtotime outside of your while loop as I imagine its an expensive operation to be running 72,000 times.

    if (mysql_num_rows($res) > 0) {
    
        // assign the unix timestamp of last sunday here.
        $last_sunday = strtotime('last sunday');
    
        while ($row = mysql_fetch_assoc($res)) {
            $return["SpentTotal"] += $row["Points"];
    
            $transaction_date = strtotime ($row["Datetime"]);
    
            if ($transaction_date > $last_sunday) {
                $return["SpentWeekly"] += $row["Points"];
            }
        }
    }
    

    Also consider running UNIX_TIMESTAMP(Datetime) AS datetime_timestamp in your SQL instead of getting it out as a string and running another expensive strtotime operation. You can then simply run:

    if (mysql_num_rows($res) > 0) {
    
        // assign the unix timestamp of last sunday here.
        $last_sunday = strtotime('last sunday');
    
        while ($row = mysql_fetch_assoc($res)) {
            $return["SpentTotal"] += $row["Points"];
    
            if ($row['datetime_timestamp'] > $last_sunday) {
                $return["SpentWeekly"] += $row["Points"];
            }
        }
    }
    

    (if your column is of type DATE, of course!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to Rails and I have never developed a large application.
I have developed a Web application (HTML5/browser based) for a client which requires fairly
I have a fairly large MVC3 application, of which I have developed a small
I have a system that we have recently developed - a web application over
I have developed a fairly small asp.net MVC application using the repository pattern and
Have any bridge libraries been developed for PHP that provide access to the jQuery
We have a system that was originally developed decades ago and has increased in
I work on two related web applications, developed in PHP, that started independently. Over
I have developed a simple library in Ruby and need to use this in
I have developed a web service and host it on my server and I

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.