<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>link rotator &#8211; ScriptNet Solutions Blog</title>
	<atom:link href="https://blog.scriptnet.net/tag/link-rotator/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.scriptnet.net</link>
	<description>Official News, Product Updates, Status and more</description>
	<lastBuildDate>Tue, 09 Jul 2024 06:13:47 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.5</generator>
	<item>
		<title>Create an efficient Banner Rotator in Json and PHP</title>
		<link>https://blog.scriptnet.net/create-an-efficient-banner-rotator-in-json-and-php/</link>
		
		<dc:creator><![CDATA[Team ScriptNet]]></dc:creator>
		<pubDate>Mon, 08 Jul 2024 16:01:06 +0000</pubDate>
				<category><![CDATA[Life as a Developer]]></category>
		<category><![CDATA[banner rotator]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[link rotator]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php code]]></category>
		<category><![CDATA[scriptnet]]></category>
		<guid isPermaLink="false">https://blog.scriptnet.net/?p=235</guid>

					<description><![CDATA[Over the years, every Developer has sooner or later faced this &#8220;dilemma&#8221;. Insert a Banner, Text or Link in a predefined position, randomly; the purpose is to show a different Image or Link each time a page loads. A common called &#8220;Banner Rotator&#8221; to be precise. Technically, there are many ways to create this system. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><span id="more-235"></span></p>
<p>Over the years, every Developer has sooner or later faced this &#8220;dilemma&#8221;.<br />
<strong>Insert a Banner, Text or Link in a predefined position, randomly</strong>; the purpose is to show a different Image or Link each time a page loads. A common called &#8220;<strong>Banner Rotator</strong>&#8221; to be precise.</p>
<p>Technically, there are many ways to create this system.<br />
Today, we want to share a <strong>Simple and Effective system</strong>, to create this banner rotator, using 1 file in JSON, and few lines of PHP.<br />
<!--more--></p>
<p>The structure is very simple and accessible to everyone:</p>
<ul>
<li> The <strong><a href="https://www.json.org/json-en.html" rel="noopener" target="_blank">JSON</a></strong> file will contain the Data you want to show (images, texts and links)</li>
<li> The <strong><a href="https://www.php.net/" rel="noopener" target="_blank">PHP code</a></strong> will import the data on your page</li>
<li> The <strong><a href="https://www.w3schools.com/tags/tag_div.ASP" rel="noopener" target="_blank">DIV code</a></strong> will show the data on your page</li>
</ul>
<p>This system is solid, non-invasive, simple to compile and can be implemented on any PHP page in a few minutes.<br />
JSON is capable of containing even discrete quantities of data without any particular problems, so you can add new fields and expand the list of data to be displayed.<br />
Let&#8217;s start with the structure of the .json file which we will call &#8220;data.json&#8221;, but you can renamed as your needs.</p>
<p><code></p>
<pre>
{
  "banners": [
    {
      "text": "Your Text 1",
      "image": "image1.jpg",
      "link": "https://website1.com"
    },
    {
      "text": "Your Text 2",
      "image": "image2.jpg",
      "link": "https://website2.com"
    },
    {
      "text": "Your Text 3",
      "image": "image3.jpg",
      "link": "https://website3.com"
    }
  ]
}
</pre>
<p></code></p>
<p>Once you have compiled this file with your data with your favorite text editor, save and upload it to your web space in the position most suited to the project. Afterwards, we just need to <strong>insert a small PHP Code</strong>, which reads the data present in the Json file we just created. You can insert this code on the page where you want to display the banners, at the beginning of the code or the </head> tag.</p>
<p><code></p>
<pre>
&lt;?php
// Read the contents of the JSON file
$jsonData = file_get_contents('data.json');
$data = json_decode($jsonData, true);

// Get a random banner
$banners = $data['banners'];
$randomBanner = $banners[array_rand($banners)];
?&gt;
</pre>
<p></code></p>
<p>Now, let&#8217;s <strong>create a div</strong>, which you can insert into the page, in the position you want; remember that if you use images of a certain weight and size you may need to add CSS to make the images compatible with mobile devices.</p>
<p><code></p>
<pre>
<div id="banner">
    &lt;a href="&lt;?php echo $randomBanner['link']; ?&gt;" rel="noopener" target="_blank" title="">
        &lt;p&gt;&lt;?php echo $randomBanner['text']; ?&gt;&lt;/p&gt;&lt;br&gt;
        &lt;img src="&lt;?php echo $randomBanner['image']; ?&gt;" alt=""&gt;
    &lt;/a&gt;
</div>
</pre>
<p></code></p>
<p>In the code, the div indicates the position; the php code displays the data of the fields present in the .json file.<br />
Easy and Fast.</p>
<p>Life as a Developer can sometimes be simpler!</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
