Jump to content

functions problems?


php_b34st

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!
Link to comment
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...
Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.