#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7

    If/then written right?


    Howdy y'all. I have the simple if/then in my while loop below. It is to meerly place an 'X' if an item has been paid. If the item HAS been paid, the value retrieved from the db is 1, otherwise 0. When the while loop runs, I get no errors, but every item is being marked with an 'X', even though a 0 (zero) has been return from the db. Am I overlooking something?

    PHP Code:
    $invoices mysqli_query($con,"SELECT * FROM `invoices` WHERE MONTH(due) = " $month " ORDER BY `due` ASC;");
    $category mysqli_query($con,"SELECT * FROM `category`;");
    while(
    $row mysqli_fetch_assoc($category)) {
          
    $cat[] = $row;

    PHP Code:
                    while ($list mysqli_fetch_assoc($invoices)) {
            if(
    $list['paid'] = "1") { $paid "X"; } else { $paid ""; }
            echo 
    "      <tr>
            <td align=\"center\">" 
    $paid "</td>
            <td align=\"center\">" 
    date('Y-d-m',strtotime($list['due'])) . "</td>
            <td align=\"right\">$" 
    $list['amount'] . "</td>
            <td align=\"left\">" 
    $cat[$list['category']-1]['name'] . "</td>
            <td align=\"left\">" 
    $list['comment'] . "</td>
          </tr>\n"
    ;

    Last edited by Triple_Nothing; December 7th, 2012 at 09:55 AM.
  2. #2
  3. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    change your code to this
    PHP Code:
     if($list['paid'] == "1") { $paid "X"; } else { $paid ""; } 
    notice the double ==

    You used a single one, which pretty much assigns the value of 1, making all loops true.

    Btw if this value is sored as an integer (which I would recommend) You don't need quotes around the 1
    Last edited by aeternus; December 7th, 2012 at 10:02 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Change:

    PHP Code:
    if($list['paid'] = "1"
    to

    PHP Code:
    if($list['paid'] == "1"
    edit: beaten bij Aeturnus
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Yep. It's still morning... The quotes weren't intended, just a troubleshootin thought. Thanks for pointin out the double equal signs.

    Comments on this post

    • aeternus agrees : Wield the power of coffee

IMN logo majestic logo threadwatch logo seochat tools logo