stublackett Posted July 18, 2008 Share Posted July 18, 2008 Hi, I've got a Calendar setup in the form of a HTML Table The problem I have is, That for each td cell I am having to put PHP Code in each cell to pull the relevant data for that Day / Month / Year Is there anyway to shorten the code so that I dont have to do it for each TD Cell Here is my code <td>1 <?php $day = 1; $result = mysql_query("SELECT * FROM $db_table WHERE day = $day"); while($day = mysql_fetch_assoc($result)) {//begin of loop $title = $day['title']; $id = $day['id']; //now print the results: // Now print the options to view item echo $title; }//End While loop ?> </td> This is obviously repeated for day 2,3,4,5 etc is there anyway at all to prevent me constantly putting this into each <td> Cell!? Quote Link to comment Share on other sites More sharing options...
JD* Posted July 18, 2008 Share Posted July 18, 2008 Do it the other way around. Get the number of days, then make a loop that will create the TD and put the relevant information inside of it. Quote Link to comment Share on other sites More sharing options...
stublackett Posted July 18, 2008 Author Share Posted July 18, 2008 Whats the best approach to get the actual date there JD* ? I was just going to do it as 1 = January, 2 = February etc, But if theres an easier way I'll try that I assume I'm doing something like this for ( $day =1; $day<31; ) echo "<td>$day"; Quote Link to comment Share on other sites More sharing options...
unkwntech Posted July 18, 2008 Share Posted July 18, 2008 Don't forget to check how many days are in the current month instead of hard codding 31. Quote Link to comment Share on other sites More sharing options...
stublackett Posted July 21, 2008 Author Share Posted July 21, 2008 Sorry to bump this up I'm totally lost on the loop front I need to get the date aswell as obviously produce the data that it pulls through from the Database for that date I've got something like this so far <?php $day=date(); do { $day++; echo "The date is " . $day . "<br />"; } while ($day<=date()); ?> Quote Link to comment Share on other sites More sharing options...
samshel Posted July 21, 2008 Share Posted July 21, 2008 <?php $result = mysql_query("SELECT * FROM $db_table"); $arrTitles = array(); while($day = mysql_fetch_assoc($result)) { $arrTitles[$day['day']] = $day['title']; } $intDays = date('t');//no of days in current month echo "<table><tr>"; for($intCount=1;$intCount<=$intDays;$intCount++) { echo "<td>".$arrTitles[$intCount]."</td>"; } echo "</tr></table>"; ?> try this...code is not tested.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.