Random Image as link associated with image - scripting

I'm looking for a script that will call a random image linked to its corresponding site on pageload. Anyone know a javascript or php way to do this?

<?php
$random = array(
array('image' => 'http://example.com/image1.jpg', 'url' => 'http://example1.com/'),
array('image' => 'http://example.com/image2.jpg', 'url' => 'http://example2.com/'),
array('image' => 'http://example.com/image3.jpg', 'url' => 'http://example3.com/'),
array('image' => 'http://example.com/image4.jpg', 'url' => 'http://example4.com/'),
);
$current = rand(0, count($random) - 1);
print "<img src=\"" . $random[$current]['image'] . "\" alt=\"\" />\n";
?>
Quick and easy. Might not be the best way of doing it - I'd personally hook it into a database instead of a static array - but it'll have you up and running in seconds.

Related

Yii2 Nav widget doesnt support routes with baseUrl

1) I have my Yii2 in subfilder, so all my link starts from Yii/, like http:/localhost/Yii/settings/usertype-activitytype/type/3
2) request component config
'request' => [
'cookieValidationKey' => 'somecookiekey',
'baseUrl' => '/Yii',
],
3) Trying to build Menu, current route is http:/localhost/Yii/settings/usertype-activitytype/type/1 the 1 is id in route, and I should specify current route as Yii::$app->request->pathInfo for Nav widget
Attempt 1 - no bracets as string NOT FIND ACTIVE ELEMENT
Nav::widget([
'items' => array_map(function($userType) {
return [
'label' => $userType->name,
'url' => Url::current(['id' => $userType->id]),
];
}, $userTypes),
'route' => Yii::$app->request->pathInfo,
'options' => ['class' =>'nav-pills'],
]);
Attempt 2 - use bracets as route NOT FIND ACTIVE ELEMENT
Nav::widget([
'items' => array_map(function($userType) {
return [
'label' => $userType->name,
'url' => [Url::current(['id' => $userType->id])]
];
}, $userTypes),
'route' => Yii::$app->request->pathInfo,
'options' => ['class' =>'nav-pills'],
]);
Attempt 3 - remove baseUrl from generated route FIND ACTIVE ELEMENT !!!
Nav::widget([
'items' => array_map(function($userType) {
return [
'label' => $userType->name,
'url' => [str_replace('Yii/', '', Url::current(['id' => $userType->id]))]
];
}, $userTypes),
'route' => Yii::$app->request->pathInfo,
'options' => ['class' =>'nav-pills'],
]);
So you should notice I have to use dirty hack to force Nav work with generated Url, it seems very unconvient.
The question is - is there are ways to force NAV widget to recognize current active item ?
If you want buil an url based on your param you should use Url::to()as
'url' => Url::to(your-controller/your-view', ['id' => $userType->id]),
so accessing the view type for controller usertype-activitytype
'url' => Url::to('usertype-activitytype/type', ['id' => $userType->id]),
current Creates a URL by using the current route and the GET parameters.
and remember that
By Design UrlManager always prepends base URL to the generated URLs.
By default base URL is determined based on the location of entry
script. If you want to customize this, you should configure the
baseUrl property of UrlManager.
Looking to your comment the url is correcly formed .. then if you need http:/localhost/Yii instead of Yii then you can:
don't set so the localhost ... url is atomatically created
OR add the proper base url configureation as http:/localhost/Yii/ in config /main and remember that you can use main.php and main-local.php for manage different congiguration

New page from HTML in kartik\mpdf (Yii2)

I have a problem. How to add a new page from HTML to PDF?
Immediately I will say that I do not know why, but solutions such as:
<pagebreak />
or
h1 {page-break-before: always}
not working.
My PHP Code:
$pdf = new Pdf([
'mode' => Pdf::MODE_UTF8,
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_FILE,
'filename' => $path,
'content' => $content,
'cssInline' => '.font_next{font-family:DoodlePen}table{border-collapse:collapse;width:100%}td{border:1px solid #000}',
]);
return $pdf->render();
Does anyone have this experience and can help you?
From the docs, looks like you can either use HTML like:
<pagebreak />
or
<tocpagebreak />
Or PHP:
$mpdf->AddPage();
$mpdf->TOCpagebreak();
But you already said you tried the html and din't work. Maybe you have a parent element with float?
OK, it works.
I use this method: https://davidwalsh.name/css-page-breaks
In HTML:
<div className="page-break"></div>
In PHP:
$pdf = new Pdf([
'mode' => Pdf::MODE_UTF8,
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_FILE,
'filename' => $path,
'content' => $content,
'cssInline' => '
#media all{
.font_next{font-family:DoodlePen}table{border-collapse:collapse;width:100%}td{border:1px solid #000}.page-break {display: none;}
}
#media print{
.page-break{display: block;page-break-before: always;}
}
',
]);
return $pdf->render();

drupal generate fpdf files

I create website based on drupal 7 with module to generate pdf - i use fpdf library.
I dont know how I can make form to send some data to node where i have php code for generate pdf.
Also i still get error message:
FPDF error: Some data has already been output, can't send PDF file (output started at Z:\xampp\htdocs\domain\includes\common.inc:2748)
where i found:
ob_flush();
when i try change it to
ob_clean()
all nodes generate pdf :/
You can try use other module which is a simple - pdf_using_mpdf
With this module you can get PDF of some node calling www.YOUR_SITE.DOMAIN/node/NID_ID/pdf - where NID_ID is the id of node what you want to render as PDF.
Or also you can use hook_menu() if you need render as PDF only some fields of your node - add your item like
$items['%node/create_pdf'] = array(
'title' => '',
'description' => '',
'page callback' => 'custom_callback_create_pdf',
'page arguments' => array(1),
'access callback' => 'node_access',
'type' => MENU_CALLBACK,
);
Define you callback like
function custom_callback_create_pdf($node) {
// Check if current node has needed field.
if (isset($node->field_my_custom_field_of_node)) {
// Render only field 'field_my_custom_field_of_node';
$view = node_view($node);
$output = render($view['field_my_custom_field_of_node']);
return pdf_using_mpdf_api($output, $node->title);
}
else {
return pdf_using_mpdf_api("<html><body>Current page do not required field.</body></html>", $node->title);
}
}
and print button for download:
$pdf_button = l(t('Download as PDF'), 'node/' . $node->nid . '/create_pdf', array(
'attributes' => array(
'target' => '_blank',
'class' => array('render_as_pdf'),
),
));
echo '<p>' . $pdf_button . '</p>';

How can convert view page into a pdf in Yii

How can convert view page into a pdf in Yii from controller.
in main.php i added this code
'ePdf' => array(
'class' => 'ext.yii-pdf.EYiiPdf',
'params' => array(
'mpdf' => array(
'librarySourcePath' => 'application.vendors.mpdf.*',
'constants' => array(
'_MPDF_TEMP_PATH' => Yii::getPathOfAlias('application.runtime'),
),
'class'=>'mpdf', // the literal class filename to be loaded from the vendors folder
),
'HTML2PDF' => array(
'librarySourcePath' => 'application.vendors.html2pdf.*',
'classFile' => 'html2pdf.class.php', // For adding to Yii::$classMap
)
),
),
Controller Code is
$mPDF1 = Yii::app()->ePdf->mpdf('', 'A5');
$mPDF1->WriteHTML($this->renderPartial('printformat', array(), true));
$mPDF1->Output();
Error : include(mpdf.php) [function.include]: failed to open stream: No such file or directory
The Error says, you have to include mpdf.php before using it.
You are using YiiPdf Bundle with html2pdf and mpdf.
First download Yii Pdf from here and include in extension as protected/extensions/yii-pdf (ext.yii-pdf as in 'class' => 'ext.yii-pdf.EYiiPdf',). This means your extension folder contains yii-pdf as directory.
Then, download mpdf from here. and place it in vendors as protected/vendors/mpdf as in
'librarySourcePath' => 'application.vendors.mpdf.*',
All explanation is here.

Magento admin: Making simple admin import interface with start-button

I am trying to make a Magento module, to enable our Magento-webshop customers to import all our products automatically. Since I'm not yet very proficient in Magento development, I run into a few stops on the way... :-)
Right now I try to make a AdminController in which the index page should simply display a text and a button to start the import process. It's the "addButton" part, that I have trouble with:
public function indexAction() {
$this->loadLayout();
$block = $this->getLayout()
->createBlock('core/text', 'example-block')
->setText("
<h1>Import/update products</h1>
<p>On this page you can start the import of all products from Misstoro.</p>
...bla bla bla...
");
$this->_addContent($block);
$url = $this->getUrl('*/*/do_import');
$this->_addButton('button_import', array(
'label' => Mage::helper('import')->__('Start import'),
'onclick' => 'setLocation(\'' . $url .'\')',
'class' => 'add',
));
$this->renderLayout();
}
$this->_addButton gives me an "Call to undefined method" error.
What is the right way to do this?
/ Carsten
Figured it out with a little more research:
$url = $this->getUrl('*/*/do_import');
$block = $this->getLayout()
->createBlock('adminhtml/widget_button')
->setData(array(
'label' => Mage::helper('import')->__('Start import'),
'onclick' => 'setLocation(\'' . $url .'\')',
'class' => 'add',
));
$this->_addContent($block);