Forum Moderators: coopster
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
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
$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!