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.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Update mysql Data after query in a while loop?

    I'm trying to get this round my head, but i am failing quite hard

    I have rows in a database which i am echoing out in a while() loop, however i want to use a switch statement to change the output to something more readable please see code example below.

    PHP Code:
    //Fetch all needed records
    $strQuery ="SELECT * FROM lead_partners_pages ";
    $strQuery .="WHERE afid_seller = '202'"//$password
    $result mysql_query($strQuery) or die(mysql_error());
                       
                       
    $query2 "SELECT SUM(price_CIQFY_paid) AS totalPrice ";
    $query2 .="FROM lead_partners_pages  ";
    $query2 .="WHERE afid_seller = '202'"//$password
    $result_set2 mysql_query($query2);


    if (
    $result && $result_set2) {
    while(
    $ors mysql_fetch_array($result) AND $ors2 mysql_fetch_array($result_set2)) {
    switch (
    $ors['date_month'])
    {
    case 
    01:
    echo 
    "Jan";
    break;
    case 
    02:
    echo 
    "Feb";
    break;
    case 
    03:
    echo 
    "Mar";
    break;
    case 
    04:
    echo 
    "Apr";
    break;
    case 
    05:
    echo 
    "May";
    break;
    case 
    06:
    echo 
    "Jun";
    break;
    case 
    07:
    echo 
    "Jul";
    break;
    case 
    08:
    echo 
    "Aug";
    break;
    case 
    09:
    echo 
    "Sep";
    break;
    case 
    10:
    echo 
    "Oct";
    break;
    case 
    11:
    echo 
    "Nov";
    break;
    case 
    12:
    echo 
    "Dec";
    break;
    default:
    echo 
    "No number between 01 and 12";
    }

    $strXML .= "<set name='{$ors['date_month']}' value='{$ors2['totalPrice']}' />";
    }


  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Okay and the problem is?

    Edit:
    Wait, I see it. You cannot use 01... as your numbers. These are octal numbers and will not likely compare properly to the switch beyond the 8th month. You will need to convert these to strings to use them, or use an integer datatype in your database.

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    The problem is, you're checking for integers. Anything coming from a database will be a string until you change it otherwise (I think). So, wrap your case check's with ' to compare your switch for a string, and it should work .

    A better way of implementing dates is to use a timestamp from time() stored in the database. That way, you can use the date() function to pull any information you want from it - including time. It's also makes comparison much easier. You should look into that . With a timestamp, you wouldn't need a switch statement - all you would do is date( "M", $timestamp) and that would give you the short month. http://php.net/manual/en/function.date.php - more info there


  •  

    Posting Permissions

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