php_b34st Posted September 21, 2006 Share Posted September 21, 2006 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 106line 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! Quote Link to comment Share on other sites More sharing options...
php_b34st Posted September 21, 2006 Author Share Posted September 21, 2006 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? Quote Link to comment Share on other sites More sharing options...
corbin Posted September 22, 2006 Share Posted September 22, 2006 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... Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 22, 2006 Share Posted September 22, 2006 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 Quote Link to comment Share on other sites More sharing options...
php_b34st Posted September 22, 2006 Author Share Posted September 22, 2006 Thank you kenrbnsn thats works Quote Link to comment Share on other sites More sharing options...
php_b34st Posted September 22, 2006 Author Share Posted September 22, 2006 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.