SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with IF statement

    I've been trying to figure this out for hours and am pulling my hair out. I use an IF statement just like this one that does something similar in another script and I can't figure out why this one isn't working. Here's my code:

    PHP Code:
    // example of what $row['date'] equals for December = 2004-12-01

        
    $currentMonth '';

        if (
    $currentMonth != $row['date']) {
            
    $currentMonth $row['date'];
        
            while (
    $row2 mysql_fetch_assoc($query)) {
                
    $number $row2['num'] - 1;
                echo 
    "<td rowspan=\"$number\">$month</td>\n";
            }
        } 
    I want it to do the echo only if it's the first time the month has been printed. So if there's 6 entries for a month, instead of printing the month in the TD for every entry, it will just print it the first time.

    Any help would be greatly appreciated.
    Last edited by JohnSaunders; Oct 27, 2004 at 12:41.
    John Saunders

  2. #2
    SitePoint Member
    Join Date
    Oct 2004
    Location
    Grand Rapids, MI
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        echo "<td rowspan="$number\">$month</td>\n"
    you should escape the " (quotes) around $number

  3. #3
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops, I had it right in my script, just not in my post here.
    John Saunders

  4. #4
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    devised,

    I'm not sure where you post disappeared to but I saw it in my email. Yes, there different months contained in the database, one is assigned to each entry. I just want to print the month one time and assign that <td> a row spam, instead of printing the month's name next to each entry.

    Here's what I want it to do:

    Code:
    <tr>
      <td rowspan="5">December</td>
      <td>Name</td>
    </tr>
    <tr>
      <td>Name</td>
    </tr>
    <tr>
      <td>Name</td>
    </tr>
    <tr>
      <td>Name</td>
    </tr>
    <tr>
      <td>Name</td>
    </tr>
    <tr>
      <td>Name</td>
    </tr>
    Here's what it's doing now:

    Code:
    <tr>
      <td rowspan="5">December</td>
      <td>Name</td>
    </tr>
    <tr>
      <td rowspan="5">December</td>
      <td>Name</td>
    </tr>
    <tr>
      <td rowspan="5">December</td>
      <td>Name</td>
    </tr>
    <tr>
      <td rowspan="5">December</td>
      <td>Name</td>
    </tr>
    <tr>
      <td rowspan="5">December</td>
      <td>Name</td>
    </tr>
    <tr>
      <td rowspan="5">December</td>
      <td>Name</td>
    </tr>
    John Saunders

  5. #5
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is $currentMonth = ''; nested in a loop and is causing the if statement to run each time?
    PHP Code:
        $currentMonth ''

        if (
    $currentMonth != $row['date']) { 

  6. #6
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's correct. It's in a do {SOMETHING } while ($row = mysql_fetch_assoc($query)); loop.
    John Saunders

  7. #7
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JohnSaunders
    Yes, that's correct. It's in a do {SOMETHING } while ($row = mysql_fetch_assoc($query)); loop.
    Is the loop that it's nested in ran for each table row in the html?

  8. #8
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, here's the entire script in case you'd like to see what all's going on:

    PHP Code:
    <?php
    require_once('includes/connections/connect.php');
    mysql_select_db($database_Connection$Connection);
    ?>
          <table cellspacing="1">
            <tr>
              <th>Month</th>
              <th>Name</th>
            </tr>
    <?php
    $sql 
    "SELECT date, name FROM calendar WHERE EXTRACT(year from date)= 2004 ORDER BY EXTRACT(month from date) ASC";
    $query mysql_query($sql) or die(mysql_error() . " when executing " $sql);
    $row mysql_fetch_assoc($query);

    while (
    $row mysql_fetch_assoc($query)) {
        
    $month strtotime($row['date']);
        
    $monthNew date('F'$month);
        
    $monthDB date('m'$month);

        echo 
    "        <tr>\n";

        
    $sql2 "SELECT COUNT(*) AS num FROM calendar where EXTRACT(month from date) = " $monthDB "";
        
    $query2 mysql_query($sql2) or die(mysql_error() . " when executing " $sql2);

        
    $currentMonth '';

        if (!
    $currentMonth == $row['date']) {
            
    $currentMonth $row['date'];

            while (
    $row2 mysql_fetch_assoc($query2)) {
                
    $number $row2['num'] - 1;
                echo 
    "          <td rowspan=\"$number\">$monthNew</td>\n";
            }
            
    mysql_free_result($row2);
        }
        echo 
    "          <td>" $row['name']. "</td>\n";
            </
    tr>
    <?
    php
    }
    mysql_free_result($row);
    ?>
         </table>
    John Saunders

  9. #9
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     $currentMonth '';

        if (
    $currentMonth != $row['date']) { 
    So wouldn't this statement always be true and echo the line out since it's being set to nothing right before you test it?

    Please forgive me if I'm misunderstanding it.

  10. #10
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're right! I moved "$currentMonth = ''; " outside the loop and it worked for the first month, but the other's didn't get printed. Do you have any idea why?
    John Saunders

  11. #11
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the updated code?

  12. #12
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, here it is:

    PHP Code:
    <?php
    require_once('includes/connections/connect.php');
    mysql_select_db($database_Connection$Connection);
    ?>
          <table cellspacing="1">
            <tr>
              <th>Month</th>
              <th>Name</th>
            </tr>
    <?php
    $sql 
    "SELECT date, name FROM calendar WHERE EXTRACT(year from date)= 2004 ORDER BY EXTRACT(month from date) ASC";
    $query mysql_query($sql) or die(mysql_error() . " when executing " $sql);
    $row mysql_fetch_assoc($query);

    $currentMonth '';

    while (
    $row mysql_fetch_assoc($query)) {
        
    $month strtotime($row['date']);
        
    $monthNew date('F'$month);
        
    $monthDB date('m'$month);

        echo 
    "        <tr>\n";

        
    $sql2 "SELECT COUNT(*) AS num FROM calendar where EXTRACT(month from date) = " $monthDB "";
        
    $query2 mysql_query($sql2) or die(mysql_error() . " when executing " $sql2);

        if (!
    $currentMonth == $row['date']) {
            
    $currentMonth $row['date'];

            while (
    $row2 mysql_fetch_assoc($query2)) {
                
    $number $row2['num'] - 1;
                echo 
    "          <td rowspan=\"$number\">$monthNew</td>\n";
            }
            
    mysql_free_result($row2);
        }
        echo 
    "          <td>" $row['name']. "</td>\n";
    ?>
            </tr>
    <?php
    }
    mysql_free_result($row);
    ?>
         </table>
    Last edited by JohnSaunders; Oct 27, 2004 at 17:34.
    John Saunders

  13. #13
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about the closing </tr> below? It needs to either be echo'ed or moved to the html.

    PHP Code:
        echo "          <td>" . $row['name']. "</td>\n";
            </tr>
    <?php
    }
    mysql_free_result($row);
    ?>
         </table>

  14. #14
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops! I left out the closing ?> tag before it. I had it right in my script, but not in my post.
    John Saunders

  15. #15
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anybody? I've spent a LOT of time trying to figure this out and cannot figure out why it will work for the first month, but won't for the rest of them?

    It seems like when the script runs for the first time the IF statement for $currentMonth runs because $row['date'] is equal to 2004-10-01, then when it hit's November, it doesn't run because $currentMonth is still equal to 2004-10-01. Can someone tell me how to reset $currentMonth after all the entries are finished for this month so it will change back to NULL?
    John Saunders

  16. #16
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried changing this:
    PHP Code:
    if (!$currentMonth == $row['date']) { 
    to this:
    PHP Code:
    if ($currentMonth != $row['date']) { 

  17. #17
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Um, why not go:

    if (empty($row['date']))

    Instead of comparing it to blank? Or even:

    if ($row['data'] == '')
    {
    }

  18. #18
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    devised,

    That did the trick. Can you tell me what the difference is betwen the two? I thought they meant the same thing?

    someonewhois,

    Thanks for the alternate suggestion!
    John Saunders

  19. #19
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    Nevada
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devised
    Have you tried changing this:
    PHP Code:
    if (!$currentMonth == $row['date']) { 
    to this:
    PHP Code:
    if ($currentMonth != $row['date']) { 
    !($currentMonth == $row['date']) == ($currentMonth != $row['date'])
    But (!$currentMonth == $row['date']) != ($currentMonth != $row['date'])
    !$currentMonth in (!$currentMonth == $row['date']) gets evaluated first which is always going to be equal to false and "false != $row['date']"

    PHP Code:
    <?php

    $currentMonth 
    "asdf";
    $row['date'] = "asdf";

    echo ( !(
    $currentMonth == $row['date']) == ($currentMonth != $row['date']) ) ? 'true':'false';
    echo 
    "<br/>";

    ?>
    Renaun
    PHP Reusable Web Framework - http://www.rwfphp.org


Bookmarks

Posting Permissions

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