Jump to content

Recommended Posts

Hello, I am pulling data from a site

I want to give color according to the value that I want to do, is this possible?

I am doing it with the code below but the result is always green

 


$url = "simple";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
curl_close($curl);

$dom = new DOMDocument();
@$dom->loadHTML($data);
$xpath = new DOMXPath($dom);


$statu1 = $xpath->query('//*[@id="page-wrapper"]/div/div[6]/div/h4[7]/span');

foreach ($statu1 as $status1) 

if ($statu1 = "Online") {
    echo "<font color = green>$status1->nodeValue</font><br>";
} elseif ($statu1 = "Offline") {
    echo "<font color = red>$status1->nodeValue</font><br>";
} 

 

if ($statu1 = "Online") {
    echo "<font color = green>$status1->nodeValue</font><br>";
} elseif ($statu1 = "Offline") {
    echo "<font color = red>$status1->nodeValue</font><br>";
}

One = does an assignment, which means the above code actually works like

$statu1 = "Online";
if ($statu1) {
    echo "<font color = green>$status1->nodeValue</font><br>";

So naturally, every status will be green.

Two ==s does equality comparison. (Three ===s is if you want to be pedantic about what it means to be "equal".)

if ($statu1 == "Online") {
    echo "<font color = green>$status1->nodeValue</font><br>";
} elseif ($statu1 == "Offline") {
    echo "<font color = red>$status1->nodeValue</font><br>";
} 

 

That aside, this is very outdated HTML 4-style markup. You should switch to <span>s and CSS.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.