...

View Full Version : Update mysql Data after query in a while loop?



Atrhick
08-30-2011, 04:00 PM
I'm trying to get this round my head, but i am failing quite hard :o

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.



//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']}' />";
}
}

Fou-Lu
08-30-2011, 04:37 PM
Okay and the problem is?


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.

BluePanther
08-30-2011, 04:45 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum