Results 1 to 6 of 6
  1. #1

    output results from MySQL

    Hi
    I have this piece of php code to display results in a table
    I use var $actual_month = date("%-m-%) in "SELECT * FROM mytable WHERE DATA like '$actual_month' ORDER BY"
    Thats out every results corresponding actual month, but I need if a result of DATA corresponds to actual day, this line of results on table, appears in different row color or highlighted.

    This results on this example (assuming that the day of access is 11-05-2004):
    | Name | Local | Phone | Birthday |
    ----------------------------------------------------------
    | John | Almada |966083405 | 11-05-1976 |
    | Mary | York | 22869974 | 25-05-1964 |

    php code
    ------------------------------------------------------------------------------

    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    print "\t<tr>\n";
    foreach ($line as $col_value) {
    print "\t\t<td bgcolor=\"9BEBFF\">$col_value</td>\n";

    ----------------------------------------------------------------------------
    I appreciate any help
    Thks

  2. #2
    Join Date
    Jan 2004
    Location
    Quebec
    Posts
    164
    Hi,

    1st - You may want to simplify your query ...

    something along the line of

    select * from mytable WHERE MONTH(CURDATE()) = MONTH(mytable.datefield)

    because LIKE are damn slow..

    2nd

    A simple IF should do the tricks

    $date_time_array = getdate();
    $hours = $date_time_array["hours"];
    $month = $date_time_array["mon"];
    $day = $date_time_array["mday"];
    $year = $date_time_array["year"];

    $todaysdate = $year . "-" . $month . "-" . $day;

    That will give you todays date.

    And in your loop something like
    if ($line["birthdate"] == $todaysdate) {
    print "line in red";
    } else {
    print "Hey his birthday is this month";
    }

    That being said, there's another condition that you may look into (assuming its a birthday reminder script)

    select * from mytable WHERE MONTH(CURDATE()) = MONTH(mytable.datefield) AND DAYOFMONTH(CURDATE()) <= DAYOFMONTH(mytable.datefield)

    Hope that helps you.

    Cheers,

    Matt

  3. #3

    Thumbs up

    Hi
    First thanks for your reply

    About the 1st suggestion, it is very better, thks

    in the 2nd
    I make a very similar

    foreach ($line as $col_value) {
    if ($actua_data == $line['data']) {
    print "\t\t<td bgcolor=\"$bg1\">&nbsp;$col_value</td>\n";
    }
    elseif ($actual_data != $line['data']) {
    print "\t\t<td bgcolor=\"$bg2\">&nbsp;$col_value</td>\n";
    }

    The only problem thats the year, for this condition i don't need the current year.
    i.e. assuming i view this result, today 2004-05-11, $todaysdate is = 2004-05-11, but i need something like (<=year)-05-11
    so that $todaysdate assumes any year below 2004 (or current year).
    in the example above if i assign to $actual_data = "1997-05-11"
    returns 1st option (because the table have this field) and the others fields with the 2nd condition. (that this result I want)
    I hope understand me
    now my problem it's the year
    Thank you

  4. #4
    Join Date
    Jan 2004
    Location
    Quebec
    Posts
    164
    Maybe you could do something along those line.

    select *, DAYOFMONTH(CURDATE()) AS Today, DAYOFMONTH(mytable.datefield) AS BirthDay from mytable WHERE MONTH(CURDATE()) = MONTH(mytable.datefield)

    This way you're keeping every date calculation on the MySQL side.
    if ($line["Today"] == $line["Birthday"])

    Cheers,

    Matt

  5. #5
    Hi
    Thanks for yours suggestions
    For now the best I can do is make 2 QUERY
    in the 1st query i select fields for the month and tell if any fields correspond to today then don't show
    In the 2nd query i select fields for the month and day

    i.e.
    -----------------
    $query = "SELECT *FROM mytable WHERE MONTH(CURDATE()) = MONTH(mytable.datefield) AND DAYOFMONTH(CURDATE()) <> DAYOFMONTH(mytable.datefield) ORDER BY DAYOFMONTH(mytable.datefield) LIMIT 0, 50";

    $query2 = "SELECT * FROM mytable WHERE MONTH(CURDATE()) = MONTH(mytable.datefield) AND DAYOFMONTH(CURDATE()) = DAYOFMONTH(mytable.datefield) LIMIT 0, 50";

    then

    /* Displat datefields match today */
    while ($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
    foreach ($line2 as $col_value2) {
    echo "\t\t<td bgcolor=\"$bg2\" bordercolor=\"$bg4\">&nbsp;<b>$col_value2</b></td>\n";
    }
    echo "\t</tr>\n";
    }
    /* Displat datefields match month and deducts datefields for today */
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
    echo "\t\t<td bgcolor=\"$bg3\">&nbsp;$col_value</td>\n";
    }
    echo "\t</tr>\n";
    }

    Now it seems simple!

    Regards,
    Fernando

  6. #6
    Join Date
    Jan 2004
    Location
    Quebec
    Posts
    164
    Happy you succeed

    Cheers,

    Matt

Posting Permissions

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