Jump to content

Recommended Posts

I have an array of months:

 

$titleMonths = array (
							0=> 'January',
							1=> 'Febuary',
							2=> 'March',
							3=> 'April',
							4=> 'May',
							5=> 'June',
							6=> 'July',
							7=> 'August',
							8=> 'Septmeber',
							9=> 'October',
							10=> 'November',
							11=> 'Decem'

						 );

I also have a list of values stored in a database, the month value for these values is stored as a $items['month'] which is an integer e.g 5 (for May)

 

I'm trying to run a foreach loop that goes through the list of months, if the array position is the same as the month number, it'll echo the title for the month with all the values for that month underneath it.

 

So far experimenting I've managed to echo just the $titleMonth values, echo the $titleMonth values for as many results as the database returns, but so far no donut!

 

Here's what I've been working with ($dates is the rows returned from the database)

 

	$titleMonths = array (
					0=> 'January',
					1=> 'Febuary',
					2=> 'March',
					3=> 'April',
					4=> 'May',
					5=> 'June',
					6=> 'July',
					7=> 'August',
					8=> 'Septmeber',
					9=> 'October',
					10=> 'November',
					11=> 'Decem' );


	foreach($titleMonths as $month)
		{
		echo $this->_html .= $month;
			for($i = 0; $i < count($titleMonths); $i++)
				{

					foreach ($dates as $items)
					{

				         echo $items['day'].'/'.$items['month'].'/'.$items['year']

					}


				}

		}

 

Please help in anyway you can!

Link to comment
https://forums.phpfreaks.com/topic/200769-foreach-echo-titles-for-content/
Share on other sites

It's probably just me, but your objective here is unclear.  What are these "values underneath" that you are reffering to, where are they stored, what does your select statement look like? Where does the information form the database come into the script?

I am not sure what you mean,

but

you could just use date to format the result, but if you wanted to use the array why not just do this

echo $titleMonths[$items['month']-1]

or

 

$titleMonths = array (
1=> 'January',
2=> 'Febuary',
3=> 'March',
4=> 'April',
5=> 'May',
6=> 'June',
7=> 'July',
8=> 'August',
9=> 'Septmeber',
10=> 'October',
11=> 'November',
12=> 'Decem' );
echo $titleMonths[$items['month']]

Hi,

 

I just re-read this and I made it pretty unclear I want to achive, but this is it basically; Each time there is value in the $datas array, I want to match it against the $titleMonths array and to echo the values of data under the titleMonths value e.g

 

($items['month'] = 1)

 

January

 

07/01/2010

29/01/2010

 

($items['month'] = 4)

April

 

03/04/2010

13/04/2010

 

 

I see, I thought that you were storing all the information in a database, then pulling it out for your page, that I could have helped with.  I'll have to concede I don't know a whole lot about array manipulation with PHP yet so can't offer any help with that :-[

The first thing you need to do is to sort the dates array so the dates(months) are in order and grouped. array_multisort would work fine for that. Once the array is in ascending groups it should not be too hard to get them under the proper month.

 

 

HTH

Teamatomic

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.