Forum Moderators: coopster

Message Too Old, No Replies

Start month in database like 1, 9, 10 - how do I get it out

         

JuiceUK

9:12 am on Jun 20, 2006 (gmt 0)

10+ Year Member



Hi all,

I have a database in mysql. I have a column called start_month. In this column I want to put the months that a particular thing starts in but like this:

1, 5, 9 - meaning Jan, May, Sept etc

I need to call back these months on my page. So the page might say 'The months this stats in are January, Febuary, March.'

Does anyone know the php to grab the data? I know how ot do the connecting to database bit I just need the specific code please - is it a array trick?

Thanks for your help -

M

wheelie34

9:28 am on Jun 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is it a array trick?

I am not a pro at php but I would use an array to line up the month number with the correct month name, starting with your highest first IE

case ($result = 12):
echo "December";
break;
case ($result = 11):
echo "November";
break;
default:
echo "no match found";

See how it goes

scriptmasterdel

9:44 am on Jun 20, 2006 (gmt 0)

10+ Year Member



Pretty easy solution, using the function explode()

We are going to split all the months into an array and then put them in to a loop to display them as a list


$sel = "select * from mytable";

$res = mysql_query($sel);

while($row = mysql_fetch_array($res))
{
$months = explode(',',$row['start_month']);
foreach($months as $month)
{
echo $month.'<br>';
}
}

This should work as you want it, you may need to tweek it to your desire, note this has not been tested.

I hope this helps!

Del

JuiceUK

9:49 am on Jun 20, 2006 (gmt 0)

10+ Year Member



Thanks for the replay. It doesn't make much sense to me though - eeek :)

In the column I might have 1 or I might have 1, 10 or 12, 1, 3

I need to somehow look at this data and say if it's 1 just say january but if it's 1, 3 say January,
March.

Hope this makes things clearer.

M

JuiceUK

9:50 am on Jun 20, 2006 (gmt 0)

10+ Year Member



Thanks Del I'll try it out asap -

M

JuiceUK

9:51 am on Jun 20, 2006 (gmt 0)

10+ Year Member



Del,

How would I then get it to say 1 - Jan 9 = sept etc?

M

scriptmasterdel

10:01 am on Jun 20, 2006 (gmt 0)

10+ Year Member



Create a new array of months:

$months_list = array('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

Remember arrays start at 0 so we blank that out then to call the month name

In your foreach loop call the month array name using the number stored in the db table.

So ...

echo $month.'<br>';

Becomes ...

echo $months_list[$month].'<br>';

Good Luck!