I want to echo some output before and after some pause.
Here is my Code:
<?php
class AsyncOperation extends Thread {
public function __construct(){
}
public function run(){
sleep(11);
echo "Running ";
}
}
echo "Its Here";
flush();
$thread = new AsyncOperation();
$thread->start();
?>
This should show output "Its Here" ....and after 11 seconds ... should show "Running"
but the browser shows it like "Running Its Here", and shows the whole string after 11 seconds.
I don't know why this is happening . Please help me solve this problem I have been struck here for last 2 nights.
Any Solution ??
You need Output Buffering...!
I am not sure this works on all browsers, However this works perfectly on
Google's Chrome.
Mozilla FireFox
Internet Explorer
Opera
<?php
if (ob_get_level() == 0) ob_start();
class AsyncOperation
{
public function run()
{
for ($i = 0; $i < 10; $i++) {
echo " ";
echo str_pad('', 4096) . "\n";
# Flushing out..........!
ob_flush();
flush();
sleep(1);
}
echo "Running ";
}
}
echo "Its Here<br>";
$thread = new AsyncOperation();
$thread->run();
?>
I modified your run() with sleep(1) under a for loop which is same as sleep(11).
Related
i am trying to apply the background color in echo using an inline style but it is not applying background color in however changes only the text color. i want to change background color in a particular part of the code
echo "<p style='color:orange';background-color:red;>"."record number: ".$rec_num. "</p>"."<br>"
program code is
class db_access
{
private $_uname;
private $_pass;
private $_db;
private $_server;
//_construct connects databaseand fetchest he result
public function __construct($server,$user_name,$password,$d_b)
{
$this->_server=$server;
$this->_pass=$password;
$this->_uname=$user_name;
$this->_db=$d_b;
$con=mysql_connect($this->_server,$this->_uname,$this->_pass);
$db_found=mysql_select_db($this->_db,$con);
if($db_found)
{
$sql="SELECT * FROM login";
$result = mysql_query($sql);
if ($result)
{
while ( $db_field = mysql_fetch_assoc($result) )
{
static $rec_num=1;
//inline css
echo "<p style='color:orange';background-color:red;>"."record number: ".$rec_num. "</p>"."<br>";
print $db_field['ID'] . "<BR>";
print $db_field['u_name'] . "<BR>";
print $db_field['pass'] . "<BR>";
print $db_field['email'] . "<BR><br><br>";
$rec_num++;
}
//returns the connection name that is used as a resource id in __destruct function
return $this->_con=$con;
}
else {die(mysql_error());}
}
else
{return die(mysql_error());}
}
// destruct function closes database
public function __destruct()
{
$close=mysql_close($this->_con);
if($close)
{print "connection closed";}
else {die(mysql_error());}
}
}
$db=new db_access("127.0.0.1","root","","fabeeno");
//var_dump($db);
Try like
echo "<p style='color:orange;background-color:red;'>record number: ".$rec_num. "</p><br>";
You have to end ' single quote after the background-color style.
try
echo "<div style='background-color:red;'><p style='color:orange'>"."record number: ".$rec_num. "</p></div>"."<br>";
Or you can ajust only the print lines with styles
print "<p style='color: red;border: 1px solid black;height:20px;box-shadow: 1px 1px 7px;'>"."message!: ".$db_field['TEXTAREA1'] . "<br>";
after this i got an fancy box around the text =) i love boxes in many ways
so no echo where placed only print {} function
Many thanx and good luck
ps
['textarea1'] <-- is assigned to database so it reads only that table
So i have this code that whenever an IP is ping able or up it'll choose the green line to appear on my screen and in reverse the red line. So what I am trying to do instead if the Round Trip Time of that IP is < 200 then it's green and when it's > 250 it's red . How can i do that?
Anyone help me. Thank you.
<?php
$page = $_SERVER['PHP_SELF'];
$sec = 5;
function pingAddress($TEST) {
$pingresult = exec("ping -c 1 $TEST", $output, $result);
if ($result == 0) {
echo "Ping successful!";
echo "<pre>Your ping: $TEST</pre>";
echo "<hr color = \"green\" width = 40%> GOOD";
} else {
echo "Ping unsuccessful!";
echo "<pre>Your ping: $TEST</pre>";
echo "<hr color = \"red\" width = 40%> BAD";
}
}
pingAddress("66.147.244.228");
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
</body>
</html>
The exec function is ok to use, but you should parse the contents of the output argument, after declaring it first as an array.
Even if you added -c 1 to only issue one ping, this is the recommended way of using exec.
define('RETRIES', 1);
define('PING_PATH', '/usr/bin/ping');
function pingAddress($IP)
{
$output = array();
exec(PING_PATH . " -c " . RETRIES . " $IP", $output);
// generic way, even for one line. You can also do -c 4,
// and preg_match will pick the first meaningful result.
$output_string = implode("; ", $output);
/// adapt the regular expression to the actual format of your implementation of ping
if (preg_match('/ time=\s+(\d+)ms/', $output_string, $bits)) {
$rt_time = (int)$bits[1];
if ($rt_time < 200) {
// green business
}
else if ($rt_time > 250) {
// red business
}
else {
// default handler business (or not...)
}
}
else {
echo "Hum, I didn't manage to parse the output of the ping command.", PHP_EOL;
}
}
I have this simple php script which outputs a string every second.
<?php
$i = 1;
while(1)
{
exec("cls"); //<- Does not work
echo "test_".$i."\n";
sleep(1);
$i++;
}
I execute the script in the command shell on windows (php myscript.php) and try to clear the command shell before every cycle. But I don't get it to work. Any ideas?
How about this?
<?php
$i = 1;
echo str_repeat("\n", 300); // Clears buffer history, only executes once
while(1)
{
echo "test_".$i."\r"; // Now uses carriage return instead of new line
sleep(1);
$i++;
}
the str_repeat() function executes outside of the while loop, and instead of ending each echo with a new line, it moves the pointer back to the existing line, and writes over the top of it.
can you check this solution
$i = 1;
echo PHP_OS;
while(1)
{
if(PHP_OS=="Linux")
{
system('clear');
}
else
system('cls');
echo "test_".$i."\n";
sleep(1);
$i++;
}
Apparently, you have to store the output of the variable and then print it to have it clear the screen successfully:
$clear = exec("cls");
print($clear);
All together:
<?php
$i = 1;
while(1)
{
$clear = exec("cls");
print($clear);
echo "test_".$i."\n";
sleep(1);
$i++;
}
I tested it on Linux with clear instead of cls (the equivalent command) and it worked fine.
Duplicate of ➝ this question
Under Windows, no such thing as
#exec('cls');
Sorry! All you could possibly do is hunt for an executable (not the cmd built-in command) like here...
You have to print the output to the terminal:
<?php
$i = 1;
while(1)
{
exec("cls", $clearOutput);
foreach($clearOutput as $cleanLine)
{
echo $cleanLine;
}
echo "test_".$i."\n";
sleep(1);
$i++;
}
if it is linux server use following command (clear)
if it is window server use cls
i hope it will work
$i = 1;
while(1)
{
exec("clear"); //<- This will work
echo "test_".$i."\n";
sleep(1);
$i++;
}
second solution
<?php
$i = 1;
echo PHP_OS;
while(1)
{
if(PHP_OS=="Linux")
{
$clear = exec("clear");
print($clear);
}
else
exec("cls");
echo "test_".$i."\n";
sleep(1);
$i++;
}
This one worked for me, tested also.
<?php
include("config.inc.php");
header("Cache-Control: no-cache, must-revalidate");
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
echo "<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.0//EN\"\"http://www.wapforum.org/DTD/xhtml-mobile10.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head>";
echo "<title>$stitle</title>";
echo "<link rel=\"StyleSheet\" type=\"text/css\" href=\"theme/theme.css\" />";
echo "</head>";
?>
<head>
<META HTTP-EQUIV="Refresh" Content="10">
</head>
<?php
echo "<body>";
echo "<p align=\"center\">";
echo "Live Score:
---
";
$feed = "http://www.scorespro.com/rss/live-soccer.xml";
$fp = #fopen($feed,"r");
while(!feof($fp)) $raw .= #fgets($fp, 4096);
fclose($fp);
if( preg_match("/<item>(.*)<\/item>/", $raw, $rawitems ) ) {
$items = explode("<item>", $rawitems[0]);
$p = 5;
if ($npage == "")$npage = "1";
$countfile= count($items);
$countfile=$countfile-2;
$first=($npage*$p)-$p;
$npages = ceil($countfile / $p);
$items = array_reverse($items);
$next_arrays=($first+($p-1));
if($next_arrays>$countfile)$next_arrays=$countfile;
for ($i=($first); $i <= $next_arrays; $i++) {
preg_match("<title>(.*)</title>",$items[$i+1], $title );
preg_match("<link>(.*)</link>",$items[$i+1], $url );
preg_match("<description>(.*)</description>",$items[$i+1], $description);
$title[1] = str_replace("'", "", $title[1]);
$title[1] = str_replace("& ", "&", $title[1]);
echo $title[1].' ';
}
}
// if ($npage <= $npages and $npage>1) $gline_rew = '[url="'.$_SERVER["]Prev[/url] ';
// if ($npages > 1 and $npage<$npages) $gline_next = ' [url="'.$_SERVER["]Next[/url]';
// echo "
// ---
// Page {$npage} of {$npages}
// ".$gline_rew.$gline_next."
// ---
// ";
echo "</small>\n";
echo "</p>";
echo "</body>";
echo "</html>";
?>
<?php
public_static_function getInstance() {
static $instance;
$class = $title;
if ( ! $instance instanceof $class) {
$instance = new $class;
}
return $instance;
}
Hello!
I am making a live-score site using PHP.
But when I use this code and run it, I get a blank page.
I hope that I will find the mistake by myself but for now I can`t.
I would be happy if someone knows where the mistake is and help me.
Thank you in advance !!!
This is wrong
public_static_function getInstance() {
It should be
public static function getInstance() {
Also add error_reporting(E_ALL) when you have blank page(Which means you have errors)
Even when fixed, this will not work:
public static function getInstance() {
static $instance;
$class = $title;
...
}
Furthermore we have simpleXML_load_str() for handling RSS (or other XML) strings.
I think what you want is a decent way to handle an RSS feed.
Have a look at this post about RSS feeds
Good luck!
i have a for loop that will loop through a set of page in php.
for ($count=0;$count<=$curr;$count=$count + 10)
{
if ($find==1) {
$result = "file.php?count=$count";
}else {
$result = "file2.php?count=$count";
}
$match = file_get_contents($result);
$nc=$count+10;
if (preg_match("/\b$file\b/i", $match)) {
print "found in $count";
} else {
print "not found in $count";
}
}
the problem is that the result is displayed after the last page is executed, since it will loop through 500 pages which takes more time. so how can i make this code display the print result as it is executed each cycle,
while(something)
{
// do something
echo "Hi";
flush();
}
Using the flush() function will output everything to the browser that has been sent so far (sent as in echo, print or other similar functions.).