How can i print values in same place when i echo the statement instead of printing the values in the same/next line (if using br tag). This is a sample for-loop
for($i=0;$i<10;$i++)
{
echo $i;
}
The output will be:
0123456789
But I want my output to be the one as shown below as it should overwrite values in the same place.
9
I will display all the values slowly one by one using timer or sleep but my objective here is to overwrite the previous value or display it in the same place.
I'm sorry, but you can't do it.
If you want to change the output in the browser itself, after you flushed the buffer, you have to use JS.
Think of it this way:
When you echo, you put some values in the buffer stream. When the buffer is flushed, you see the printed value in the browser.
You can manage the buffer, flush it, clean it, change it, but you can't change whats already out there.
I hope that helps some how.
Look at this link for more information about stream output:
Output Control
Related
Should be a pretty obvious answer, but I have spent several hours looking at existing similar questions and none are working for me
My code generates logfiles for (manual) debugging etc
If I use print_r($array,TRUE) to capture the output from an array as a string and then echo with <pre> tags to display that on screen, it's really easy to view and understand what's going on.
However, when I write the same info to the logfile, fwrite doesn't preserve the line break and indentation formatting so there is a splurge of info that takes significant amounts of time to make sense of, esp larger arrays and objects.
I have tried using output buffer
$string=print_r($array,TRUE);
ob_start();
echo "<pre>$string</pre>";
$outputBuffer = ob_get_contents();
ob_end_clean();
fwrite($handle,$outputBuffer);
However, all that's now happening is that I see the <pre> tags added into the basic, non-layout output
e.g.
<pre>DOING QUERY: SELECT * FROM event_triggers WHERE DateTime<='2015-09-16 13:04:30'</pre><pre>Completed checking for event triggers</pre>
You can't just add HTML tags to a document, open it in an editor expect HTML tags to be rendered correctly.
You either have to setup your log file as a HTML file (doesn't neccessarily have to be valid, so just add .html to the file name and open it in the browser) or use var_dump to echo out the variables.
Rename file to .html extension and just open with a browser. Browser will detect it with line break html document. <pre></pre> will output like <p></p> in the browser.
I have an array of items. There are two "types" or items: a heading row, and a regular row. I want to print the heading only if there are any "regular" rows under it (before the next heading).
(This is a simplification - in practice I wont be able to look at the next item from in the current iteration).
To solve this, I used buffer control. I clear and start the buffer at each heading row, and in each regular row I flush the buffer. So the heading wont print if there is a heading after it (buffer will be cleaned) and will be flushed to user if there is a regular row to "flush" it out.
(psudeo code)
for i in array:
if i is heading:
ob_end_clean();
ob_start();
echo "header $i";
else:
ob_end_flush();
echo "regular $i"
This works great.
Problem is, I have an option to download the page as a PDF and this broke it. The way it works is if the pdf flag is on, at the top of the page, buffer was started and at the bottom of the code, the buffer was dumped into a variable and sent to the pdf api as a string. Now, the buffer is sent to the user at ob_end_flush() which produces this error when I try to download the pdf:
cannot modify header information - headers already sent.
I like my solution but is there a way to make it work? Or is it a bad solution to begin with? thanks
Instead of outputing to buffer, output to a file buffer instead. At then end just send the whole file (it is a bit tricky but it should work fine if you manage the redirections correctly):
pseudo-code:
for i in array:
if i is heading:
// output to file buffer
echo "header $i";
else:
// stop output to file buffer
echo "regular $i"
My suggestion is, even though you can't look at the next item, I think you could keep track of the previous item. I think this should work regardless of your output method.
$previous = '';
foreach ($rows as $row) {
if ($row == 'header') {
// Wait to print header rows depending on what the next row is.
$previous = $row;
} else {
// For regular rows, print the preceding header row (if there is one.)
echo $previous;
echo $row; // Print the regular row.
$previous = ''; // Then clear the saved header row.
}
}
I'm having a problem where my PHP scripts are returning my json encoded array with a number above and a number below it. Like follows:
26
[0,"edited_token_string"]
0
I have not changed any of the scripts that I'm encountering this on, but it is happening with all of them. I don't have any other echos other than the one used to echo the array. Our server was returning "null" from all of the scripts all morning and now is returning the correct array, with these numbers surrounding it. Is it possible something was updated on the server that accidentally turned on some type of debugging? I've called our hosting service, but they are incredibly unhelpful.
Thanks in advance,
Max
Try adding header('Content-type: application/json'); directly above the line that outputs your JSON. If something else has already outputted something, you will get an error telling you where in your code that happened.
For the undesired output after the JSON, could it be that there is also an extra space after the closing ?> tag? A quick and dirty fix would be to just add die; after the last intentional echo;
I have a php script that does a query in my database and returns a string ( like "2" ). I print it using
print strip_tags('2');
but in the output of my browser I get :
<body><html>2</html></body>
Is there any way to prevent the tags from beiing printed? Is it maybe that the browser auto adds them?
For all those answering about strip_tags (" 2 ");
THIS IS WRONG:
I want a siple version.php
with
echo '2';
and nothing else. It prints the tags too. I don't have the tags and then try to print.
More explanation to those who try to get easy rep
my code is:
$str = '2';
print strip_tags($str);
and it prints
<html><head></head><body>2</body></html>
It is not possible. The browser creates these elements automatically, without it there would not be any text flow(means nothing of this could be made visible). You can just use this variable for any script, it won't include the HTML tags. This is only made by the browser to make it visible for you.
You can use
header("Content-Type: text/plain");
at the beginning of your script, in order to tell the browsers you're only gonna send plain text, not html. This will prevent your browser from automatically adding those html tags.
Then, check what you print (or echo). Here, the body tag should be in html tag.
i have a site, where i buffer some output with
ob_start();
...
and it worked fine until today i updated my debian from an older php5.3 to the latest php5.3.3-7+squeeze8
Now i sometimes have something in the output buffer before i call it the first time
please don't answer things like
"header must be called before any output is sent."
(I know, I work a lot with output buffers)
when i set an extra ob_get_clean(); at the very first line of my script, it works
<?
ob_get_clean();
it seems, like php is creating some output beforehand
if i put the first line
<? print_r(ob_get_clean()); ?>
then i see, that there is an empty string already in the buffer:
""
on all other pages it isn't, there ob_get_clean(); contains
null
is it possible you have some " " in front of your <?php somewhere? or wrong file encoding issue its usually some kind of that nature, check your files and include files.
Now i sometimes have something in the output buffer before i call it
the first time
It'll be much easier if you give us some info about that mysterious data.
perhaps a case of BOM character?
more info here
i found it:
i had no invisible character in front, it was something different: i called ob_end_clean() one time too much:
this was my code, inside a function i call:
function print_something(){
ob_start();
echo some stuff...
echo ob_get_clean();
ob_end_clean(); // this was the bug!
}
it seems, that you can clear your main output buffer ;)