97096aca157a48f03443eae96ed2d118

I am looking for a way to retrieve the username of a user by comparing it to another database with only userid.
UserID and is unique for each username.

Its a little messed up but i dont know much php and mysql syntax :(

The code is what retrieves the latest high scores and displays them as SCORE (USERID)
I would like it to display SCORE (USER NAME)

primkey is in database AMCMS_highscores
userkey is in database AMCMS_users
AMCMS_users also contains field username.

So basically i want to get the high score that the person scored and display the username of the person that scored and not the USERID.

Thanks Everyone, i hope somebody can help me :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * am_getLatestHighScores
 *
 * 
 * 
 * 
 *
 * 
 */
	function am_getlatestscores($db, $cat) {
		$sql = "SELECT * FROM `AMCMS_highscores` ORDER BY `primkey`='$id' DESC LIMIT 10;";
		$res = am_queries($db, $sql);
		return $res;
	}	
	
/**
 * am_latesthighscores
 *
 *
 */
	function am_latesthighscores($db = NULL, $wholelinked = NULL, $list = NULL) {
		global $db;
		$icounter = 0;
		$numcats = am_numArcademGameCat($db, NULL);
		$res = am_getlatestscores($db, NULL);
		if ($wholelinked == 'linkall') {$first = ''; $second= '</a>';} else {$first = '</a>'; $second= '';}
		if ($list == 'bracket') {echo '[ ';$bracket = ' | ';}
		while ($icounter < $numcats) {
			if ($wholelinked!='none') {$numgamecat = ' ('.$res[$icounter][0].')';}
			if ($list == NULL && $list != 'flat') {
				echo '<li><a href="index.php?cat='.$res[$icounter][1] ,'">',$res[$icounter][1] , $first,$numgamecat,$second,'</li>';
			} elseif ($list != 'bracket' && $list != 'flat') {
				echo '<a href="index.php?cat=',$res[$icounter][1] ,'">',$res[$icounter][1] , $first,$numgamecat,$second;
			} elseif ($list == 'bracket' && $list != 'flat') {
				echo '<a href="index.php?cat=',$res[$icounter][1] ,'">',$res[$icounter][1] , $first,$numgamecat,$second,$bracket;
			} 
			if ($list == 'flat') {
				$retvalue[$icounter] = $res[$icounter][1];
			}
			++$icounter;
			if ($icounter == $numcats-1) {$bracket = ' ]';} else {$bracket = ' | ';}
		}
			if ($list == 'flat') {
				return $retvalue;
			}
	}

Refactorings

No refactoring yet !

5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

May 17, 2009, May 17, 2009 13:57, permalink

1 rating. Login to rate!

I'm not sure that the following code resolve your problems but I need more hints to better refactor your solution:
I found a lot of glitches in your code (I think it doesn't work) and I've tried to solve them but I was unable to do it.

on line 26 of your code you call am_getlatestscores with a NULL parameter (value you don't use in your query).
I suspect you have to pass to it a value (maybe a category?) but have no clues from where to get it.

You use numeric values to address the columns of the recordset you obtain from am_getlatestscores so I had
to guess the column names in order to obtain a query and retrieve your data.

In short, consider the refactoring just a hint to put you on the right track.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
    function am_getlatestscores($db, $cat) {
        $sql = "SELECT username, score FROM `AMCMS_highscores` JOIN `AMCMS_users` ON `primkey`=`userkey` WHERE `primkey`='$cat' ORDER BY `score` DESC LIMIT 10;";
		$res = am_queries($db, $sql);
		return $res;
	}	

	function am_latesthighscores($db = NULL, $wholelinked = NULL, $list = NULL) {
		$numcats = am_numArcademGameCat($db, NULL);
		$res = am_getlatestscores($db, NULL);

		$scoreTemplate = '<a href="index.php?cat=%s">%s'
                       . ($wholelinked=='linkall'?'%s</a>':'</a>%s');
		
        if ($list == NULL) {
			$scoreTemplate = '<li>'.$coreTemplate.'</li>';
        }

		foreach ($res as $record) {
			if ($wholelinked!='none') {
               $numgamecat = ' ('.$record[0].')';
            }
            retvalue[]=($list == 'flat')? $record[1]: sprintf($scoreTemplate, $record[1], $record[1], $numgamecat);
	}

        if ($list == 'flat') {
		  return $retvalue;
	}

        if ($list == 'bracket') {
          echo '[ '. implode(' | ', $retvalue) . echo ' ]';
        } else {
		  echo implode('', $retvalue);     
	}
   }
?>

Your refactoring





Format Copy from initial code

or Cancel