Jump to content

chrmlr

Members
  • Posts

    11
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

chrmlr's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thank you very much, that did the trick!
  2. Thanks for the reply. If I use float I get unknown function error. number_function doesn't return an error, but keeps them as whole numbers.
  3. Is it possible to force float as the key for at least 2 decimals? The code I have now is rounding the numbers down to integers. I want to keep them as floating point numbers. function rank() { $query="select * from ladder"; $result=mysql_query($query); while ($row=mysql_fetch_assoc($result)) { $ascarray[($row['win'] / $row['lose'])] = $row['userid']; } krsort($ascarray); return $ascarray;
  4. Thanks for the reply. That almost does it except for it saves it as an associative array and I just was a normal array. If I take out the mysql_assoc part I get this output when I print_r after your code. The array doesn't quite look right, but it loaded both rows. Array ( [0] => Array ( [0] => 1 [userid] => 1 [1] => 2314-4953-3952 [friendcode] => 2314-4953-3952 [2] => 5 [win] => 5 [3] => 3 [lose] => 3 [4] => 0 [disconnect] => 0 [5] => 0 [newbr] => 0 ) [1] => Array ( [0] => 3 [userid] => 3 [1] => 2314-4953-3952 [friendcode] => 2314-4953-3952 [2] => 23 [win] => 23 [3] => 7 [lose] => 7 [4] => 2 [disconnect] => 2 [5] => 0 [newbr] => 0 ) )
  5. Thanks for the reply! Is there a way to load all the rows one after each other into the array. ie if i have two rows and 2 fields userid and username then $array[0]=userid $array[1]=username $array[2]=userid $array[3]=username
  6. I'm still having trouble on the rank function. Everything should work except I'm only storing one row in $row and I want to store all of the rows. I think I need a while loop around $row=mysql_fetch_row($result); but I'm not exactly sure how to do it so it saves all the rows after each other. function rank() { $sql="select * from ladder"; $result=mysql_query($sql); $row=mysql_fetch_row($result); for($i=0; $i<(mysql_num_rows($result)*6); $i+=6) { $ascarray[(read(ladder, win, $row[$i]) / read(ladder, lose, $row[$i]))] = $row[$i]; } return krsort($ascarray); }
  7. Thanks, I have solved my problem. The read function should have looked like this: function read( $table, $field, $userid ) { $sql="SELECT $field FROM $table WHERE userid='$userid'"; $result=mysql_query($sql) or die(mysql_error()); $row=mysql_fetch_assoc($result); return $row["$field"]; }
  8. If I change the code to the following, it returns nothing. function read( $table, $field, $userid ) { $sql="SELECT $field FROM $table WHERE userid='$userid'"; $result=mysql_query($sql) or die(mysql_error()); $row=mysql_fetch_assoc($result); return $row['$field']; }
  9. Seems to work properly SELECT username FROM user WHERE userid='1'
  10. Thank you for all replys. I have made the changes you suggested. I haven't been able to test the rank function as it calls the read function, and the read function still displays an error. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1 <?php class ladderfunctions { function connect { HIDDEN } function selectdb( $db ) { mysql_select_db("$db")or die("cannot select DB"); } function read( $table, $field, $userid ) { $sql="SELECT $field FROM $table WHERE userid='$userid'"; $result=mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($result)) { // userid exists $sql="SELECT $field FROM $table WHERE userid='$userid'"; $result=mysql_query($result) or die(mysql_error()); $row=mysql_fetch_assoc($result); return $row['$field']; } } function write( $table, $field, $userid ) { } function rank() { $query="select * from ladder"; $result=mysql_query($query); $row=mysql_fetch_row($result); $array[mysql_num_rows($result)]; //Store userid into array for( int $i=0; $i<mysql_num_rows($result); $i++) { $array[$i]=$row[$i]; } //Sort userid in array by win/lose ratio function cmp($a, $b) { if( ((read(ladder, win, $a) / (read(ladder, lose, $a)) > read(ladder, win, $b) / read(ladder, lose, $b))) ) { return $a; } else return $b; } usort($array, "cmp"); return $array; } } ?>
  11. I'm working on my first php project, so I apologize if the questions seem a little simple . Thanks for your help it is greatly appreciated . Here is my classes.php file. I want the write function to take the table the field I'm looking for and the userid, but it is not working properly. I get this error when I try to run the function. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1 The rank function is supposed to load up all the userids in the database into an array and use usort to rank them according their win/lose ratio. I was a little confused on how to use the usort function. Any help here would be greatly appreciated. I'm still working on the write function, but I assume it will be pretty similar to the read function. <?php class ladderfunctions { function connect { [b]HIDDEN[/b] } function selectdb( $db ) { mysql_select_db("$db")or die("cannot select DB"); } function read( $table, $field, $userid ) { $sql="SELECT $field FROM $table WHERE userid='$userid'"; $result=mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($result)) { // userid exists $sql="SELECT $field FROM $table WHERE userid='$userid'"; $result=mysql_query($result) or die(mysql_error()); $row=mysql_fetch_assoc($result); return $row['$field']; } } function write( $table, $field, $userid ) { } function rank() { $query="select * from ladder; $result=mysql_query($query); $row=mysql_fetch_row($result); $array[mysql_num_rows($result)]; //Store userid into array for( int i=0; i<mysql_num_rows($result); i++) { $array[i]=$row[i]; } //Sort userid in array by win/lose ratio function cmp($a, $b) { if( ((read(ladder, win, $a) / (read(ladder, lose, $a)) > read(ladder, win, $b) / read(ladder, lose, $b))) ) { return $a; } else return $b; } usort($array, "cmp"); return $array; } } ?>
×
×
  • 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.