Using PhP to control what is Printing - php

Good day everyone
I have a printing problem so I'm going to explain the problem and share some basic code but the coding is not the problem, the problem is that I'm using the wrong method there must be a better method.
My web page is generated by using PhP to fetch information from mySQL and display each set of information as a table. so I may have 3 tables on 1 page or 100 tables. the problem is I really need to print this but when I do google puts the page breaks straight through my table.
the tables vary in height depending on how much information I need to display.
I use javascript to set the print area.
below you will see I try anticipate the page break with table 3 but when table 1 and 2 take more than a page, it puts the break through table 2 then the rest of page 2 is empty and table 3 is only found on page 3. In this case i would need it to put the break before table 2 so the whole table prints on page 2.
<div id='print_area'>
<table>
this is table 1
</table>
<table>
this is table 2
</table>
<table style='page-break-before:always'>
this is table 3
</table>
</div>
Sorry I need to leave for the day but I'm really hoping some one will be able to help with this problem. I will be checking back tomorrow.
Thank you in advance!

You'll never really be able to guarantee page layout just using PHP and HTML. Have you considered creating a PDF instead of a printable web page? There are very simple but powerful PDF libraries available for PHP, my preferred one is FPDF.

Count the number of rows in each table before you actually print them. Assume a number of rows that fit on a page, and space you need for table headers/space between tables.
So for example
$rows_between_tables=3
$rows_needed_for_header=2
$rows_used_on_this_page=0
$rows_per_page=72 # (depends on font size etc.)
while (there_is_another_table_to_print()) {
$rows_after_this_table = $rows_used_on_this_page +
$rows_needed_for_header + $rows_between_tables +
rows_for_next_table()
if ($rows_after_this_table > $rows_per_page) {
$rows_used_on_this_page=$rows_needed_for_header + rows_for_next_table()
print_next_table_with_page_break_in_front_of_it();
}
else {
$rows_used_on_this_page=$rows_after_this_table;
print_next_table_no_page_break();
}
}

Related

How to set visibility of table row in PHP Office Word TemplateProcessor

I would like to conditionally display table row in predefined Ms Office Word file. The solution I am using currently only allows to manage display of whole block and so on the whole tables according to different combination of displayed data. I find this solution obscure. This is why I need conditionally display table row.
Current solution works with:
$$nameofdesiredkey
the conditionally shown content
nameofdesiredkay$$
I was able to solve that issue by workaround using another data table as result of dynamic values from pontential spread of values in the original table.
During my reserach idea of using multiple tables, using one dynamic data table list in one TD or just using $$variablestart variableend$$ and the start and end of each of nearest surrounding TDs seemed potent also.

Getting trouble in assigning id value in database in php [duplicate]

This question already has answers here:
auto increment primary leaving gaps in counting
(5 answers)
Closed 7 years ago.
i am making s simple registration page using php and mysql database.
id column of Table is AUTO INCREAMENTED
Registration Process working well i.e., data is getting Inserted in database.
currently i have 3 records in my table.
like
Raghvendra
Surendra
mohit
Now My Problem is:
When i deleted The 3rd Record i.e
mohit
My database looks like
Raghvendra
Surendra
Now, When I add another Record, my table looks like
Raghvendra
Surendra
4.varun
What is want is like
Raghvendra
Surendra
varun
sorry for my english..Help me!
Don't worry about the gaps. Either you need a unique number for each person, in which case, reusing 3 is a bad idea (does it refer to Varun or Mohit?) or you're only concerned with the names, and you can use CSS (an ordered list) to number them on the webpage when they're displayed.
It's generally very useful to preserve the order in which people are added to a database, and trying to "fill in the gaps" prevents that.
Also, remember there's no guarantee that for every user you delete, leaving a gap, there will be a new user available to fill it.
The harsh answer is to stop caring what the id is (it's really just an internal unique identifier, so no need to show it to the user).
If you are displaying a listing to the user, just use an <ol> element.
echo '<ol>';
while ($row = $results->fetch_row()) {
echo '<li>' . $row->name . '</li>';
}
echo '</ol>';
That will render the results are you expected, but you really should just treat the id as an internal thing and not worry about its actual value.

Using mysqli query to display second query while still displaying first query

I would like to have two separate queries. The first query I would like to display a bunch of numbers in descending order in a table (pretty simple to do). Then what I would like to do is a second query which for each of those matching numbers I would like to display the corresponding data. The reason I want to do it separately instead of just combing it all into one query is so that I can display all of the results of the first query whether it has data or not. IE:
1 - (blank)
2 - I have data
3 - I also have data
4 - (blank)
5 - I have more data
So far the code I am using is:
// Perform number query
$number_query = mysqli_query($con,$num_query);
while($numbers = mysqli_fetch_array($number_query)) {
// Do data query
$data_query = mysqli_query($con,$d_query);
while($data = mysqli_fetch_array($data_query)) {
if ($numbers['number'] == $data['number']) {
echo $numbers['number'];
echo $data['content'];
echo '<br>';
}
else {
echo $numbers['number'];
echo '<br>';
}
}
}
I dont think I'm taking the right approach but it does seem that I am getting partially the right results as it is displaying the data next to the numbers with content. The problem is that for each piece of data in the database the $numbers['number'] is being replicated. So if $data['content'] has 5 database entries then each entry of the $numbers['number'] is being replicated 5 times.
-------Edit-----
Ok I realized why my question wasnt making sense (maybe). I'm going to use an example of these tables:
Table 1
unique_id | number | identifier | etc
Database Table 1 might be updated once a month, and all the numbers and identifiers must be visible on the page at all times. The query needs to be kept separate because it is used multiple times (more then 10) on other pages so I've included it outside of the page in a header file.
Table 2
unique_id | number | description | etc.
Database Table 2 needs to be referenced via the number columns, this content is changing on an interval of about every 10-15 minutes, now Table 1 might only have 50 rows of data while Table 2 probably has close to a 100+ new rows a day. The query is embedded on the page because it will be the only page that actually uses this specific query.
The idea with the layout is something like this:
<table>
<tr>
<td>Number & Identifier</td> (taken from Table 1)
<td>Description</td> (content from table 2)
<td>(more content from table 2)</td>
etc
So looking back at the top with my question is the problem is that for every description and other content that is being displayed on the page with the corresponding number and identifier information, all the number and identifiers without descriptions or content are being replicated the same amount of times as there are entries with descriptions and content.
If I remove the else conditional statement then the issue becomes that the blank entries do not display at all, which doesnt meet my requirements. The reason that these blank entries need to be in there is that this page can be printed off, so people not using computers can fill out the blank fields with pen/pencil and it can be manually entered in after the fact.
I do understand your question now, but i still think that the solution using join as i posted earlier is your best bet.
http://www.sqlfiddle.com/#!2/26854/2

Match performance times with audience availability using MySQL & PHP

I've looked at a lot of the stackoverflow answers around this subject, but they all seem to be coming at it from a different direction. Here's what I want to do, and at this stage I'm more head-scratching as to the way to approach it than writing the specific code.
The task is to display shows that a user could see based on their existing commitments, whether that be not available or already seeing something else.
Table A contains a list of start & end times of user unavailability
Table B contains a list of start & end times of user existing bookings.
Table C contains a list of start & end times of all shows.
To add more detail to this, I'm using FullCalendar agendaView to display a users commitments. In the gaps I want to display shows they could see. The code to display the unavailability and existing commitments is already written, but I have no clue now trying to find shows in Table C that don't clash with entries in Tables A & B.
Any ideas?
My solution is for PHP, but I'm sure there is a better solution hiding in an SQL stored procedure or something.
If you work with Unix Time you can simply test for open slots by comparing begin and end times:
Dynamically create a list of available slots for a user.
Iterate through this list and compare an open slot with each show in Table C. Something like
if (
(showtime['begin'] >> opentime['begin']) &&
(showtime['end'] << opentime['end'])
) return true;
If you find a match, print it out.
EDIT:
After thinking a bit I came up with possibly a better solution. There is no way to get away from not comparing everything item by item. So the trick is to instead of doing all the processing at once, spread it out over the entire lifecycle of the app:
Edit the database and add duration field to all the tables.
Make a stored procedure (mySQL can do this, right?) that triggers on a table update. This procedure calculates the duration and fills the corresponding field for that tuple.
Now you select all the tuples whose duration =< some open slot
This way all the grunt work is done in SQL and not in PHP, so it could be faster.

Split data received into separate fields

Problem : Data supplied is not ok for me to handle directly.
Description : I'm using curl to get data I need from another website.
However the data supplied on this website is formatted in a way that I need to split it to be able to work with it.
Example:
I'm scraping a div (soccer competition), but the match is supplied in one field, I need to have the 2 teams separated into 2 fields.
MysqlDB Field holds after the scrape:
team 1 - team 2
I need to separate that DB info into 2 columns
preg_match would be able to do this, but there are many opponents, so its a hassle like this.
Right now, I added (manually which is a pain) a team_id column and added myteam to the games which involve my team to the db table and did a where team_id = myteam
This way I can at least sort the competition table and just get my teams games.
This is the data I'm talking about getting with curl:
$value['Wedstrijd'] = htmlentities($value['Wedstrijd'], ENT_QUOTES);
So right now I'm scraping the field and putting it in the db.
I'm wondering, how can I separate the content of $value['Wedstrijd'] into 2 writes.....
Is this even possible?
I'm not able to post the entire code here, the formatting gets screwed.
Using substr and strrpos I was able to get the first team out the field,
I thought setting it to -1 would give me the other way around, which it actually does, but it doesn't go back till the - symbol, it just gives me 1 letter/symbol then.
substr($wedstrijd,0,strrpos($wedstrijd,'-')); this would return the first team, but I'm not sure how to use this to get the team after the - symbol.
use strpos + substr or preg_match

Categories