[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP rplace number array with images when printing

Posted on 2009-02-14
11
Medium Priority
?
265 Views
Last Modified: 2012-05-06
Hi, I have the code below in PHP which is pretty simple. I'm trying to do a random lotto picker. I have all the images in a folder "images/numbers/1.gif" all the way up to 45.gif

I know this should be pretty simple to implement but I'm not as well versed in PHP.

I want the end result to print out the corresponding image numbers instead of text.

Any help much appreciated :) Thanks
<?php
$balls = range(1,45);
shuffle($balls);
$pick = array_slice($balls,0,6);
$drawn = implode(", ",$pick);
sort($pick);
$ascend = implode(" ... ",$pick);
 
print ("<center><b>Your Lucky Quickpick</b><br>");
print $drawn;
print ("<br><br>");
print ("<center>In ascending order ...<br>");
print $ascend;
print ("<br><br>");
print ("<center>Good Luck :)");
 
?>

Open in new window

0
Comment
Question by:keevo
  • 7
  • 3
11 Comments
 
LVL 9

Expert Comment

by:unassassinable
ID: 23639962
Im also not well versed in PHP, but would something like this work?  youd have to integrate the print function in there...

$rand=rand(1,45);
$name="images/numbers/".$rand.".gif";
include_once($name); ?>
0
 
LVL 1

Author Comment

by:keevo
ID: 23639977
i'll try this now and let you know. thanks for quick response
0
 
LVL 9

Expert Comment

by:unassassinable
ID: 23640003
Yeah you bet.  Thats a piece of code I use on my site to randomly show 1 of 5 different divs.  I just replaced the directories and the extensions (to .gif rather than .php for your case).  you can see how it works here:

http://equinoxits.com/home.php

just hit the refresh button a few times
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:keevo
ID: 23640043
I'm pretty sure this is the code :) it will work but at the moment it is printing out the text location of the image and not retrieving the actual image

$rand = range(1,45);
$name= "images/numbers/".$rand.".gif";

print= $name;

I tried

$name= $_SERVER['images/numbers/$rand.gif'];

but no joy

0
 
LVL 9

Expert Comment

by:unassassinable
ID: 23640120
Could you use something like this?  Not sure what the application is your using,  if its a web app, this would work.  

<img src="images/<?php echo(rand(1,45)); ?>.jpg" alt="" />

heres a working example:

http://equinoxits.com/lotto.php
0
 
LVL 1

Author Comment

by:keevo
ID: 23640187
i'm still a small bit lost. was trying something like this:

$images = array(
        1 => '1.gif',
        2 => '2.gif',
        3 => '3.gif',
        4 => '4.gif',
      5 => '5.gif',
        6 => '6.gif',
        7 => '7.gif',
        8 => '8.gif',
        9 => '9.gif',
      10 => '10.gif',
        11 => '11.gif',
        12 => '12.gif',
        13 => '13.gif',
        14 => '14.gif',
      15 => '15.gif',
        16 => '16.gif',
        17 => '17.gif',
        18 => '18.gif',
        19 => '19.gif',
            20 => '20.gif',
        21 => '21.gif',
        22 => '22.gif',
        23 => '23.gif',
        24 => '24.gif',
        25 => '25.gif',
            26 => '26.gif',
        27 => '27.gif',
        28 => '28.gif',
        29 => '29.gif',
        30 => '30.gif',
        31 => '31.gif',
            32 => '32.gif',
        33 => '33.gif',
        34 => '34.gif',
        35 => '35.gif',
        36 => '36.gif',
        37 => '37.gif',
            38 => '38.gif',
            39 => '39.gif',
        40 => '40.gif',
        41 => '41.gif',
        42 => '42.gif',
            43 => '43.gif',
            44 => '44.gif',
            45 => '45.gif',
    );

shuffle($images);
$pick = array_slice($images,0,6);
$drawn = implode(", ",$pick);
sort($pick);
$ascend = implode(" ... ",$pick);

print ("<center><b>Your Lucky Quickpick</b><br>");
print $drawn;
print ("<br><br>");
print ("<center>In ascending order ...<br>");
print $ascend;
print ("<br><br>");
print ("<center>Good Luck :)");



resulting in

Your Lucky Quickpick
27.gif, 9.gif, 5.gif, 7.gif, 15.gif, 35.gif

In ascending order ...
15.gif ... 27.gif ... 35.gif ... 5.gif ... 7.gif ... 9.gif


lol...

            
0
 
LVL 1

Author Comment

by:keevo
ID: 23640240
hmmmmm ok i have the numbers and a corresponding image array but don't know how to display the images

$balls = range(1,45);
$images = array(
        1 => '1.gif',
        2 => '2.gif',
        3 => '3.gif',
        4 => '4.gif',
            5 => '5.gif',
        6 => '6.gif',
        7 => '7.gif',
        8 => '8.gif',
        9 => '9.gif',
            10 => '10.gif',
        11 => '11.gif',
        12 => '12.gif',
        13 => '13.gif',
        14 => '14.gif',
            15 => '15.gif',
        16 => '16.gif',
        17 => '17.gif',
        18 => '18.gif',
        19 => '19.gif',
            20 => '20.gif',
        21 => '21.gif',
        22 => '22.gif',
        23 => '23.gif',
        24 => '24.gif',
        25 => '25.gif',
            26 => '26.gif',
        27 => '27.gif',
        28 => '28.gif',
        29 => '29.gif',
        30 => '30.gif',
        31 => '31.gif',
            32 => '32.gif',
        33 => '33.gif',
        34 => '34.gif',
        35 => '35.gif',
        36 => '36.gif',
        37 => '37.gif',
            38 => '38.gif',
            39 => '39.gif',
        40 => '40.gif',
        41 => '41.gif',
        42 => '42.gif',
            43 => '43.gif',
            44 => '44.gif',
            45 => '45.gif',
    );
shuffle($balls);
$pick = array_slice($balls,0,6);
$drawn = implode(", ",$pick);
sort($pick);
$ascend = implode(" ... ",$pick);

print ("<center><b>Your Lucky Quickpick</b><br>");
print $drawn;
0
 
LVL 1

Author Comment

by:keevo
ID: 23640395
lost now :(
0
 
LVL 20

Accepted Solution

by:
thehagman earned 1000 total points
ID: 23640437

<?
$balls = range(1,45);
shuffle($balls);
$pick = array_slice($balls, 0 6);
sort($pick);
?>
<h1>Your lucky numbers</h1>
<div>
<?
foreach ($pick as $number) {
?>
<img src="/images/<?=$number?>.gif" alt="<?=$number"/>
<?
}
?>
</div>
<p>Good luck!</p>

Open in new window

0
 
LVL 1

Author Comment

by:keevo
ID: 23640524
absolute genius :)

you even got them to come out in error............

thanks all for your help.

if you want to see what you helped create check http://apps.facebook.com/quickpicker/

now all i need is the lotto numbers lol :)
0
 
LVL 1

Author Comment

by:keevo
ID: 23640525
i meant to say order not error
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question