Daybook/Logbook/Diary

Hello guys and gals.

I have a database built up and have two table in which I would like to display in the form of a diary or daybook. First table contains date, vehicle. Second Table holds job info.

I can get it to display in rows of date but is there a way of making it display more like this.

         M   |  T  |  W  |  T  |  F  |  S  |  S

Vehicle 21st 24th
clean tidy

so the date recognises the day etc… and displays in that place rather than what I have at the moment of

vehicle | date | Info - all in rows.

Any help? thoughts ?

This is what I have so far…but it does not work correctly…

[php]$datesrc = date(‘Y-m-d’, strtotime("-1 days"));
$datesrc2 = date(‘Y-m-d’,strtotime("+6 days"));
$date = date(‘d’);
$date1 = date(‘d’,strtotime("+1 days"));
$date2 = date(‘d’,strtotime("+2 days"));
$date3 = date(‘d’,strtotime("+3 days"));
$date4 = date(‘d’,strtotime("+4 days"));
$date5 = date(‘d’,strtotime("+5 days"));
$date6 = date(‘d’,strtotime("+6 days"));

echo ‘

’;
	$db_host = "localhost";
	$db_user = "******";
	$db_pass = "******";
	$db_base = "******";

$db_server = mysql_connect($db_host, $db_user, $db_pass);

if (!$db_server) die("unable to connect: " . mysql_error());

mysql_select_db($db_base)
	or die("Unable to select database: " . mysql_error());
						
						$query = "SELECT veh_reg, DATE_FORMAT(veh_date, '%d'), eas_no FROM daybook_db NATURAL JOIN job_db WHERE veh_date>=('$datesrc') AND veh_date<=('$datesrc2')";
								
		
	$result = mysql_query($query);			

	$rows = mysql_num_rows($result);

			for ($j = 0 ; $j < $rows ; ++$j)

			{
	
	$row = mysql_fetch_row($result);
				
	
			
		if ($row[1]==$date) echo '<tr><td>' . $row[0] .  $row[1] . '</td>';
		else echo '<td>No Vehicle</td>';
		if ($row[1]==$date1) echo '<td>' . $row[0] .  $row[1] .  '</td>';
		else echo '<td>No Vehicle</td>';
		if ($row[1]==$date2) echo '<td>' . $row[0] .  $row[1] .  '</td>';
		else echo '<td>No Vehicle</td>';
		if ($row[1]==$date3) echo '<td>' . $row[0] .  $row[1] .  '</td>';
		else echo '<td>No Vehicle</td>';
		if ($row[1]==$date4) echo '<td>' . $row[0] .  $row[1] .  '</td>';
		else echo '<td>No Vehicle</td>';
		if ($row[1]==$date6) echo '<td>' . $row[0] .  $row[1] .  '</td></tr></table>';
		else echo '<td>No Vehicle</td></tr></table>';
		
		
			
		
		
	} 

?>[/php]

’ . $date . ‘ ’ . $date1 .’ ’. $date2 . ‘ ’ . $date3 . ‘ ’ . $date4 . ‘ ’. $date5 . ‘ ’. $date6 .’

I have progressed to get pretty much the results I need, I just need help getting it cleaner rather than using 900 IF statements, I also need it to be more dynamic so I can have back arrows to then go from one week to another.

This is just one week, thats all. I want any week possible.

Any help at all on this one guys?

[php]<?php

$datesrc = date(‘Y-m-d’, strtotime("-1 days"));
$datesrc2 = date(‘Y-m-d’,strtotime("+6 days"));
$date = date(‘D, d’);
$date1 = date(‘D, d’,strtotime("+1 days"));
$date2 = date(‘D, d’,strtotime("+2 days"));
$date3 = date(‘D, d’,strtotime("+3 days"));
$date4 = date(‘D, d’,strtotime("+4 days"));
$date5 = date(‘D, d’,strtotime("+5 days"));
$date6 = date(‘D, d’,strtotime("+6 days"));

echo ‘

’;
	$db_host = "localhost";
	$db_user = "+++++";
	$db_pass = "+++++";
	$db_base = "+++++";

$db_server = mysql_connect($db_host, $db_user, $db_pass);

if (!$db_server) die("unable to connect: " . mysql_error());

mysql_select_db($db_base)
	or die("Unable to select database: " . mysql_error());
						
						$query = "SELECT veh_reg, DATE_FORMAT(veh_date, '%a, %d'), eas_no FROM daybook_db NATURAL JOIN job_db WHERE veh_date>=('$datesrc') AND veh_date<=('$datesrc2')";
								
		
	$result = mysql_query($query);			

	$rows = mysql_num_rows($result);

			for ($j = 0 ; $j < $rows ; ++$j)

			{
	
	$row = mysql_fetch_row($result);
				
	echo'<tr><td>';
	
	
			
		if ($row[1]==$date) { echo $row[0] .'<form method="get" action="DAY.php"><input type="hidden" id="reg" name="reg" value="' . $row[2] . '"><input type="submit" value="More info..." /></form>' ;}
		else {echo 'FREE';}
		
	echo '</td><td>';	
	
			
		if ($row[1]==$date1) {echo $row[0] .'<form method="get" action="DAY.php"><input type="hidden" id="reg" name="reg" value="' . $row[2] . '"><input type="submit" value="More info..." /></form>' ;}
		else {echo 'FREE';}
		
	echo '</td><td>';
	
			
		if ($row[1]==$date2) {echo $row[0] .'<form method="get" action="DAY.php"><input type="hidden" id="reg" name="reg" value="' . $row[2] . '"><input type="submit" value="More info..." /></form>' ;}
		else {echo 'FREE';}
		
	echo '</td><td>';
	
	
			
		if ($row[1]==$date3) {echo $row[0] .'<form method="get" action="DAY.php"><input type="hidden" id="reg" name="reg" value="' . $row[2] . '"><input type="submit" value="More info..." /></form>' ;}
		else{ echo 'FREE';
		}
	echo '</td><td>';
	
	
			
		if ($row[1]==$date4){ echo $row[0] .'<form method="get" action="DAY.php"><input type="hidden" id="reg" name="reg" value="' . $row[2] . '"><input type="submit" value="More info..." /></form>' ;}
		else {echo 'FREE';
		
		}
		
		
		
	echo '</td><td>';
	
	
	
			
		if ($row[1]==$date5) {echo $row[0] .'<form method="get" action="DAY.php"><input type="hidden" id="reg" name="reg" value="' . $row[2] . '"><input type="submit" value="More info..." /></form>' ;}
		else {echo 'FREE';
		
		
		
		}
		
	echo '</td><td>';
	
			
		if ($row[1]==$date6) {echo $row[0] .'<form method="get" action="DAY.php"><input type="hidden" id="reg" name="reg" value="' . $row[2] . '"><input type="submit" value="More info..." /></form>' ;}
		else {echo 'FREE';
		
		}
		
	echo '</td>';
			
	}

?>

’ . $date . ‘ ’ . $date1 .’ ’. $date2 . ‘ ’ . $date3 . ‘ ’ . $date4 . ‘ ’. $date5 . ‘ ’. $date6 .’
[/php]

Yikes… First, remove all those 's… You only need one form. Inside your outside of your code.

That will be much easier to read.

Now, this code doesn’t do what you want it to. If you look at where you compare row(1) to date1 and row(1) to date2, the results of these compares do the same thing. They display the exact code after the compare…
So, no matter what date row(1) is equal to they do the same thing. I am assuming you are doing this to get the “FREE” results. Remove them all and the compare and just have: (And, change the “FREE” check…)

echo $row[0] .'<input type="hidden" id="reg" name="reg" value="' . $row[2] . '"><input type="submit" value="More info..." />

So, as posted, your old code becomes this:

<form method="get" action="DAY.php">
<?php

$datesrc  = date('Y-m-d', strtotime("-1 days"));
$datesrc2  = date('Y-m-d',strtotime("+6 days"));
$date  = date('D, d');
$date1 = date('D, d',strtotime("+1 days"));
$date2 = date('D, d',strtotime("+2 days"));
$date3 = date('D, d',strtotime("+3 days"));
$date4 = date('D, d',strtotime("+4 days"));
$date5 = date('D, d',strtotime("+5 days"));
$date6 = date('D, d',strtotime("+6 days"));

echo '<table border="3"><tr><th>' . $date . '</th><th>' . $date1 .'</th><th>'. $date2 . '</th><th>' . $date3 . '</th><th>' . $date4 . '</th><th>'. $date5 . '</th><th>'. $date6 .'</th></tr>';

$db_host = "localhost";
$db_user = "+++++";
$db_pass = "+++++";
$db_base = "+++++";

$db_server = mysql_connect($db_host, $db_user, $db_pass);
if (!$db_server) die("unable to connect: " . mysql_error());

mysql_select_db($db_base)
or die("Unable to select database: " . mysql_error());

$query = "SELECT veh_reg, DATE_FORMAT(veh_date, '%a, %d'), eas_no FROM daybook_db NATURAL JOIN job_db WHERE veh_date>=('$datesrc') AND veh_date<=('$datesrc2')";

$result = mysql_query($query);

$rows = mysql_num_rows($result);

for ($j = 0 ; $j < $rows ; ++$j)
    {

$row = mysql_fetch_row($result);

echo'<tr><td>';

for ($i = 0; $i <= 6; $i++) {
  $tempdate = date('D, d',strtotime("+" . $i . " days"));
  if ($row[1]==$tempdate) { echo $row[0] .'<input type="hidden" id="reg" name="reg" value="' . $row[2] . '"><input type="submit" value="More info..." /> ;}

  else {echo 'FREE';}
  }
echo '</td>';
}

?>
</form></tr></table>

You could go one step further and make your heading a loop like
for ($i = 0; $i <= 6; $i++) {
$tempdate = date(‘D, d’,strtotime("+" . $i . " days"));
and loop thru your heading echo’ing something like: echo("

" . $tempdate . "’);
This way you would not need to assign all of those $date1-7, etc…

Hope all that helps you. Should make the page a little faster to without the extra forms… Good luck…

I kinda like puzzles, I kinda opted to go for the layout you wanted.
You may ask questions, but I prefer you do research on how it is done before asking :slight_smile:

[code]<?php
// header(‘Content-Type: text/plain’); // Uncomment to view the html code generated

// Get todays weekday in numeric 0=sunday - 6=saturday
$dow=strftime(’%w’);

$start=strtotime(($dow==1)?‘now’:‘last Monday’); // Is today Monday? If not use last monday
$end=strtotime(‘next Sunday’,$start); // Get Next Sunday in time form

// Dummy data Generator
$rdata=rand(2,6); // How many elements to generate

$rmakes=array(‘Chevy’,‘Ford’,‘Honda’,‘Toyota’,‘Pontiac’);
$haveday=array();
for($i=0;$i<$rdata;$i++)
{
do {
$rday=rand(0,6);
} while(in_array($rday,$haveday)); // Do not have same random day data
$haveday[]=$rday;
$rdate=strtotime((’+’. $rday .’ days’),$start);
$veh=$rmakes[rand(0,count($rmakes)-1)];
$db[]=array($veh,$rdate);
}
// Sort by date
for($i=0;$i<(count($db)-1);$i++)
{
for($j=$i+1;$j<count($db);$j++)
{
if($db[$i][1]>$db[$j][1])
{
$tmp=$db[$i];
$db[$i]=$db[$j];
$db[$j]=$tmp;
}
}
}

// Our html generation begins
echo ‘

’.PHP_EOL;
$days=‘MTWTFSS’;
for($i=0;$i<7;$i++) // Create our weekday header (Yes, I hate repetitive stuff
echo ‘’.PHP_EOL;
echo ‘’;
// Loop through out db
echo ‘’;
$curdate=$start;
foreach($db as $row)
{
while($curdate<$row[1]) // Are we on a weekday without data?
{
echo ‘
’. substr($days,$i,1) .’
FREE
’.PHP_EOL;
$curdate=strtotime(’+1 day’,$curdate);
}
echo ‘’.PHP_EOL;
echo ‘
’. strftime(’%d’,$curdate) .’
’. $row[0] .’
’.PHP_EOL;
$curdate=strtotime(’+1 day’,$curdate);

}
echo ‘’.PHP_EOL;

?>[/code]

I was just trying to give you the basics , not the entire code… Looks much better than the beginning… Nice!

Not sure by your response, but, was there another question? LOL…

Thanks for the compliment :slight_smile:

It’s a quick example of how I would do it. with some basic commenting to show whats going on. I like to provide examples like this, not to provide people with an answer, but to provide code to study and learn from and learn some new tricks. As well as an exercise for myself to solve and create/test some ideas.

Tricks:

  1. strtotime to do date/time manipulations/calculations.
  2. a simple array sort system, to do multidimmed arrays (ahhh, insertion sort is easy and does quite well)
  3. condensation of if/else blocks by using loops and updating variables (Exactly as you suggested and demonstrated except I use a while and you used)
  4. Sometimes a short script to test ideas, is easier to do than trying to work on a live script
  5. header trick to display output generation, useful when it comes to testing idea scripts, without html or a quick way instead of ‘View Source’ from the browser :slight_smile:

Even with the script the coder still has a lot to do in order to implement it into his script :slight_smile:

My comments is a general comment for coders, as some will use the code and wont know how it works, so they come back and ask silly questions about how to modify to suit their needs. But for those who do take time to dissect the code and understand, and ask questions about the code so they can have a new tool in their bag of tricks…

Plus I like solving puzzles :slight_smile:

Some of my example/snippet scripts can be found on GitHub Gist
for a few more usefule tricks :slight_smile:

There has to be different form tags for cell because I have another query on the page which displays information about that vehicle when you select ‘more info’ this does not work with one form tag as it will GET all the data from every hidden input field (row) and return the same vehicle info everytime. I tried using the one form tag in prior versions.


current display

this shows too many rows, it should not display ‘FREE’ (false) in the other data when it has already found one to be TRUE. 6 rows when i should be about 2/3. (terrible english and explanation i know…)


like this…but as im dealing in rows i cant see this happening so i might just remove the FREE ‘ELSE’ echo alltogether

I am sifting my brain through your other code and LAFFINS…so I can include the principles.

Cheers for your input guys I hope I will get my head around it all.

Laffin went into more detail than I did. Since you showed more samples of what you are doing, it will help with further questions. So, if you get stuck again, let us know…

tried to use your wisdom and came up with more code that does not work - the variable $date seems to be my problem (not to mention the others).

[php]$day = $_POST[‘day’];
$mon = $_POST[‘month’];
$yer = $_POST[‘year’];

$comb = $yer . ‘-’ .$mon . ‘-’. $day;

$date = strtotime($comb);

$datesrc = date(‘Y-m-d’, strtotime(’-1 days’, $date));
$datesrc2 = date(‘Y-m-d’,strtotime(’+31 days’, $date));

echo<<<_END

Date: _END; for($i=0;$i<=31;$i++){ if($i < 10) { $no ="0".$i; } else { $no = $i; }
print "<option value=\"$no\">$no</option>\n"; 
}

echo '</select>-<select name="month">';

for($i=0;$i<=12;$i++){	
if($i < 10)	{		$no ="0".$i;	}	
else	{		$no = $i;	}	

print "<option value=\"$no\">$no</option>\n"; 
}


echo '</select>-';
echo<<<_END

<select name="year">
<option value"2012">2012</option>
<option value"2013">2013</option>
<option value"2014">2014</option>
<option value"2015">2015</option>
<option value"2016">2016</option>
<option value"2017">2017</option>
</select>
[/php]

this this is the results bit…

[php] for($n=0;$n<=10;++$n) {

							print '<th>'. date('D, d', strtotime('+' .$n. ' days', $date)) . '</th>';
							
							echo '</tr><tr><td>';
							
							
							if($row[1]==$date) {echo $row[0];}
							
							else {echo 'FREE';}
							
							echo '</td></tr>';
							
							}[/php]

any more pointers?

Well, Kid,
I gave you general info to help stear you to a solution. Laffin gave you a solution without your data.
You displayed a sample with “more-info” buttons that looked nearly complete. Looks like you are on the way to a finished project.

So, going back to the working “more-info” button display. What is wrong with this and what help do you want with it. If you can explain what you need and show just that part of the code, we can help. Not sure what you meant by too many "free"s showing…

The sample you mention ‘worked’ but didnt work in the way that I need, if there were 6 results to the query, it would display the one correct number plate and then 5 ‘FREE’ results - I need it to display only one - the plate or the FREE.

I used part of code which you guys gave to me, using the ‘FOR’ loop which almost gets my desired results, just the loop stops once it has found one correct result and does not display all the correct results - only the first one found.

example: I have number plates for the 10th/11th and 12th - I then search startng from the the 10th. The page will then display the number plates for the 10th but then FREE for 11th and 12th so my loop stops.

well it doesnt stop but it doesnt work for me haha.

They are examples for you to follow on how to get to the solution.
Not the solution itself. What good would it do us if we spent our time providing a solution for everyone?
Only we would learn, but this is a php help forum, where ppl come to get guidance and learn for themselves.

Both examples provided show this extremely well. It’s up to you to understand how they work. Ernie used your code, which should be simpler to understand. Mine was from scratch, which means you will invest more time in learning it.

I fully understand what you two are saying but maybe I am not asking my question properly.

for the display I have used your code and that works perfectly from what i have taken and adapted and for that am im in sync!

just asking about the querying now - row results.

i need it to loop all true not stop once it finds its first true result. i thought the For looped till false, which means I am doing something wrong…just hoped that you could see in my post any errors which would make it stop and not display all correct rows.

I am guessing I am on my own again haha.

Thanks for the pointers previously then.

The problem you will encounter is building the columns and rows.
since php displays goes from column to column you have 2 options

  1. prefetch all the data, and build a data array according to row & column, then use this array to display your table
  2. use a table within a table to build the columns, which is much simpler. Using loops to fill up each row (in the inner table of each column) and fill in any ‘Free’ Spaces, than move to the next date

sweet jesus you got me there!

I have it how i wanted now and all it took was for me to stop being an idiot.

Cheers boys - my brain stopped farting after reading your second option Laffin.

LOL, Thats what I like to see myself. When the lightbulb strikes and code flys from the fingertips of the users.
I think most helpers will agree with that.

Gratz on getting it all figured out

LOL, Yep! 100% agree with Laffin… That warm and fuzzy feeling at the end of fixing a problem is great!
Glad to hear you figured it all out…

I spoke a bit too soon - it works in part.

to display my HTML i am using a FOR loop to generate a variable $temp to render 10 dates.

within this FOR loop I have inserted an IF statement to match my query $row to match $temp - so if true echo my vehicle - false echo ‘FREE’.

I echo both my $row and $temp date and as they loop those lines in the loop have to match in order to get a true result.

I tried to alter this by sticking it into an array (mysql_fetch_array) etc… but the loops read one line at a time so if the dates do not match in that line it reads false.

How do I get it to match ($temp==$row[]) and search the whole row array for a true result and not LINE BY LINE.

[php]for($l=0;$l<=10;++$l){

		$temp = date('Y-m-d', strtotime('+' .$l. ' days', $date));

$row = mysql_fetch_array($result, MYSQL_NUM);//tried a few different types

echo $temp . $row[1];//seeing results for my own piece of mind that the query is correct

if($row[1]==$temp) {echo ‘

’ . $row[0] . ';
}
else {echo ‘FREE’;}
}[/php]

I tried to think of a way but unless the lines match line by line within the loop then it is always false even though the dates do match.

Can I run the loop and then stick the loop within an array and then after the loop use the if statement to match the dates???

Yes, I think you are getting it. You can nest for’s.

Basically, as you loop thru rows you can create another for to loop thru items in the current row.

Just think out your goal first and then code it. The solution is always there to find.
Sometimes it is so simple and staring right at you…

Two ways to use inside loops, you can requery, but, no need to do this in your case.
I would just use a new for loop to go thru the row and test for whatever you need.

Hope that made sense… Good luck… (Ask again if stuck!)

Sponsor our Newsletter | Privacy Policy | Terms of Service