Jump to content

looping issues for calendar! help me experts!


lorkan

Recommended Posts

Hi!

 

I'm trying my best to make a sort of calendar that only shows colors.

It takes the colors from a DB. The output should be a table with 1 row for each objektnr (object number) in the db. That row should contain a field for address, a field for the objektnr and a field for every day in that month and finally a field for comments.

 

Please see the code and understand more! I need your help!

<?php

//för månad!
$c=$_GET["c"];
//--------------


$con = mysql_connect("localhost","db_name","the_password"); 
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  } 
  
  
  mysql_select_db("table_name", $con);



	$sql="SELECT * FROM `schema` ORDER BY `objektnr`";

$month_length = "28";
$sql="SELECT * FROM `schema` ORDER BY `objektnr`";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "<table bgcolor='#FFFFFF' border='1'>
<tr style='font-family:Tahoma; font-weight:bold; font-size:12px; color:#000000; padding-top:0px; padding-left:0px; padding-right:0px;'>
<th>Adress</th>
<th>Objnr</th>
<th>01</th>
<th>02</th>
<th>03</th>
<th>04</th>
<th>05</th>
<th>06</th>
<th>07</th>
<th>08</th>
<th>09</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
<th>14</th>
<th>15</th>
<th>16</th>
<th>17</th>
<th>18</th>
<th>19</th>
<th>20</th>
<th>21</th>
<th>22</th>
<th>23</th>
<th>24</th>
<th>25</th>
<th>26</th>
<th>27</th>
<th>28</th>";

if ($c!=="2")
{
$month_length = "30";
echo "<th>29</th>
<th>30</th>";
}

if ($c=="1" || $c=="3" || $c=="5" || $c=="7" || $c=="8" || $c=="10" || $c=="12")
{
echo "<th>31</th>";
$month_length = "31";
}
echo "<th>".utf8_encode('Anmärkningar')."</th>
</tr>";


$sqlj="SELECT `objektnr`, `manad`, `dag`, `status`, `mer`, `adress` 
FROM `schema` LEFT JOIN `lgh` ON `objektnr`=`objectnr` WHERE `manad`='".$c."' ORDER BY `objektnr`, `dag`";
$resultt = mysql_query($sqlj);
$row = mysql_fetch_array($resultt);
$count = mysql_num_rows($resultt);
$nbr=($count)-1;

$current_objekt="000";
foreach (array($row) as $data_line)
{
if ($current_objekt!=$data_line['objektnr'])
{
	$current_objekt = $data_line['objektnr'];
	echo "<tr>";
	echo "<td class='ha'><b>".$data_line['adress']."</b></td>";
	echo "<td class='ha'><b>".$data_line['objektnr']."</b></td>";
}

for ($d=1; $d<=$month_length; $d++)
{
	$color_status = "white";
	if ($current_objekt == $data_line['objektnr'])
	{
		if ($data_line['dag']==$d)
		{
			switch ($data_line['status'])
			{
				case 0: 
				{
				$color_status = "white";
				break;
				}
				case 1:
				{
				$color_status = "red";
				break;
				}
				case 2: 
				{
				$color_status = "blue";
				break;
				}
				case 3: 
				{
				$color_status = "orange";
				break;
				}

			}
			//if($data_line<$nbr)
			//{
				next($data_line);
				//ska det vara $current_objekt?
			//}
		}
	}
	echo "<td bgcolor='".$color_status."'> </td>";
}	
echo "<td>".$data_line['mer']." </td>";
echo "</tr>";
$current_objekt = $data_line['objektnr'];

}

echo "</table>";

mysql_close($con);
?>

the problem is that the script only gives me one line as output.

or lets say only gives my one

<tr>blabla</tr>

you can also say that it only shows me one objektnr, because it should be onte

<tr>

for every objektnr.

 

so somehow it only loops vertically once...

 

do you understand?

You only fetch the first row.. There is a single call

 

$row = mysql_fetch_array($resultt);

 

which gets the fields of the first row and puts them into an array.

 

It does NOT put all the results into an array.

 

http://www.php.net/mysql_fetch_row

 

 

 

Remove that line and change the foreach line to

 

while ($data_line = mysql_fetch_array($resultt)) {

 

PS

 

you can replace all the hard coding of the headers with

 


echo "<table bgcolor='#FFFFFF' border='1'>
<tr style='font-family:Tahoma; font-weight:bold; font-size:12px; color:#000000; padding-top:0px; padding-left:0px; padding-right:0px;'>
<th>Adress</th>";

$c = $_GET['c'];
$month_length = date ('t', mktime(0,0,0,$c,1,date('Y')));       //get days in month
for ($d=1; $d <= $month_length; $d++)
{
    printf ('<th>%02d</th>', $d);
}

echo "<th>".utf8_encode('Anmärkningar')."</th>
</tr>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.