Jump to content

Archived

This topic is now archived and is closed to further replies.

php_b34st

functions problems?

Recommended Posts

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:

[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++;
}
[/code]

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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
To remove the commas:
[code]<?php
$num_str = "123,556,234";
$num_str = str_replace(',','',$num_str);
echo $num_str;
?>[/code]
To insert the commas:
[code]<?php
$num = 123445423;
$num_str = number_format($num,0);
echo $num_str;
?>[/code]

Ken

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.