lorkan Posted September 23, 2008 Share Posted September 23, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/125539-looping-issues-for-calendar-help-me-experts/ Share on other sites More sharing options...
Barand Posted September 23, 2008 Share Posted September 23, 2008 And your problem is what? Link to comment https://forums.phpfreaks.com/topic/125539-looping-issues-for-calendar-help-me-experts/#findComment-649106 Share on other sites More sharing options...
lorkan Posted September 24, 2008 Author Share Posted September 24, 2008 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? Link to comment https://forums.phpfreaks.com/topic/125539-looping-issues-for-calendar-help-me-experts/#findComment-649261 Share on other sites More sharing options...
Barand Posted September 24, 2008 Share Posted September 24, 2008 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)) { Link to comment https://forums.phpfreaks.com/topic/125539-looping-issues-for-calendar-help-me-experts/#findComment-649299 Share on other sites More sharing options...
Barand Posted September 24, 2008 Share Posted September 24, 2008 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>"; Link to comment https://forums.phpfreaks.com/topic/125539-looping-issues-for-calendar-help-me-experts/#findComment-649307 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.