在PHP中使用数组

Roman_CrossIT

我对PHP还是很陌生,我想通过$ _GET在我的PHP网站中创建一个数组并使用该数组的元素。

我得到这样的数组:

<?php $values = explode(",", $_GET["id"]); ?>

这些在这里工作正常:

<img src="<?php echo $values[0]; ?>">
<img src="<?php echo $values[1]; ?>">

但是这不起作用

$newsSource = array(
  array(
    "title" => "COMPANY",
    "url" => $values[2]
  )
);

...

function getFeed($url){

...

我不能分配这样的数组值吗?

URL示例

https://DOMAIN/PHPFILE.php?id = IMAGE.jpg,IMAGE.png,RSS_FEED_WEBSITE,一个

完整代码在这里:

<!DOCTYPE html>
<html lang="en">
    <head>
       
    </head>
    <body>
        <?php $values = explode(",", $_GET["id"]); 
        // 0 = Logo
        // 1 = News Logo
        // 2 = RSS Feed
        // 3 = Page
        ?>
        <div class="container">
            <div class="logo">
                <img src="<?php echo $values[0]; ?>">
            </div>
            <div class="logo">
                <img src="<?php echo $values[1]; ?>">
            </div>
                
                <?php
                    
                    function getContent() {

                        $file = "./feed-cache.txt";
                        $current_time = time();
                        $file_time = filemtime($file);

                        if(file_exists($file) && ($current_time < $file_time)) {
                            return file_get_contents($file);
                        }
                        else {
                            $content = getFreshContent();
                            file_put_contents($file, $content);
                            return $content;
                        }
                    }

                    function getFreshContent() {
                        $html = "";
                        $newsSource = array(
                            array(
                                "title" => "COMPANY",
                                "url" => $values[2]
                            )
                        );
                        function getFeed($url){
                            $html = "";
                            $rss = simplexml_load_file($url);
                            $count = 0;        
                    
                            $page = $values[3] ?? 'one';
                            
                            if ($page == 'two') {
                            $counter = 2;
                            } else if ($page == 'three') {
                                $counter = 4;
                            } else {
                                $counter = 0;
                            }
                            
                            $itemcount = count($rss->channel->item);
                            
                            for ($i = $counter; $i <= $counter+1; $i++) {
                                $curr_item = ($rss->channel->item[$i]);

                                // split description from img and text
                                $content = ($curr_item->description);
                                preg_match('/(<img[^>]+>)/i', $content, $matches);

                                $item->image = $matches[0];                            
                                $item->description = str_replace($matches[0],"",$content);

                                $html .= '<div class="entry"><h3>'.htmlspecialchars($curr_item->title).'</h3>';
                                $html .= '<div class="entry-image">'.($item->image).'</div>';
                                $html .= '<div class="entry-text">'.($item->description).'</div>';
                                $html .= '</div>';

                            }
                            
                            return $html;
                       }

                        foreach($newsSource as $source) {
                            //$html .= '<h2>'.$source["title"].'</h2>';
                            $html .= getFeed($source["url"]);
                        }
                        return $html;
                    }

                    print getContent();
                    
                ?>
        </div>
    </body>
</html>
戴维斯

更新问题后,我发现了问题。

在方法中,需要传递变量!

 function getFreshContent($values) {
    // ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章