简体   繁体   中英

PHP - echo doesn't seem to work right

I have this function that gets a table from another site by finding each row of that table and, providing that row isn't a duplicate, echos it to my site. I want to add my own row to the bottom though so I thought it would be as simple as just echoing

<tr><td>TEXT</td></tr>

as you can see below. But when I load the page, this row isn't added. Anyone know what the cause may be?

Here is the website if that helps.

function getStats(){
$page = file_get_html(getPageURL());
$rows = array();

echo "<table id=statsTable>"; 
foreach($page->find('html/body/div/div[1]/center/table/tbody/tr[1]/td/table/tbody/tr/td[2]/table/tbody/tr/') as $key=>$element) {
    if(!in_array($element, $rows)){
        $rows[$key]=$element;
        echo $rows[$key-1];
        }
}
echo "<tr><td>Viewing old updates will be added soon</td></tr></table>";
}   

Main problem was in key offset, when you work with arrays it's better to use keys in this case. So in order to that I've changed the in_array to array_key_exists , because we want to check if key exists, but if you want to work with element you have to know its key.

function getStats(){
    $page = file_get_html(getPageURL());
    $rows = array();

    echo "<table id=statsTable>";
    foreach($page->find('html/body/div/div[1]/center/table/tbody/tr[1]/td/table/tbody/tr/td[2]/table/tbody/tr/') as $key => $element) {
        if(!array_key_exists($key, $rows)){
            $rows[$key] = $element;
            /* Echo only when it exists */
            if(array_key_exists($key-1, $rows)){
                echo $rows[$key-1];
            }
        }
    }
    echo "<tr><td>Viewing old updates will be added soon</td></tr></table>";
}
?>
<?php
    include "getData.php";
    include "simple_html_dom.php";
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>KSA Flight Tracker</title>
        <link rel="stylesheet" type="text/css" href="KSAStyle.css"/>
    </head>
    <body>
        <div id="page">
            <div id="leftPanel">
                <p id="missionsHeader">Active Missions</p>
                <?php getList(); ?>
            </div>
            <div id="mainPanel">
                <p id="infoHeader">
                    <?php getTitle(); ?>
                </p>
                <div id="info">
                    <center>
                        <?php
                            getInfo();
                            getImage();
                            getMap();
                            getStats();
                        ?>
                    </center>
                </div>
            </div>
        </div>
    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM