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>