Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,555
    Thanks
    75
    Thanked 105 Times in 104 Posts

    Better way to do this?

    So without the IF and ELSE statement, this works perfectly fine and updates the players bank account. However I need to add a check to see if they receive interest or the payment.

    So when a player deposits an amount into Long Term, it adds a date of today and 30 days in advance (receivedate). So when the day comes, I need to do a payout to the players bank account rather than add more interest.

    However right now that isn't working properly. I am guessing to use FOREACH? But it's one of the pieces of code that I haven't learned yet.
    Let me know if this all doesn't make sense.

    PHP Code:
    $bquery mysqli_query($link"SELECT * FROM rod_bank WHERE longterm!='0' AND receivedate!='".date('Ymd')."'");

    while (
    $brow mysqli_fetch_array($bquery)) {
    $amount $brow['longterm'];
    $interest $amount .02;

    if (
    $brow['receivedate'] == date('Ymd')) {
      echo 
    'Yes Paid';
    //Add interest but don't give to player
    }
    else {
      echo 
    'Not Paid';
    //Query to give player the interest
    }


    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    erm, receivedate should really be a DATE field, so you can do calculations on it (like DATE_ADD(CURRENT_DATE, INTERVAL 30 DAY) => 30 days from today)

    So when the day comes, I need to do a payout to the players bank account rather than add more interest.
    does that affect your DB in any way?

    normally you could select any entries that match today (if you had a proper date field):
    SELECT * FROM rod_bank WHERE receivedate = CURRENT_DATE -- and some other required conditions
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,555
    Thanks
    75
    Thanked 105 Times in 104 Posts
    receivedate is DATE in the DB. So if I use the highlighted query, do I check it for true? Such as

    PHP Code:
    $query = ($link"SELECT * FROM rod_bank WHERE receivedate = CURRENT_DATE");
    if (
    $query == TRUE) {
    //Give interest, delete longterm details
    }
    else {
    //Add interest

    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by myfayt View Post
    So if I use the highlighted query, do I check it for true?
    you just check the number of matches: SELECT COUNT(*) FROM rod_bank WHERE receivedate = CURRENT_DATE
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,555
    Thanks
    75
    Thanked 105 Times in 104 Posts
    Sorry to bother you again, but I can't seem to understand how to use COUNT to check for each person. I tried a second query with count and as the main query.

    PHP Code:
    include ('CharNameDB.php');
    include (
    'header_common.php');

    $bquery mysqli_query($link"SELECT * FROM rod_bank WHERE longterm!='0' AND receivedate!='".date('Ymd')."'");

    while (
    $brow mysqli_fetch_array($bquery)) {
    $amount $brow['longterm'];
    $interest $amount .02;

    if (
    $brow['receivedate'] == date('Ymd')) {
      echo 
    'Deposit Interest, Reset Savings';
    }
    else {
      echo 
    'Add Interest';

    Thanks for any help you can provide
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    why do you need toch check for each person?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,555
    Thanks
    75
    Thanked 105 Times in 104 Posts
    This is going to be a 24 hour cronjob.

    When someone deposits gold into a long term savings account, they have to wait 30 days before they can get it back, while it's in there they earn interest on it. So I need to add a check on the while loop when it runs to see if their receive date is todays date and if so, place their gold back into their account with interest added.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    depending on your db structure you could do that by SQL alone.

    So I need to add a check on the while loop when it runs to see if their receive date is todays date
    not necessary when you have that condition in the SQL.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,555
    Thanks
    75
    Thanked 105 Times in 104 Posts
    So I have something working, but don't think it's the right way to go about it. I kind of understand what you mean about doing it in SQL alone, which I assume you are meaning using the UPDATE query where those values match? But here is what I got and the output and DB structure.

    PHP Code:
    <?php

    include ('CharNameDB.php');
    include (
    'header_common.php');

    $bquery mysqli_query($link"SELECT * FROM rod_bank WHERE longterm!='0' AND receivedate!='".date('Ymd')."'");
    $interestquery mysqli_query($link"SELECT * FROM rod_bank WHERE longterm!='0' AND receivedate='".date('Ymd')."'");

    while (
    $brow mysqli_fetch_array($bquery)) {
    $amount $brow['longterm'];
    $interest $amount .02;

    if (
    $brow['receivedate'] == date('Ymd')) {
      echo 
    'Deposit Interest, Reset Savings';
    }
    else {
      echo 
    'Add Interest';
    }

    //mysqli_query($link, "UPDATE rod_bank SET interest=interest+$interest WHERE charname='".$brow['charname']."'");

    }

    while (
    $irow mysqli_fetch_array($interestquery)) {

        echo 
    'Match<br />';

       }
    ?>
    Output:

    Add InterestMatch
    Better way to do this?-bank_db.jpg
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    first, you have duplicate queries.

    second, `longterm` is an INT not a CHAR (integers shouldn’t be quoted).

    third, you don’t need PHP to pass in today’s date, there are SQL functions/constants for that (CURRENT_DATE, CURRENT_DATE(), NOW())

    fourth, the MySQL DATE format is YYYY-MM-DD, even though it accepts also YYYYMMDD as input it will always return YYYY-MM-DD as output.

    fifth, if you select each entry where the receivedate is today, why making the same check again in PHP?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #11
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,555
    Thanks
    75
    Thanked 105 Times in 104 Posts
    So by fixing things as you said, I came up with this:

    PHP Code:
    $bquery mysqli_query($link"SELECT * FROM rod_bank WHERE longterm!=0 AND receivedate=CURRENT_DATE");


    while (
    $brow mysqli_fetch_array($bquery)) {
    $amount $brow['longterm'];
    $interest $amount .02;

    if (
    $brow['receivedate'] == date('Y-m-d')) {
      echo 
    'Deposit Interest, Reset Savings';
    }
    else {
      echo 
    'Add Interest';
    }

    //mysqli_query($link, "UPDATE rod_bank SET interest=interest+$interest WHERE charname='".$brow['charname']."'");


    and result is:

    Deposit Interest, Reset Savings
    So the IF/ELSE statement is only processing one of the two accounts. One should be Deposit and the other should be Add.
    I need the check in there so I can update every single account if it needs to add interest or deposit the money.
    It's not for a single, it's for all accounts that meet that criteria
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #12
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    220
    Thanks
    3
    Thanked 11 Times in 10 Posts
    You are selecting items in the table where receivedate is the current date. Then, in a while loop, you are checking if receivedate is the current date. Since you only asked the database to give you rows where that was true, it would be a major problem if you got anything else. You should *NEVER* see the "Add Interest" message.

    (This was dormilich's 5th point)

  • #13
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,555
    Thanks
    75
    Thanked 105 Times in 104 Posts
    So you are saying this is the way to go?

    PHP Code:
    $bquery mysqli_query($link"SELECT * FROM rod_bank WHERE longterm!=0 AND receivedate=CURRENT_DATE");


    while (
    $brow mysqli_fetch_array($bquery)) {
    $amount $brow['longterm'];
    $interest $amount .02;


      echo 
    'Deposit Interest, Reset Savings';

      if (
    $brow['receivedate'] != date(Y-m-d); {

    mysqli_query($link"UPDATE rod_bank SET interest=interest+$interest WHERE charname='".$brow['charname']."'");

     }

    Been a sign maker for 7 years. My business:
    American Made Signs

  • #14
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    220
    Thanks
    3
    Thanked 11 Times in 10 Posts
    No. What I'm saying is that your query:
    Code:
    SELECT * FROM rod_bank WHERE longterm!=0 AND receivedate=CURRENT_DATE
    will *NEVER* give you a row where receive date is not the current date. Comparing receivedate to the current date (either using == or !=) is redundant because it will *ALWAYS* be equal, because that is what you asked for! Therefore, you if statement there will *ALWAYS* be false (and never execute what's inside) and in the previous example, it will *ALWAYS* be true (therefore, never executing the else)

  • #15
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,555
    Thanks
    75
    Thanked 105 Times in 104 Posts
    So basically it seems like this is impossible to do. I am at a loss here. If it's not possible to check if today's date is the same as the receive date, what is my other option? Thanks

    Edit: I am not angry just frustrated
    Been a sign maker for 7 years. My business:
    American Made Signs


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •