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

  • Home
  • SEARCH
  • 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 8881019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:15:45+00:00 2026-06-14T20:15:45+00:00

I have a problem with the following function: function get_organization_score($org_id) { $sql1 = SET

  • 0

I have a problem with the following function:

function get_organization_score($org_id)
{
  $sql1 = "SET @rownum := 0";
  $sql2 = "SELECT rank, xp_total FROM (
           SELECT @rownum := @rownum + 1 AS rank, g_org.id AS org_id,
           (Sum(g_npc.xp) + (Sum(g_npc.level)*128)) AS xp_total FROM g_org
           LEFT JOIN g_npc ON g_org.id = g_npc.g_org_id GROUP BY g_org.id
           ORDER BY xp_total DESC
                          ) as result WHERE org_id='".$org_id."'";
  mysql_query($sql1);
  $result = mysql_query($sql2);
  $rows = '';
  $data = array();
  if (!empty($result))
      $rows = mysql_num_rows($result);
  else
      $rows = '';
  if (!empty($rows)){
      while ($rows = mysql_fetch_assoc($result)){
          $data[] = $rows;
      }
  }
  if (empty($data[0]['rank']))
      return 1;
  return $data[0]['rank'];
}

This code is to show a organization rank in a scoreboard.
So for example when I give organization ID then I get its rank number positioned in the scoreboard.

Currently the problem is that I don’t get the rank number but the ID itself that I am calling the function as organization id.

Example:
get_organization_score(1)
then I get rank number as 1
or when I call
get_organization_score(34)
then I get rank number as 34

but I want its position in the scoreboard ordered by the XP amount that comes from other table by left join.

Can anyone help? If any questions please ask…

EDIT:
Adding table schema and sample data:

CREATE TABLE IF NOT EXISTS `g_org` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `org_name` varchar(255) NOT NULL,
  `founded` datetime NOT NULL,
  `g_userid` int(255) NOT NULL,
  `g_username` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `g_userid` (`g_userid`),
  KEY `g_username` (`g_username`(191)),
  KEY `g_username_2` (`g_username`),
  KEY `org_name` (`org_name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=47 ;

INSERT INTO `g_org` (`id`, `org_name`, `founded`, `g_userid`, `g_username`) VALUES
(1, 'test_org_1', '2012-07-04 02:30:56', 1, 'DDD'),
(2, 'test_org_2', '2012-07-04 02:34:57', 2, '777');

--
-- Table structure for table `g_npc`
--

CREATE TABLE IF NOT EXISTS `g_npc` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `g_userid` int(255) NOT NULL,
  `g_username` varchar(255) NOT NULL,
  `g_org_id` int(255) NOT NULL,
  `g_org_name` varchar(255) NOT NULL,
  `g_npc_name` varchar(255) NOT NULL,
  `level` int(255) NOT NULL,
  `xp` int(255) NOT NULL,
  `last_update` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `g_userid` (`g_userid`),
  KEY `g_username` (`g_username`),
  KEY `g_org_id` (`g_org_id`),
  KEY `g_org_name` (`g_org_name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=181 ;


INSERT INTO `g_npc` (`id`, `g_userid`, `g_username`, `g_org_id`, `g_org_name`, `g_npc_name`, `level`, `xp`, `last_update`) VALUES
(1, 1, 'DDD', 1, 'test_org_1', 'Kinuzehute Doqasi', 4, 155, '2012-11-13 14:30:54'),
(2, 1, 'DDD', 1, 'test_org_1', 'Zabava Qigatagise', 2, 107, '2012-10-30 17:38:12'),
(3, 2, '777', 2, 'test_org_2', 'Roci Vuzoducu', 6, 135, '2012-11-13 23:17:40'),
(4, 2, '777', 2, 'test_org_2', 'Gexoye Zeze', 4, 638, '2012-11-06 02:02:27'),
(5, 2, '777', 2, 'test_org_2', 'Sibalabu Gozi', 9, 285, '2012-11-06 02:02);

When I call a function get_organization_score(2); then the organization id would be 2 and I would like to make a query to table ‘g_org’ with ID as 2 and LEFT JOIN to table ‘g_npc’ to get all entries with ‘g_org_id’ value 2 in ‘g_npc’ table and SUM up all results from ‘g_npc’ table field ‘xp’ and then ORDER BY all those results by field ‘xp’ DESC.

Then the remaining PHP code with @rownum would give this organization a ranking among the other organizations.

So the following function by organization ID 2 would result the ranking to be 1 because it has more XP in the ‘xp’ field and when I would call the same function with organization ID 1 then the ranking number would be 2 because it has smaller number in the ‘XP’ field.

Basicly I dont want to result a list of organization but only the ranking number. Nothing else.

The ending should have WHERE org_id='”.$org_id.”‘ because it needs to get the ranking number for only 1 entry.

  • 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-14T20:15:48+00:00Added an answer on June 14, 2026 at 8:15 pm

    The only thing I can think of is this awfully slow code:

    select rank from (
      select s1.id, @row := @row + 1 rank from (
        select
          o.id,
          sum(coalesce(n.xp, 0) + coalesce(n.level, 0) * 128) totalXP
        from g_org o
        left join g_npc n on (o.id = n.g_org_id)
        group by o.id
        order by totalXP desc
      ) s1, (select @row := 0) init
    ) s2
    where id = 1
    

    Note there is no need to run the SET query and remember to replace id = 1 with whatever you’re using in PHP.

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

Sidebar

Related Questions

I have the following function. The problem is that the $lang variable will vary
In my home work of Ackermann function I have solved the problem as following
The problem is the following. We have lots of ajax call via $.ajax function.
I have problems with following code: http://lisper.ru/apps/format/96 The problem is in normalize function, which
Never ran into this problem with jQuery before. I have the following: $(document).ready(function() {
I have a problem following from my previous problem . I also have the
I have the following problem. I need to pass the hello function to name
I have reduced the problem to the following basic function which should simply print
I have the following problem: Let's consider we have #define SET callMe #define COLUMN(x)
I have problem with the following code : function KeyCheck(e) { // ... Some

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.