Web Analytics Made Easy -
StatCounter Update mysql Data after query in a while loop? - CodingForum

Announcement

Collapse
No announcement yet.

Update mysql Data after query in a while loop?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment


    • #3
      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
      Useful function to retrieve difference in times
      The best PHP resource
      A good PHP FAQ
      PLEASE remember to wrap your code in [PHP] tags.
      PHP Code:
      // Replace this
      if(isset($_POST['submitButton']))
      // With this
      if(!empty($_POST))
      // Then check for values/forms. Some IE versions don't send the submit button 
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎