Printing a dynamic content
Moderator: General Moderators
Printing a dynamic content
Hello! Could anybody help me.
I want to allow the clients to print (send to printer) a dynamical content, created by php.
To do this I created the javascript function:
<script type="text/javascript">
function printit()
{
var d = window.open();
d.document.open();
d.document.write("<html>\n<head>\n<title>To Print</title>\n</head>\n"+
"<body>\n"+
"<?php
$file_array = file('files/goods.txt');
$ad = explode('|',$file_array[0]);
echo '<table width = \"100%\" border = \"1\" class=\"goods\">';
echo '<tr>';
for($i=1; $i < (count($file_array)-1); $i++)
{
$ad = explode('|',$file_array[$i]);
echo '<tr>';
for($j=0; $j < count($ad); $j++)
{
echo '<td >';
echo $ad[0]; /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!THE TROUBLE IS HERE!!!!!!!!!!!!!!!!!!!!!!!! */
echo '</td >';
}
echo '</tr>';
}
echo '</table>';
?>"+
"</body>\n</html>\n");
d.document.close();
d.print();
}
</script>
I call the function in this way:
echo '<input type="button" value="To print" onclick="printit()"/><br />';
All works fine while I did not use variable index in that line, when I try to use $ar[$j] - the function doesn't work!
I cannot imagine where is the problem. Why $ar[0] works fine and $ar[$i] does not work?
I simplyfied the function for the example, in fact it uses inserted loops to read txt file into a table.
Sorry for my English, I did not find any answer on Russian forums, so I decided to try here.
I want to allow the clients to print (send to printer) a dynamical content, created by php.
To do this I created the javascript function:
<script type="text/javascript">
function printit()
{
var d = window.open();
d.document.open();
d.document.write("<html>\n<head>\n<title>To Print</title>\n</head>\n"+
"<body>\n"+
"<?php
$file_array = file('files/goods.txt');
$ad = explode('|',$file_array[0]);
echo '<table width = \"100%\" border = \"1\" class=\"goods\">';
echo '<tr>';
for($i=1; $i < (count($file_array)-1); $i++)
{
$ad = explode('|',$file_array[$i]);
echo '<tr>';
for($j=0; $j < count($ad); $j++)
{
echo '<td >';
echo $ad[0]; /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!THE TROUBLE IS HERE!!!!!!!!!!!!!!!!!!!!!!!! */
echo '</td >';
}
echo '</tr>';
}
echo '</table>';
?>"+
"</body>\n</html>\n");
d.document.close();
d.print();
}
</script>
I call the function in this way:
echo '<input type="button" value="To print" onclick="printit()"/><br />';
All works fine while I did not use variable index in that line, when I try to use $ar[$j] - the function doesn't work!
I cannot imagine where is the problem. Why $ar[0] works fine and $ar[$i] does not work?
I simplyfied the function for the example, in fact it uses inserted loops to read txt file into a table.
Sorry for my English, I did not find any answer on Russian forums, so I decided to try here.
Re: Printing a dynamic content
I think you need to use $ar[$j] and not $i.
Your second for loop uses $j and not $i in that loop there is a chance that $i is bigger then your $ad array so you get a invalid index or out of bounds error.
Your second for loop uses $j and not $i in that loop there is a chance that $i is bigger then your $ad array so you get a invalid index or out of bounds error.
Re: Printing a dynamic content
I would make sure which variable you are using, In one place you said you were using $ad[$j] while later you mentioned using $ad[$i]
Based upon the code you have though, I would replace this:
With this:
-Greg
Based upon the code you have though, I would replace this:
Code: Select all
$ad = explode('|',$file_array[$i]);
echo '<tr>';
for($j=0; $j < count($ad); $j++)
{
echo '<td >';
echo $ad[0]; /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!THE TROUBLE IS HERE!!!!!!!!!!!!!!!!!!!!!!!! */
echo '</td >';
}
echo '</tr>';Code: Select all
echo '<tr><td>',str_replace('|','</td><td>',$file_array[$i]),'</td></tr>';Re: Printing a dynamic content
It does not matter which variable to use inside [] $i or $j. The problem is that the function gives up working if I try to use any variable inside [].
If I use a number like $ar[0] or $ar[1] or anything else - the function works. Of course it does not create a coorect table, but it creates a page and prints it.
But when I try to use $ar[$ANYTHING] - the function does not reply at all!
I am slowly going mad................
If I use a number like $ar[0] or $ar[1] or anything else - the function works. Of course it does not create a coorect table, but it creates a page and prints it.
But when I try to use $ar[$ANYTHING] - the function does not reply at all!
I am slowly going mad................
Last edited by AlexZ on Thu Aug 11, 2011 5:40 pm, edited 1 time in total.
Re: Printing a dynamic content
You mention that the function does not work, so I am assuming you are talking about the javascript function. Have you checked the source code to make sure that the generated javascript looks appropriate. Have you checked what javascript errors may been thrown when the function was tried? (In Firefox, SHIFT-CTRL-J to bring up the console) then call the page). Will any of the content you are displaying have quotes inside of them? (if there are double quotes (") they will CLOSE your opening javascript quotes, so try
The first one (which you hard code using $ad[0]) may not have quotes, but $ad[3] may have which is why when you tested with only 0 it worked.
The way I do it is work backwards:
1. Try it.
2. If didn't work, what errors if any were given
3. check the code that was output (perhaps PHP is giving you an error, and that error contains quotes which are interrupting your javascript quotes)
4. If you got an error in the code instead of data, do a dump of the variable you are trying to use, does it contain the correct data
5. ... keep working your way back to does the file even exist and what is in it
Some people go the other way, I prefer this way.
Code: Select all
echo str_replace('"','\"',$ad[$j]);The way I do it is work backwards:
1. Try it.
2. If didn't work, what errors if any were given
3. check the code that was output (perhaps PHP is giving you an error, and that error contains quotes which are interrupting your javascript quotes)
4. If you got an error in the code instead of data, do a dump of the variable you are trying to use, does it contain the correct data
5. ... keep working your way back to does the file even exist and what is in it
Some people go the other way, I prefer this way.
NEW EDIT: Printing a dynamic content
Hello everybody. It's me again. With the same questuion. It seems to me that the solution is somewhere near, but I still cannot catch it.
I ask you to look at this. Here is my index.php:
This is the file goods.txt:
[text]number | name | item | price
1 | Good1| item | 29,56
2 | Good2| item | 100,49
3 | Good3| item | 163,30
4 | Good4| item | 81,65
5 | Good5| item | 85,06[/text]
Now the most interesting. When I run all this I have a javascript mistake "unterminated string constant at line 10 symbol 69",
This is the HTML generated by IE7. It proves that variable "line" is initialized, but the very first string of the variable "line" is "unterminated".
Though all the data from file is in that "line" as it should be:
And finally the resulted page created by IE7:
[text]
Print
1 Good1 item 29,56
2 Good2 item 100,49
3 Good3 item 163,30
4 Good4 item 81,65
5 Good5 item 85,06
6 Good6 item 76,61
7 Good7 item 58,36
[/text]
Why that string can be unterminated? I feel that this is something about ' or ", but I am not sure. Maybe anybody copies my index.php and that goods.txt
to try to test them. I feel myself such a stupid because of this error...
Once again I try to explain the problrm. PHP code inside function printme() creates a dynamical content, puts it into temporary window and then prints this
window by javascript print(). But while the content is created the first sting always had a mistake (unterminated string constant). I cannot see visually what
is "unterminated". All the data and ' and " and < or > are present! Because of this mistake the function peintme() refuses to work after clicking the button.
But if you change this statemrnt in printme():
for:
the function printme() will work and print.
SORRY FOR THIS LONG-LONG POST.
Hope for an advice
I ask you to look at this. Here is my index.php:
Code: Select all
<html><head><title>Test</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type='text/javascript'>
function printme(elem)
{
/* I prepare the string to print in vbariable line*/
var line="<h1>Price list</h1><table width = '100%' border = '1' class='goods'>"+
"<?php
$file_array = file('goods.txt');
for($i=1; $i < (count($file_array)-0); ++$i)
{
echo '<tr><td>',str_replace('|','</td><td>',$file_array[$i]),'</td></tr>'; /*I used the advice given here - Thank you!*/
}
echo '</table>';
?>";
var pw = window.open();
pw.document.open();
pw.document.write(line);
pw.document.close();
pw.print();
pw.print();
}
</script>
</head>
<body>
<?php
$file_array = file("goods.txt");
if(!$file_array)
{
echo("No goods");
}
else
{
echo '<input type="button" value="Print" onclick="printme(this); return false;"><br>';
/* the code below simply creates a humble table to place on the page - it works fine*/
/*the javascript function above reads the same data to prepare a temporary page for printing*/
echo "<br><table width = '100%' border = '0' class='goods'>";
echo "<tr>";
for($i=1; $i < (count($file_array)-1); $i++)
{
$ad = explode("|",$file_array[$i]);
echo "<tr>";
for($j=0; $j < count($ad); $j++)
{
if($j != 0 && $j != count($ad)-1)
echo "<td class='td_num'>".$ad[$j]."</td>";
else if($j == count($ad)-1)
echo "<td class='td_num_opt'>".$ad[$j]."</td>";
else
echo "<td>".$ad[$j]."</td>";
}
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
[text]number | name | item | price
1 | Good1| item | 29,56
2 | Good2| item | 100,49
3 | Good3| item | 163,30
4 | Good4| item | 81,65
5 | Good5| item | 85,06[/text]
Now the most interesting. When I run all this I have a javascript mistake "unterminated string constant at line 10 symbol 69",
This is the HTML generated by IE7. It proves that variable "line" is initialized, but the very first string of the variable "line" is "unterminated".
Though all the data from file is in that "line" as it should be:
Code: Select all
<html>
<head>
<title>шаблон сайта</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type='text/javascript'>
function printme()
{
var line="<h1>Price list</h1><table width = '100%' border = '1' class='goods'>"+
"<tr><td>1 </td><td> Good1</td><td> item </td><td> 29,56 /*!!!!!!!!!!!THIS IS LINE 10!!!!!!!!!!!!!!!!!!!*/
</td></tr><tr><td>2 </td><td> Good2</td><td> item </td><td> 100,49
</td></tr><tr><td>3 </td><td> Good3</td><td> item </td><td> 163,30
</td></tr><tr><td>4 </td><td> Good4</td><td> item </td><td> 81,65
</td></tr><tr><td>5 </td><td> Good5</td><td> item </td><td> 85,06
</td></tr><tr><td>6 </td><td> Good6</td><td> item </td><td> 76,61
</td></tr><tr><td>7 </td><td> Good7</td><td> item </td><td> 58,36
</td></tr><tr><td>8 </td><td> Good8</td><td> item </td><td> 45,25
</td></tr></table>";
var pw = window.open();
pw.document.open();
pw.document.write(line);
pw.document.close();
pw.print();
pw.print();
}
</script>
</head>
<body>
<input type="button" value="Print" onclick="printme(); return false;"><br><br><table width = '100%' border = '0' class='goods'><tr><tr><td>1 </td><td class='td_num'> Good1</td><td class='td_num'> item </td><td class='td_num_opt'> 29,56
</td></tr><tr><td>2 </td><td class='td_num'> Good2</td><td class='td_num'> item </td><td class='td_num_opt'> 100,49
</td></tr><tr><td>3 </td><td class='td_num'> Good3</td><td class='td_num'> item </td><td class='td_num_opt'> 163,30
</td></tr><tr><td>4 </td><td class='td_num'> Good4</td><td class='td_num'> item </td><td class='td_num_opt'> 81,65
</td></tr><tr><td>5 </td><td class='td_num'> Good5</td><td class='td_num'> item </td><td class='td_num_opt'> 85,06
</td></tr><tr><td>6 </td><td class='td_num'> Good6</td><td class='td_num'> item </td><td class='td_num_opt'> 76,61
</td></tr><tr><td>7 </td><td class='td_num'> Good7</td><td class='td_num'> item </td><td class='td_num_opt'> 58,36
</td></tr></table></body>
</html>
[text]
1 Good1 item 29,56
2 Good2 item 100,49
3 Good3 item 163,30
4 Good4 item 81,65
5 Good5 item 85,06
6 Good6 item 76,61
7 Good7 item 58,36
[/text]
Why that string can be unterminated? I feel that this is something about ' or ", but I am not sure. Maybe anybody copies my index.php and that goods.txt
to try to test them. I feel myself such a stupid because of this error...
Once again I try to explain the problrm. PHP code inside function printme() creates a dynamical content, puts it into temporary window and then prints this
window by javascript print(). But while the content is created the first sting always had a mistake (unterminated string constant). I cannot see visually what
is "unterminated". All the data and ' and " and < or > are present! Because of this mistake the function peintme() refuses to work after clicking the button.
But if you change this statemrnt in printme():
Code: Select all
echo '<tr><td>',str_replace('|','</td><td>',$file_array[$i]),'</td></tr>'; /*I used the advice given here - Thank you!*/Code: Select all
echo '<tr><td>',str_replace('|','</td><td>','ANY STRING or even $file_array[0]'),'</td></tr>'; /*I used the advice given here - Thank you!*/SORRY FOR THIS LONG-LONG POST.
Hope for an advice
Last edited by AlexZ on Fri Aug 12, 2011 2:38 am, edited 1 time in total.
Re: Printing a dynamic content
1. can you edit the post and use the tags that are available for wrapping PHP code to make it more readable.
2. Can you past the source code from when you visit this page, or the file goods.txt to know what it is trying to output.
2. Can you past the source code from when you visit this page, or the file goods.txt to know what it is trying to output.
Re: Printing a dynamic content
Try modifying your javascript function, it's most likely not working if you place a string over multiple line.
try modifying you php so it outputs the string properly like below:
try modifying you php so it outputs the string properly like below:
Code: Select all
function printme(elem)
{
var line="<h1>Price list</h1><table width = '100%' border = '1' class='goods'>"+
"<tr><td>1 </td><td> Good1</td><td> item </td><td> 29,56" +
"</td></tr><tr><td>2 </td><td> Good2</td><td> item </td><td> 100,49" +
"</td></tr><tr><td>3 </td><td> Good3</td><td> item </td><td> 163,30" +
"</td></tr><tr><td>4 </td><td> Good4</td><td> item </td><td> 81,65" +
"</td></tr><tr><td>5 </td><td> Good5</td><td> item </td><td> 85,06" +
"</td></tr><tr><td>6 </td><td> Good6</td><td> item </td><td> 76,61" +
"</td></tr><tr><td>7 </td><td> Good7</td><td> item </td><td> 58,36" +
"</td></tr><tr><td>8 </td><td> Good8</td><td> item </td><td> 45,25" +
"</td></tr></table>";
var pw = window.open();
pw.document.open();
pw.document.write(line);
pw.document.close();
pw.print();
pw.print();
}
Re: Printing a dynamic content
I did it! The problem was in "\r\n" in input file. To make everything work I added such string preprocessing:
the javascript function should look like this:
Code: Select all
$trimmed = trim($file_array[$i], " \r\n");Code: Select all
function printme()
{
var line='<h1>Price list</h1><table width = "100%" border = "1" class="goods">'+
'<?php
$file_array = file("goods.txt");
$sep = array("|", "\n", "\r\n");
for($i=1; $i < (count($file_array)-0); ++$i)
{
$trimmed = trim($file_array[$i], " \r\n");
echo '<tr><td>'.str_replace($sep,'</td><td>',$trimmed).'</td></tr>';
}
echo "</table>";
?>';
var pw = window.open();
pw.document.open();
pw.document.write(line);
pw.document.close();
pw.print();
}