PHP Random Images (Mr. Powers or other PHP Experts)
Hello everyone,
This is the script from David Power's book using arrays and the rand function (Disclosure).
Basically it changes the images randomly on browser load or refresh.
However, I am trying to add an additional, not sure what to call it, a string or another caption for each images.
Instead of this being a caption, it will be a link, so a link will appear under each caption that goes to another page
when it is clicked on for additional info about the image.
I tried adding another caption and adding a like to it but getting syntax error.
Thanks everyone!
Here is the code:
<?php
$images = array(
array('file' => 'image1',
'caption' => 'Caption 1'),
array('file' => 'image2',
'caption' => 'Caption 2'),
$i = rand(0, count($images)-1);
$selectedImage = "graphics/{$images[$i]['file']}.png";
$caption = $images[$i]['caption'];
?>
And here is where the images are written to the page:
<div id="stage">
<img src="<?php echo $selectedImage; ?>" alt="Random image" />
<p id="caption"><?php echo $caption; ?></p>
</div>
Thanks everyone!
WE
Thanks for the helpful link!
I found the solution to the problem it was very simple and I feel a little stupid asking the question in the first place but, that's how learning works.
We sometimes stumble upon knowledge.
The problem is I was adding the second string outside of the array.
Here is the right way:
array('file' => 'image1',
'caption' => 'Hello!',
'link1' => '<a href="http://www.msnbc.com">Read more</a>',<--Instead of putting the link here
link2' => '<a href="http://www.npr.org">Idea Center</a>'
<-- I was putting the link here out side the array and not properly ending the string.
$i = rand(0, count($images)-1);
$selectedImage = "graphics/{$images[$i]['file']}.png";
$caption = $images[$i]['caption'];
$link1 = $images[$i]['link1'];
$link2 = $images[$i]['link2'];
?>
Output here:
<div id="stage">
<img src="<?php echo $selectedImage; ?>" alt="Random image" />
<p><?php echo $caption; ?></p>
<?php echo $link1; ?>
<?php echo $link2; ?>
</div>
WE
Similar Messages
-
I am trying to use the PHP random image generator but I don't
know where to specify the size I want all the pictures to come up
in the code. I created a "php" test page and inserted said code
where I want the images to display but they are too large and throw
the entire page off. Is there a way to tell it that all pictures
should be called up at 240(width) x 350(height)? Any help would be
greatly appreciated. The reason I am doing this is because there
are about 200 images (of students) in this folder and coding each
picture seperately is too time consuming.
Regards,
RM (using DW MX2004)Disregard....I figured it out. As it turns out the best
solution would be to have your original pictures saved with the
desired dimensions. I was able to point the PHP to a directory that
contained pictures with said dimensions and it worked like a charm.
regards. -
Hello everyone,
Im trying to use (for the first time) random images generated
by PHP. I
installed a Technorama extension and then followed thru the
steps to place a
random image in the page, but there is nothing there when I
browse the page.
Please forgive my ignorance, but I have never done any php
stuff before.
And yes, the files are uploaded to my server. (which does
have php enabled).
The practice page I am using is
www.fionahayward.com/interiordesign/practice.php
I have the effect working with javascript on this page
www.fionahayward.com/interiordesign but am trying to use php
incase people
have javascript turned off.
Maybe its something to do with how Ive set up Server
Behaviours or something
(only guessing)
Thanks for listening
Fiona1) create a php file named rotate.php in the folder where the
images are.
2) paste this code (between the [code ] and [/code]
markers... into that file and save it.
[code]
<?php
$folder = '.';
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';
// --------------------- END CONFIGURATION
$img = null;
if (substr($folder,-1) != '/') {
$folder = $folder.'/';
if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
isset( $extList[ strtolower( $imageInfo['extension'] ) ] )
file_exists( $folder.$imageInfo['basename'] )
$img = $folder.$imageInfo['basename'];
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $extList[ strtolower( $file_info['extension'] ) ] )
$fileList[] = $file;
closedir($handle);
if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[
$imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = @imagecreate (100, 100)
or die ("Cannot initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
?>
[/code]
4) in whichever file you want the random images to appear,
reference the image so:
src="path/to/that/image/folder/rotate.php"
I tend to use root-relative pathing, but that part is up to
you.
NOTE: I've found that random image references are quirky in
IE browsers - they seem to want the image width and height to
display images that are dynamically selected... I've used this
script to display images that are all the same size and groups that
aren't. When the image size is unknown, I use width:100%
height:100% in my css)
email me if you have any issues with using the script. I use
it a lot with WordPress sites, but it isn't a WP only solution.
syncbox AT gmail DOT com (do the obvious)
HTH -
I am using a
PHP random image
script on a site in which I have 2 different images on each
templated page (random images throgh a site). The two images call
to 2 different folders with the random image script in them (to get
2 distinct images). Thus, 2 different random images. My issue is
that it seems that most browsers at some point do not call for a
new image but use what was there in the last page as it is the same
name:
http://ansano.com/asl/images/random/top/random_image.php
or
http://ansano.com/asl/images/random/bottom/random_image.php
Any idea of how to force a browser to call the PHP script? Or
how to change it so it works site wide?
Thanks!!I've now been sitting looking for this for a while. I don't seem to be able to get it to work.
Do I need to host to a folder first and then to the FTP server after?
I don't seen to be able to find the new page (TEST PAGE) in the folder I used to publish the site to before I went online. Tried searching for it in finder but I don't get any results on (TESTPAGEfiles).
I can see what you are talking bout I only publish to a local folder but I want to publish via the FTP option in iWeb.
Thank you very much for helping me -
Suggestions for simple random image script
Would like to randomly display one of five new JPG banner each time page is loaded.
Your experience would be appreciated.
ThanksSince no one replied, I am posting solution I found.for future inquiries.
Simple PHP Random Image Script
This can be done in one line of text directly in your HTML and a collection of images.
The first step is to gather together the images you wish to have randomly rotated.
Change all of their file names to a numerical order, starting with the number 1.
* 1.jpg
* 2.jpg
* 3.jpg
* 4.jpg
Be sure each image has the same extension (either all jpg, png, or gif),
and that there are no gaps in the numbers.
Also, place these images in their own folder to keep everything organized.
Write the Code
In the HTML of your page, insert the following code where you want an image to display:
<img src="path/<?php echo rand(1,n);?>.jpg" alt="Random Image" />
The “rand” operator will display a randomly generated number between 1 and ‘n’.
Change ‘n’ to the total amount of pictures you have arranged numerically
(or the highest numbered picture you have).
In the example above I only have 4 pictures, therefor I would change it to: rand(1,4); -
Using PHP to display semi-random images?
Hello,
I'm looking for help using PHP and MySQL to generate semi-random images on my web site. Here's what I'm trying to do: I have 60 images, but I only want to display 16 at a time (for examlpe http://artisdead.net/antelopegardens.php).
The way I imagine this is, the design has 16 image placeholders. I want Placeholder 1 (the first image) to randomally pull record ID 1, 2, or 3 from the SQL database, while Placeholder 2 pulls record ID 4-6, etc. It's important that one of the first 3 images displays first, while one of the second 3 images displays next, etc.
I've found various ways to randomize all the images, but I'm not understanding how to constrain the randomization the way I'm looking to. Any help would be greatly appreciated. Thanks in advance.Alamo_Melt wrote:
The way I imagine this is, the design has 16 image placeholders. I want Placeholder 1 (the first image) to randomally pull record ID 1, 2, or 3 from the SQL database, while Placeholder 2 pulls record ID 4-6, etc. It's important that one of the first 3 images displays first, while one of the second 3 images displays next, etc.
Before offering a possible solution, I should point out that your plan has a minor flaw. If you display only 16 images, and want the random choice to come from incremental sets of three, the maximum number you can have in your set is 48. The final 12 images will never be selected. For 60 images, you need 20 placeholders. The alternative is to have a total set of 64 images, and make the random choice come from incremental sets of four images.
With that caveat, here's how I would do it. The primary keys of your records might not be consecutive, particularly if records are deleted and new ones added, so it might be a good idea to have a separate column with consecutive numbers from 1 to the the maximum. To select random numbers from incremental groups of three, use the following:
$id = array();
$min = 1;
$max = 3;
for ($i = 0; $i < 16; $i++) {
// generate random number from current set
$id[$i] = mt_rand($min, $max);
// increment the minimum and maximum numbers for the next set
$min += 3;
$max += 3;
// join the selected values as a comma-separated string
$vals = implode(',', $id);
$sql = "SELECT image FROM images WHERE id IN ($vals)";
// excecute the SQL query -
I am looking for an easy to understand PHP tutorial on
uploading an image to a folder. However I would like to put a
reference/path to the image in a MySQL batabase table.newhorizonhosting.com wrote:
> I am looking for an easy to understand PHP tutorial on
uploading an image to a folder. However I would like to put a
reference/path to the image in a MySQL batabase table.
The PHP online manual is pretty comprehensive on file
uploads.
http://www.php.net/manual/en/features.file-upload.php
You can get the name of the file from
$_FILES['fieldName']['name']
(where 'fieldName' is the name of the form field that uploads
the
image). It's just a question of using that information to
insert into a
database.
I cover image file uploads in considerable detail in "PHP
Solutions" if
you're interested in a more comprehensive approach.
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/ -
I was trying to open a website as a link from another website and I get a new tab that says
awclick.php(GIF Image 1x1 pixels)
I can open the website without clicking on a link to it by just typing in the address but as this is a cash back site I am linking from it doesn't help!You're welcome
-
PHP -Mysql image resize and upload
All,
Any good ideas, code out there to help me do a PHP MySQL image resize, uploade and display? Thanks for your help!For upload image, u may look at the similar thread HERE
-
David Powers cs5 with php...Zend
This one is aimed for David Powers. We are using your book for my advanved programming class. I continually have problems because they had us buy a domain instead of the one the book does it. At this point I'm on page 222ish, installing Zend. Obviously I'm using dreamweaver and my domain is with godaddy.com. I'm doing the section where you are are installing the library. I've got the library.php with no errors, I'm connected to my domain, and I have the zend file inside my local site folder. I've tried in the C drive but I cotinually get the same error:
Warning: require_once(Zend/Loader/Autoloader.php) [function.require-once]: failed to open stream: No such file or directory in/home/content/79/7340479/html/library.php on line 4
Fatal error: require_once() [function.require]: Failed opening required 'Zend/Loader/Autoloader.php' (include_path='.:/usr/local/php5/lib/php:C:\Users\Becky\Desktop\colts\ZF\ZendFramework-1. 11.4-minimal\library') in /home/content/79/7340479/html/library.php on line 4
I've tried putting the zend folder in c, desktop, in side local site folder and same thing everytime. Any help would be appreciated.Re: David Powers cs5 with php...Zend
Ok I'm gonna try and help ya until David gets here. Since I use Zend and have read parts of his book.
First, are you trying to install zend at godaddy?
Your include file is odd. "include_path='.:/usr/local/php5/lib/php:C:\Users\Becky\Desktop\colts \ZF\ZendFramework-1.11.4-minimal\library') in/home/content/79/7340479/html/library.php" - means PHP and Zend are located on 2 different machines
I appreciate it. the "C:\Users\Becky\Desktop\colts \ZF\ZendFramework-1.11.4-minimal\library' is where the file path.I use Godaddy but I have it zend on my computer and am just trying to get it to run. I tried running it from desktop, C:(where it's at), andneven put it in the file manager on godaddy and tried that path...
the code I'm using is:
<?php
$library = 'C:\Users\Becky\Desktop\colts\ZF\ZendFramework-1.11.4-minimal\library';
set_include_path(get_include_path() . PATH_SEPARATOR . $library);
require_once('Zend/Loader/Autoloader.php');
$loader = Zend_Loader_Autoloader::getInstance();
if ($loader) {
echo 'OK';
} else {
echo 'We have a problem';
I'm followed the book step by step but...
when i click autoloader.php in the tab bar it has a get option. but when i click it it says "operation failed because it is not on the remote site. -
Anyone have experience with other PHP Frameworks?
Hey PHP devs,
I was hoping to get some advice on what other PHP Framework you guys like to use. (I realize ADDT might not be considered a PHP framework.)
I love the ADDT Framework, and it is a wonderful tool for many of the projects I do. However, I have a project coming up that will be much more extensive than my usual work. Don't get me wrong, I have built some fairly complex dynamic PHP applications with ADDT, but with this next project I need to go for more of an MVC type application. Also, I need to separate the php from the html.
Right now I am looking at CakePHP, symfony, CodeIgniter, and of course Zend. Anyone have any experience with these? Has anyone used ADDT along with another framework? Supposedly these frameworks can be used together in combinations.
Thanks for any advice.
ShaneHi Shane,
from all the frameworks you mention, Zend seems to get the highest appreciation from the developer crowd, whereas you´ll find a nice mixture of good & bad reviews on all others.
I realize ADDT might not be considered a PHP framework
what else would you call it ? :-)
Supposedly these frameworks can be used together in combinations
I doubt you´ll be able to use ADDT in conjunction with e.g. Zend, and from what I know, every framework out there is a sort of "closed system" with no hooks to access the methods used by other frameworks.
Cheers,
Günter Schenk
Adobe Community Expert, Dreamweaver -
Random Image not displaying in Live View.
My page is at:
www.andrewjamesartist.co.uk
There's a random image script on the front page.
<img src="randomimages/rotate.php" alt="A Sample Image from the work of Andrew James" />
That's how it is placed in the page.
It shows in preview in browser and it's OK live but it doesn't show in LiveView.
I use a testing server set up with virtual hosts.
MartinMy page is at:
www.andrewjamesartist.co.uk
There's a random image script on the front page.
<img src="randomimages/rotate.php" alt="A Sample Image from the work of Andrew James" />
That's how it is placed in the page.
It shows in preview in browser and it's OK live but it doesn't show in LiveView.
I use a testing server set up with virtual hosts.
Martin -
Randomizing image/text on refresh
Hello. I'm new...quite new to DW and web work, but have a small, general understanding of it. I'm an animator and am in the midst of creating my own portfolio site. I don't understand code, at least uber-extreme code. I'm attempting to have specific quotes on my homepage from clients and would like to have them change, or have a new text/quote appear on refresh or upon a new visit to the site (just so it's not the same one everytime).
Would I need to just render out a .swf file in AE or is there a means of doing this within DW w/o using crazy code? Thanks in advance, AndyHTML has no way to do something like this. You would indeed have to use Flash, or JavaScript, or some server-scripting to accomplish that task.
It's quote easy to do with something like PHP, for example, by declaring a random variable in the page, e.g.,
<?php
$imageVar = rand(1,5) /* this will generate a random number between 1 and 5 */
?>
and then using that random variable in the image's source attribute -
<img src="images/random/image<?php echo $imageVar; ?>.jpg" />
If you are getting the idea that you will have to come to grips with code sooner rather than later, then you are on the right track. DW is very punishing to those who won't take the time to learn the underlying technologies....
If you Google "random image" and "Dreamweaver" you may also be able to find extensions that do the same thing for you (in JavaScript). I would recommend that you visit http://www.projectseven.com and search there - I do know that they have such a thing, and can wholeheartedly endorse their products as being the best of show.... -
Need Random Image Script with Links
I am needing a script for random images on my homepage. I
need the image to have its own link depending on what image is
displayed. I would like a php script (non database) if
possibleSee comments on DW forum about random includes and this
particular markup
shown below.
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com
- Template Triage!
http://www.projectseven.com/go
- DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs,
Tutorials & Resources
http://www.macromedia.com/support/search/
- Macromedia (MM) Technotes
==================
"newhorizonhosting.com" <[email protected]>
wrote in message
news:e29on1$buf$[email protected]..
>I figured it out by doing a random include
>
> <?
> srand(time());
> $random = (rand()%3);
> print("$random");
> require_once("./$random.php");
> ?> -
Need Random Image Script with Link
I am needing a script for random images on my homepage. I
need the image to have its own link depending on what image is
displayed. I would like a php script (non database) if
possibleNot sure about the PHP but there are some good scrips on
www.kaosweaver.com
Paul Whitham
Certified Dreamweaver MX2004 Professional
Adobe Community Expert - Dreamweaver
Valleybiz Internet Design
www.valleybiz.net
"newhorizonhosting.com" <[email protected]>
wrote in message
news:e28rpm$8is$[email protected]..
>I am needing a script for random images on my homepage. I
need the image to
>have its own link depending on what image is displayed. I
would like a php
>script (non database) if possible
Maybe you are looking for
-
Cannot install Win7 on my Qosmio F50
HI, I hope you can help me in my problem. I have Laptop Old Qosmio F50-10G Model #. PQF55E-00V012AR. It's installed windows vista home premium. For my desired to change my OS to windows 7, I reformat it true proper installing. the problem is, It can'
-
i have a string "hai oracle tech team" i want to calculate the number of words in it i.e. it should return 4 how to do it
-
Blackberry curve 9300 manual network selection
when i set my curve 9300 to manual network selection, and i select my networ, if i go in a palce underground or something and i lose the signal, the phone doesn' t pick up the signal automatically when i am back in a coverage area. instead, i find an
-
Are optional (non-existing) elements ALWAYS copied to target in TransformXS
Assume I use the TransformXSL activity to copy a structure from source XSD to target XSD. Some of the elements are specified as optional. Say for a particular situation some of these optional elements in the source XML do NOT exist. It seems to me th
-
Ipbase to IP service License upgrade stack switches
All switch part number # WS-C3750X-48P-S current License Level: ipbase switch currently connected as a stack with three switches I am planning to install license to make existing switch IP base to full L3 functionality that is IP services , just t