Jump to content


Photo

functions problems?


  • Please log in to reply
5 replies to this topic

#1 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 21 September 2006 - 08:49 PM

Hi I have a script which gets all the usernames out of one table in the db then uses the username to get the relevant stats for that user out of a different table in the db. The script works fine but when i try to sort the data i get an error because im trying to declare the same funtion for every user.

here is a snippet of the relevant code:

$info = array();
$i = 0;
while ($row = mysql_fetch_assoc($result))
{
	$info[$i]['username'] = $row['rsusername'];
	$info[$i]['user_id'] = $row['user_id'];
	$i++;
}
$i = 0;
foreach ($info as $r => $value)
{
	$user = $info[$r]['username'];
	$id = $info[$r]['user_id'];
	
	$query = "SELECT * FROM stats WHERE user='$user' AND cat='$skill'";
	$result = mysql_query($query) or die("Could not query: " . mysql_error());
	
	$data = array();
	$i = 0;
	while ($row = mysql_fetch_assoc($result))
	{
		$data[$i]['cat'] = $row['cat'];
		$data[$i]['statA'] = $row['statA'];
		$data[$i]['statB'] = $row['statB'];
		$data[$i]['statC'] = $row['statC'];
		if ($skill == 'Combat' || $skill == 'Overall')
		{
			$order = $data[$i]['statB'];
		}
		else
		{
			$order =  $data[$i]['statC'];
		}
		$data[$i]['order'] = str_replace(",", "", $order);
		$i++;
	}
	
	function desc($a, $b)
	{
		if($a['order'] == $b['order'])
		return 0;
		else
		return ($a['order'] > $b['order']) ? -1 : 1;
	}
	usort($data, 'desc');

	foreach ($data as $r => $value)
	{
		echo '<tr>
			<td class="row1"><span class="genmed">' .$rank . '</span></td>
			<td class="row1"><span class="genmed"><a href="profile.php?mode=viewprofile&u=' . $id . '">' . $user . '</a></span></td>
			<td class="row1"><span class="genmed">' . $data[$r]['statA'] . '</span></td>
			<td class="row1"><span class="genmed">' . $data[$r]['statB'] . '</span></td>
			<td class="row1"><span class="genmed">' . $data[$r]['statC'] . '</span></td>
		      </tr>';
		$i++;
	}
	$i++;
}

I get the following error:

Fatal error: Cannot redeclare desc() (previously declared in C:\web\exigence\1.1\hiscore1.php:106) in C:\web\exigence\1.1\hiscore1.php on line 106

line 106 is function desc($a, $b)

I think the problem is that this function can only be declared once but the problem is if i declare it outside the foreach loop then it doesnt sort the stats.

Is there a way around this or another way of sorting the data? I cannot simply just use ORDER BY in the mysql statement as the figures in the db have commas in (ie. 156,345,344 is 156345344)

Hope this makes sense and thank you in advance!

#2 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 21 September 2006 - 11:29 PM

Or maybe  if I did i the other way? If I removed the commas before i enter the data into the db then I could just use ORDER BY? but then how would I put the commas back in? ie turn 120500600 into 120,500,600?

#3 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 22 September 2006 - 12:35 AM

Ummm not sure about your first question but for the comma thing i would use something like

<?
$str = explode(',', $num);
$i = 0;
while(count($str) > $i) {
if($str[$i] != ",") {
$num2 .= $str[$i];
}
}
?>

Then $num2 would contain the number with just numerals... Probably a more effecient way to do it though...
Why doesn't anyone ever say hi, hey, or whad up world?

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 September 2006 - 12:46 AM

To remove the commas:
<?php
$num_str = "123,556,234";
$num_str = str_replace(',','',$num_str);
echo $num_str;
?>
To insert the commas:
<?php
$num = 123445423;
$num_str = number_format($num,0);
echo $num_str;
?>

Ken


#5 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 22 September 2006 - 12:56 AM

Thank you kenrbnsn thats works

#6 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 22 September 2006 - 03:13 PM

ALthought that part works for adding or taking away the commas I still cannot get the data in the order I need it to be in. I used ORDER BY within the mysql statement but there was no change I believe this is because the mysql statement is in another foreach loop so the data is being passed to it in a certain order therefore my mysql statement cannot do anythingwith it! Is there anything I could do about my original problem?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users