How would I join these two strings and create random link based on the entry list.
// Add a link and the associated image //
$adlink1="http://www.****.com/sale.php";
$adlinkpic1="http://www.***-cdn.com/blogAssets/ad/1.jpg";
$adlink2="http://www.*****.com/sale.php";
$adlinkpic2="http://www.**-cdn.com/blogAssets/ad/2.jpg";
$adlink3="http://www.**.com/product.php?prodref=564_white&ref=AddSphere";
$adlinkpic3="http://www.**-cdn.com/blogAssets/ad/3.jpg";
$adlink4="http://www.**.com/wedding-boutique.php";
$adlinkpic4="http://www.**-cdn.com/blogAssets/ad/4.jpg";
$adlink5="http://www.**.com/made-to-measure-service.php";
$adlinkpic5="http://www.**-cdn.com/blogAssets/ad/5.jpg";
// SHOW ONE AD LINK
srand ((double) microtime() * 1000000);
$adlink[] + $adlinkpic[] = rand(0,count($quotes)-1);
echo "<a href='$adlink'><img src='$adlinkpic' />";
// SHOW TWO AD LINKS /cannot be same
// code here
It is easy to put all of your links in an (associative) array then use array functions to manipulate them:
<?php
$ad = array(
array(
"url" => "http://www.****.com/sale.",
"img" => "http://www.***-cdn.com/blogAssets/ad/1.jpg"
),
array(
"url" => "http://www.*****.com/sale.",
"img" => "http://www.**-cdn.com/blogAssets/ad/2.jpg"
),
array(
"url" => "http://www.**.com/product.php",
"img" => "http://www.**-cdn.com/blogAssets/ad/3.jpg"
),
array(
"url" => "http://www.**.com/wedding-boutique.",
"img" => "http://www.**-cdn.com/blogAssets/ad/4.jpg"
),
array(
"url" => "http://www.**.com/made-to-measure-service.",
"img" => "http://www.**-cdn.com/blogAssets/ad/5.jpg"
)
// more ads
);
$id = array_rand($ad); // choose a random index from the array
echo "<img src=\"{$ad[$id]['img']}\" />\n";
unset($ad[$id]); // remove the chosen one so that it is not displayed on next pass
$id = array_rand($ad);
echo "<img src=\"{$ad[$id]['img']}\" />\n";
unset($ad[$id]);
I'd be tempted to do it something like this:
<?php
// Add a link and the associated image //
$adlink1="http://www.****.com/sale.php";
$adlinkpic1="http://www.***-cdn.com/blogAssets/ad/1.jpg";
$adlink2="http://www.*****.com/sale.php";
$adlinkpic2="http://www.**-cdn.com/blogAssets/ad/2.jpg";
$adlink3="http://www.**.com/product.php?prodref=564_white&ref=AddSphere";
$adlinkpic3="http://www.**-cdn.com/blogAssets/ad/3.jpg";
$adlink4="http://www.**.com/wedding-boutique.php";
$adlinkpic4="http://www.**-cdn.com/blogAssets/ad/4.jpg";
$adlink5="http://www.**.com/made-to-measure-service.php";
$adlinkpic5="http://www.**-cdn.com/blogAssets/ad/5.jpg";
$links = array();
$links[0]=array('link'=>$adlink1,'pic'=>$adlinkpic1);
$links[1]=array('link'=>$adlink2,'pic'=>$adlinkpic2);
$links[2]=array('link'=>$adlink3,'pic'=>$adlinkpic3);
$links[3]=array('link'=>$adlink4,'pic'=>$adlinkpic4);
$links[4]=array('link'=>$adlink5,'pic'=>$adlinkpic5);
$alreadyAdded=array();
for ($i=0;$i<2;$i++) {
$added = false;
while (!$added) {
// generate random number
$rand = mt_rand(0,4);
if (!in_array($rand,$alreadyAdded)) {
echo "<a href='".$links[$rand]['link']."'><img src='".$links[$rand]['pic']."' />";
$added = true;
$alreadyAdded[]=$rand;
}
}
}
Edit: noticed you wanted more than 1 outputted, updated code to reflect.
Related
I've modify this code to get data from a json file instead of xml file (my old code)
<?php
$tracking_id = 'MyID'; //This is used to track the user doing the offer. can be email, clickid, subid.. etc
$userip = $_SERVER['REMOTE_ADDR']; //We need to get the users ip, so the rss feed can display the correct offers for their country.
$user_agent = $_SERVER['HTTP_USER_AGENT']; //lets collect their user agent to pass along.
$max_offers = 5; //max number of offers to display.
$str = file_get_contents('https://www.cpalead.com/dashboard/reports/campaign_json_load_offers.php?id=296213&geoip='.$userip.'&ua='.urlencode($user_agent).'&subid='.urlencode($tracking_id));
$json = json_decode($str, true);
$opOffer = array();
$offer_cnt = 0;
foreach($json['offers'] as $offeritem)
{
$opOffer[$offer_cnt] = array("title" => array($offeritem->title), "link" => array($offeritem->link));
$offer_cnt++;
};
if (0 == count($opOffer)):
echo 'Sorry there are no offers available for your region at this time.';
else:
echo json_encode($opOffer);
endif;
?>
and it's output like this:
[{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]}]
it should be like this:
[{"title":[{"What is Your Favorite Time for McDonald's?"}],"link":[{"https:\/\/filetrkr.com\/show.php?l=0&u=31802&id=5882&tracking_id=MyId"}]}]
Link to see json output: https://www.cpalead.com/dashboard/reports/campaign_json_load_offers.php?id=296213
For the line:
$opOffer[$offer_cnt] = array("title" => array($offeritem->title), "link" => array($offeritem->link));
Shouldn't it be either:
$opOffer[$offer_cnt] = array("title" => $offeritem->title, "link" => $offeritem->link);
or:
$opOffer[$offer_cnt] = array("title" => $offeritem['title'], "link" => $offeritem['link']);
Also, just after the line:
$json = json_decode($str, true);
Add this line and post the results:
echo "<p>JSON: <pre>".print_r($json,true)."</pre></p>";
That last bit to verify you are receiving the JSON you think you should be getting.
I just changed:
$opOffer[$offer_cnt] = array("title" => array($offeritem->title), "link" => array($offeritem->link));
to:
$opOffer[$offer_cnt] = array("title" => array($offeritem["title"]), "link" => array($offeritem["link"]));
And now give me what you want on the JSON:
[{"title":["Amplificador de Bater\u00eda"],"link":["http:\/\/gtrcking.com\/offer.php?id=744798&pub=296213&subid=MyID"]},{"title":["\u0645\u0628\u0627\u0631\u0627\u0629 \u0645\u0630\u0647\u0644\u0629!"],"link":["http:\/\/gtrcking.com\/offer.php?id=550635&pub=296213&subid=MyID"]},....
heres what you want. give that a go.
foreach($json['offers'] as $offeritem)
{
$opOffer[$offer_cnt] = array("title" => array($offeritem['title']), "link" => array($offeritem['link']));
$offer_cnt++;
if ($offer_cnt == $max_offers) break;
};
I have a scenario :
There are some divs are displaying the based on the following loop:
<?php foreach($posts->content as $entry) { ?>
<div><a class="popup-with-zoom-anim wiplay" id="<?=$entry->id?>"
href="#small-dialog" data-detail-id ="<?=$entry->id?>"
data-stream="******">content here</a></div>
<?php } ?>
In the a href "data-stream" i want to pass some youtube url so the each div will show random url based on th for loop.
I have the following code tried.But not working.How will we call this function inside the loop?
$assoc_array = array( "url" => "https://www.youtube.com
/watch?v=A7XdOyZIkko",
"url" => "https://www.youtube.com/watch?v=dMH0bHeiRNg
",
"url" => "https://www.youtube.com/watch?v=xEs59zTXu7s",
"url" => "https://www.youtube.com/watch?v=tlDAgZO2ZDM
");
function shuffle_assoc_array(&$array) {
if (!is_array($array)) return $array;
$keys = array_keys($array);
shuffle($keys);
$random = array();
foreach ($keys as $key)
$random[$key] = $array[$key];
$array = $random; return TRUE;
}
shuffle_assoc_array($assoc_array);
How can i pass this youtube url randomly to the for loop?
As Ima told, you must have to cahnge your array format and than try with below code.
<?php
$assoc_array = array( "https://www.youtube.com/watch?v=A7XdOyZIkko", "https://www.youtube.com/watch?v=dMH0bHeiRNg", "https://www.youtube.com/watch?v=xEs59zTXu7s", "https://www.youtube.com/watch?v=tlDAgZO2ZDM");
shuffle($assoc_array);
for($i=0;$i<count($assoc_array);$i++){
echo '<div><a class="popup-with-zoom-anim wiplay" id="'.$i.'" href="#small-dialog" data-detail-id ="'.$i.'" data-stream="'.$assoc_array[$i].'">'.$assoc_array[$i].'</a></div>';
}
?>
THis array will give you only one value
array(
"url" => "https://www.youtube.com
/watch?v=A7XdOyZIkko",
"url" => "https://www.youtube.com/watch?v=dMH0bHeiRNg",
"url" => "https://www.youtube.com/watch?v=xEs59zTXu7s",
"url" => "https://www.youtube.com/watch?v=tlDAgZO2ZDM
");
This is equivalent to
array(
"url" => "https://www.youtube.com/watch?v=tlDAgZO2ZDM
");
Try something like this
array(
"https://www.youtube.com/watch?v=A7XdOyZIkko",
"https://www.youtube.com/watch?v=dMH0bHeiRNg",
"https://www.youtube.com/watch?v=xEs59zTXu7s",
"https://www.youtube.com/watch?v=tlDAgZO2ZDM"
);
I have a class which is working fine but I need to do multiple results.
Here is the current code:
$url = 'http://mydomain.com';
$keyword = 'somekeyword';
$RankChecker=new RankChecker(1,5);
$result=$RankChecker->find($url,$keyword);
if ($result!==false) {
echo "Your website is found at page number ".$result["page"].".";
}
What is the best way to get it to read multiple url's / keyword's ?
Put the URLs and keywords into an array and loop through it:
$urls = array(
'http://mydomain.com' => 'somekeyword',
'http://myotherdomain.com' => 'someotherkeyword'
);
$RankChecker=new RankChecker(1,5);
foreach($urls as $url => $keyword) {
$result=$RankChecker->find($url,$keyword);
if ($result!==false) {
echo "Website " . $url . " is found at page number ".$result["page"].".";
}
}
Using an array :
<?php
$websites[1] = array('url' => 'http://mydomain.com', 'keyword' => 'somekeyword');
$websites[2] = array('url' => 'http://mydomain2.com', 'keyword' => 'somekeyword2');
$websites[3] = array('url' => 'http://mydomain3.com', 'keyword' => 'somekeyword3');
// etc...
foreach ($websites as $val)
{
$RankChecker=new RankChecker(1,5);
$result=$RankChecker->find($val['url'], $val['keyword']);
if ($result!==false) {
echo "Your website is found at page number ".$result["page"].".";
}
}
Can someone please point me in the correct direction to convert my switch code from currently being listed like below to being drawn from a CSV file instead:
$video = (isset($_GET['video']) ? $_GET['video'] : null);
if($video) {
switch($video) {
case "apple":
$Heading ='Apple Heading';
$Videonum ='1';
$Content ='<h2>Apple Sub Heading</h2>
<p>Apple content</p>';
$SideContent ='Apple side content';
break;
I will end up with lots of cases and it'll be easier to manage from a .csv file - thank you
I think you need a two-dimensional array with the identifier ('apple', …) as the key for the inner arrays. By parsing a CSV-file you will get an array with multiple rows, but you need to search for the row, that contains the required data. Maybe you also can save PHP files containing the necessary data-arrays or even use a database (which is probably most common for such cases).
Target arrays as I would use it:
$data = array(
'apple' => array(
'heading' => 'Apple Heading',
'video_num' => 1,
'content' => '<h2>Apple Sub Heading</h2>
<p>Apple content</p>',
'side_content' => 'Apple side content',
),
/* more manufacturer sub-arrays */
);
In this first case you could access the whole data by just reading from the array:
if( !empty( $_GET['video'] ) && isset( $data[$_GET['video']] ) )
{
var_dump(
$data[$_GET['video']]['heading'],
$data[$_GET['video']]['content']
);
}
else
{
echo '<p class="error">No video specified or "' . $_GET['video'] . '" is not available.</p>';
}
FYI; Array as retrieved from a CSV-file:
$data = array(
1 => array(
'manufacturer' => 'Apple',
'heading' => 'Apple Heading',
'video_num' => 1,
'content' => '<h2>Apple Sub Heading</h2>
<p>Apple content</p>',
'side_content' => 'Apple side content',
),
/* more rows */
);
Read your csv file
$data = array();
$fp = fopen('manufacturer.csv', 'r');
while (!feof($fp)) {
$line = explode(';',fgets($fp));
$data[$line[0]]['heading'] = $line[1];
$data[$line[0]]['video'] = $line[2];
$data[$line[0]]['content'] = $line[3];
$data[$line[0]]['side'] = $line[4];
}
fclose($fp);
Your csv looks like
apple;Apple Heading;1;<h2>Apple Sub Heading</h2><p>Apple content</p>;Apple side content
microsoft;MS Heading;1;<h2>MS Sub Heading</h2><p>MS content</p>;MS side content
...
Then acces your content with the manufacturer name
if(isset($data[$_GET['video']] && !empty($_GET['video']))){
$Heading = $data[$_GET['video']]['heading'];
$Videonum = $data[$_GET['video']]['video'];
$Content = $data[$_GET['video']]['content'];
$SideContent = $data[$_GET['video']]['side'];
}
I have a database field called "servers"
This field has a link in each row, this field content:
> http://www.rapidshare.com/download1
> http://www.rapidshare.com/download2
> http://www.rapidshare.com/download3
> http://www.megaupload.com/download1
> http://www.megaupload.com/download2
> http://www.megaupload.com/download3
> http://www.fileserve.com/download1
> http://www.fileserve.com/download2
> http://www.fileserve.com/download3
I want to create an array with all the server names, and create more array with links inside.
That's how it should be:
$servers = array(
'rapidshare' => array(
'link1' => 'http://www.rapidshare.com/download1',
'link2' => 'http://www.rapidshare.com/download2',
'link3' => 'http://www.rapidshare.com/download3'),
'megaupload' => array(
'link1' => 'http://www.megaupload.com/download1',
'link2' => 'http://www.megaupload.com/download2',
'link3' => 'http://www.megaupload.com/download3'),
'fileserve' => array(
'link1' => 'http://www.megaupload.com/download1',
'link2' => 'http://www.megaupload.com/download2',
'link3' => 'http://www.megaupload.com/download3')
);
This will do the trick: (make sure that domain is actually showing up in $domain variable though because it might be $matches[1]... I can't remember)
$newStructure = array();
foreach($links as $link) {
preg_match("/www\.([^\.])\.com/",$link,$matches);
$domain = $matches[0];
$currentLength = count($newStructure[$domain]);
if($currentLength) {
$newStructure[$domain]['link'.($currentLength+1)] = $link;
} else {
$newStructure[$domain] = array('link1'=>$link);
}
}
$server = array(
'http://www.rapidshare.com/download1',
'http://www.rapidshare.com/download2',
'http://www.rapidshare.com/download3',
'http://www.megaupload.com/download1',
'http://www.megaupload.com/download2',
'http://www.megaupload.com/download3',
'http://www.fileserve.com/download1',
'http://www.fileserve.com/download2',
'http://www.fileserve.com/download3'
);
$match = array();
$myarray = array();
foreach($server as $v) {
// grab server name
preg_match('/\.(.+)\./', $v, $match);
$serverName = $match[1];
// initialize new array if its the first link of that particular server
if (!isset($myarray[$serverName])) {
$myarray[$serverName] = array();
}
// count server array to check how many links are there, and make next link key
$linkKey = 'link' . (count($myarray[$serverName]) + 1);
// store value
$myarray[$serverName][$linkKey] = $v;
}
print_r($myarray);
Hey maybe this will help you. But i dont see the purpose of those names of the keys (link1,link2 etc..). This wont work on pagination thou.