[PHP]A way of doing this shorter?

Results 1 to 3 of 3
  1. #1
    The Gamma..? EliteGM is offline
    Grand MasterRank
    Jul 2006 Join Date
    NandolandLocation
    4,077Posts

    [PHP]A way of doing this shorter?

    This code pays people out per 1hour~24hour.
    I.e: you buy whores, then it sets the time you bought 'em.
    Then if you wait an hour, it'll pay you.
    But if you wait 10 hours, it'll pay you obviously 10 times as much.
    It's working.. but I dont think my code is optimal:

    PHP Code:
    $result mysql_query("SELECT * FROM `users` WHERE `username` = '$_SESSION[user]' LIMIT 1");
    $row mysql_fetch_assoc($result);
    $result2 mysql_query("SELECT * FROM `city` WHERE `name` = '$row[city]' LIMIT 1");
    $city mysql_fetch_assoc($result2);
    $time time();
    if (
    $row['whores'] > 0) { //Do they have any whores ?
    $pc round(($row['whores'] * $city['priceper']) / rand(713)); 
    $cash $row['whores'] * $city['priceper'] + rand(1$pc); //Price formula
    $for $row['thetime'] + 3600//1hr
    $for2 $row['thetime'] + 7200//2hr
    $for3 $row['thetime'] + 10800//etc...
    $for4 $row['thetime'] + 14400;
    $for5 $row['thetime'] + 18000;
    $for6 $row['thetime'] + 21600;
    $for7 $row['thetime'] + 25200;
    $for8 $row['thetime'] + 28800;
    $for9 $row['thetime'] + 32400;
    $for10 $row['thetime'] + 36000;
    $for11 $row['thetime'] + 39600;
    $for12 $row['thetime'] + 43200;
    $for13 $row['thetime'] + 46800;
    $for14 $row['thetime'] + 50400;
    $for15 $row['thetime'] + 54000;
    $for16 $row['thetime'] + 57600;
    $for17 $row['thetime'] + 61200;
    $for18 $row['thetime'] + 64800;
    $for19 $row['thetime'] + 68400;
    $for20 $row['thetime'] + 72000;
    $for21 $row['thetime'] + 75600;
    $for22 $row['thetime'] + 79200;
    $for23 $row['thetime'] + 82800;
    $for24 $row['thetime'] + 86400;

         if (
    $for24 <= $time) { $cash $cash*24$hrs 24;  } //24 times the cash if you wait 24 hours.. ofc.
    else if ($for23 <= $time) { $cash $cash*23$hrs 23; } //etc.
    else if ($for22 <= $time) { $cash $cash*22$hrs 22; }
    else if (
    $for21 <= $time) { $cash $cash*21$hrs 21; }
    else if (
    $for20 <= $time) { $cash $cash*20$hrs 20; }
    else if (
    $for19 <= $time) { $cash $cash*19$hrs 19; }
    else if (
    $for18 <= $time) { $cash $cash*18$hrs 18; }
    else if (
    $for17 <= $time) { $cash $cash*17$hrs 17; }
    else if (
    $for16 <= $time) { $cash $cash*16$hrs 16; }
    else if (
    $for15 <= $time) { $cash $cash*15$hrs 15; }
    else if (
    $for14 <= $time) { $cash $cash*14$hrs 14; }
    else if (
    $for13 <= $time) { $cash $cash*13$hrs 13; }
    else if (
    $for12 <= $time) { $cash $cash*12$hrs 12; }
    else if (
    $for11 <= $time) { $cash $cash*11$hrs 11; }
    else if (
    $for10 <= $time) { $cash $cash*10$hrs 10; }
    else if (
    $for9 <= $time)  { $cash $cash*9;  $hrs 9;  }
    else if (
    $for8 <= $time)  { $cash $cash*8;  $hrs 8;  }
    else if (
    $for7 <= $time)  { $cash $cash*7;  $hrs 7;  }
    else if (
    $for6 <= $time)  { $cash $cash*6;  $hrs 6;  }
    else if (
    $for5 <= $time)  { $cash $cash*5;  $hrs 5;  }
    else if (
    $for4 <= $time)  { $cash $cash*4;  $hrs 4;  }
    else if (
    $for3 <= $time)  { $cash $cash*3;  $hrs 3;  }
    else if (
    $for2 <= $time)  { $cash $cash*2;  $hrs 2;  }
    else if (
    $for <= $time)   { $cash $cash;    $hrs 1;  }

    //BIG line coming up!
    if ($for24 <= $time || $for23 <= $time || $for22 <= $time || $for21 <= $time || $for20 <= $time || $for19 <= $time || $for18 <= $time || $for17 <= $time || $for16 <= $time || $for15 <= $time || $for14 <= $time || $for13 <= $time || $for12 <= $time || $for11 <= $time || $for10 <= $time || $for9 <= $time || $for8 <= $time || $for7 <= $time || $for6 <= $time || $for5 <= $time || $for4 <= $time || $for3 <= $time || $for2 <= $time || $for <= $time) {
    $whores "Your whores brought you a total of: $"$cash ." in "$hrs ." hour(s)!<br />["date("d M, H:i:s") ."]"//message the player
    $cor mysql_query("UPDATE `users` SET `cash` = `cash` + '$cash', `msg` = '$whores', `thetime` = '$time' WHERE `username` = '$_SESSION[user]' LIMIT 1"); //give cash
    if (!$cor)
    {
        die(
    "Error["mysql_errno() ."]: "mysql_error());
    }
    }

    As you see that's soooo much code.
    I just wonder if there's a shorter way (which I've been trying but failing).


  2. #2
    Grand Master Daevius is offline
    Grand MasterRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [PHP]A way of doing this shorter?

    Lol, yes. What you need is a formula :)

    PHP Code:
    $hrs floor(($time $row['thetime']) / 3600);
    if (
    $hrs 24)
    {
        
    $hrs 24;
    }

    $cash *= $hrs

  3. #3
    The Gamma..? EliteGM is offline
    Grand MasterRank
    Jul 2006 Join Date
    NandolandLocation
    4,077Posts

    Re: [PHP]A way of doing this shorter?

    Lol ;p i gotta read up on PHP
    Such a 'simple' calculation.
    I can do pretty hard ones but yet..

    Thanks =)



Advertisement