Php mysql while loop fetching rows -
i want fetch experience
row database, level players accordingly experience, is, level equal experience ÷ 100.
for example, if player's experience 100, he's level 1. if it's 200, he's level 2.
here's code:
<?php $con = mysqli_connect("localhost", "username", "password", "db"); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $result = mysqli_query($con, "select * playerdata order bank desc limit 9999"); while($row = mysqli_fetch_array($result)) { $row['unique_id']; // ? echo "<br>"; $xp = $row['experience']; if ($xp == '1') { echo 'you\'re level one'; } if ($xp == '2') { echo 'you\'re level 2'; } } mysqli_close($con); ?>
i don't know how handle while statement while loop yet.
how can calculate level users?
if want calculate level accordingly experience, answer question:
<?php // simulates database. $players = [ ['unique_id' => 1, 'experience' => '100'], ['unique_id' => 2, 'experience' => '150'], ['unique_id' => 3, 'experience' => '200'], ['unique_id' => 4, 'experience' => '299'], ['unique_id' => 5, 'experience' => '320'], ]; foreach ($players $player) { // basically, want level dividing experience 100, // rounding down function floor(). no need loop. $level = floor($player['experience'] / 100); echo 'player #', $player['unique_id'], ' level ', $level, "\n"; }
this outputs:
player #1 level 1
player #2 level 1
player #3 level 2
player #4 level 2
player #5 level 3
now, if want update level users, suggest 1 of following:
- use cron job calculates level of users (using method described above) , update database.
- update users' level when experience changes.
Comments
Post a Comment