Jump to content

Beginer Help. creating a loop for multiple html tables for each month


Recommended Posts

Hi,

 

hopefully somebody can help me, prob pretty simple but its hurting my head. basically i have a SQL database with 5 colums and one being the date. my query calls the date as month.

 

i want to create a loop so it will print the month then create an html table with all the data from that month, end the table then go and do the same thing but with the next month.

Off the top of my head, it sounds like you will have to create an array or arrays to get that to happen.

I am not sure about the syntax, but I did something similar once when I needed to create a list of cities in a given country.

 

I created the content array first - example: $us = array ('1'=>'Los Angeles', '2'=>'New York'...)

$brasil=array('1'=>'Sao Paulo', '2'=>'Rio de Janeiro'...)

 

and finally $country = array($us, $brasil)

 

Then you will have to create a foreach loop inside another foreach loop in order to display things as you described...

 

foreach (country array){

 

title of the table goes here

    foreach (city array){

 

    table syntax goes here

}

 

}

 

This should get you started. Hope this helps

You need to create something like this pseudo:

 

query for dates...

while dates...

start table...

query for other info WHERE date = $query['date_from_other_table']...

rows, cols, info inside them...

close while...

close table...

close while...

 

 

is there any way i can call the previous part of the array?

 

so basically i have

 


while ($row = mysql_fetch_array($result)) {

      if($row['month'] == previous_month_in_array ) {

	...[prints data]...

	} else {

       ...[new table]...

               }
        } //

 

or maybe i should just have one big table as im confusing myself  ???

Are you basically trying to display all the months, then under each month have the data for it? Like:

 

January

---------

blah blah blah

blah blah blah

 

February

---------

blah blah blah

blah blah blah

 

March

---------

blah blah blah

blah blah blah

 

along them lines yes. but there is quite a few with the same dates.

 

i did try a for loop so it runs throught all the dates using mysql select where month = $i

 

but obviously it wouldnt take into account the year :(

 

what i want is

 

<h1>Jan</h1>

<table>

<th>header</th>

<tr><td>data</td></tr>

<tr><td>data</td></tr>

<tr><td>data</td></tr>

</table>

 

<h1>Feb</h1>

<table>

<th>header</th>

<tr><td>data</td></tr>

<tr><td>data</td></tr>

<tr><td>data</td></tr>

</table>

i want a loop that lists all the data for say january in one table then jumps to next month and does same thing

 

That's the point of the while loop.  So however many months you have in the database is how many tables are going to be generated.

i got it working, but not the proper way..

 


<?php

$prev = false;
		while($row = mysql_fetch_array($result)) //while loop to fetch array
			{

				if($prev != $row['month']) {

					echo "</table><h2>";
					month($row['month']);//<-- function for format as JANUARY FEBRUARY ETC....
					echo "</h2>\n";
					echo "<table>\n<th>Date</th><th>Account</th><th>Deposit</th><th>Withdrawn</th>";
					$prev = $row['month'];
				}

				echo "\n<tr>";	
				echo "<td>".$row['date']."</td>";
				echo "<td>".$row['account']."</td>";
				echo "<td>£".$row['deposit']."</td>";
				echo "<td>£".$row['withdraw']."</td>";
				echo "<td class =\"hidden\">Delete</td>";
				echo "</tr>\n";

				} 
?>

 

as you can see i have to end the table within the if statement, so i have an extra end table in code :( not sure how to break out the bottom code to end table

 

appriciate any suggestions

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.