0

I'm fairly new to PHP and I'm trying to make an array and use the elements of that array in my PHP Website by using $_GET.

I get my array like this:

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

And these here work fine:

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

This one however doesn't work

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

...

function getFeed($url){

...

Can I not assign an array value like that?

URL Example

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

Full Code Here:

<!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>
  • 2
    `var_dump($values);` Print the array,View the existing elements in the array. This is not recommended. It is easy to trigger null pointer error. It is recommended to use `isset ()` judgment to make the program powerful – Davis Jan 25 '21 at 08:57
  • This should work, if there are 3 items in the array... but the $url is not the "url" in the array, that would be $newsSource[0][''url"] – Gert B. Jan 25 '21 at 08:59
  • 1
    and if there is no `id` parameter in the querystring/GET request what happens? – Professor Abronsius Jan 25 '21 at 09:03
  • @ProfessorAbronsius that is not the question :-) there could be code above that checks the querystring... – Gert B. Jan 25 '21 at 09:05
  • @ProfessorAbronsius Without using `id` nothing worked anymore :/ – Roman_CrossIT Jan 25 '21 at 09:10
  • @Davis I `tried var_dump` and it showed me my 4 strings, and they all looked correct. – Roman_CrossIT Jan 25 '21 at 09:16
  • @Roman_CrossIT ~ yep - that was more or less the point I was leading you to. You should have some sanity checks in place to test if the required `id` parameter is available in the GET array - ie: `isset( $_GET['id'] )` – Professor Abronsius Jan 25 '21 at 09:25

1 Answers1

0

After updating the problem, I found the problem.

In methods, variables need to be passed in!

 function getFreshContent($values) {
    // ...
Davis
  • 96
  • 5
  • I inserted the variable like you did, but it still can't get the RSS feed somehow :/ – Roman_CrossIT Jan 25 '21 at 09:21
  • @Roman_CrossIT This is another question. Is it convenient to display XML files – Davis Jan 25 '21 at 09:40
  • @David I could solve the Problem by making $values a Global like: $GLOBALS['values'] = $values; then adding the new global to getFreshContent! – Roman_CrossIT Jan 25 '21 at 09:49
  • @Roman_CrossIT Yes, but the global variable approach is not used in all scenarios. It is very common to pass parameters between functions – Davis Jan 25 '21 at 10:07
  • @Roman_CrossIT [php params in function](https://doc.bccnsoft.com/docs/php-docs-7-en/functions.arguments.html) – Davis Jan 25 '21 at 10:18