Need help writing a simple function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chrisbrooks7
Forum Newbie
Posts: 4
Joined: Thu Jul 17, 2008 7:51 pm

Need help writing a simple function

Post by chrisbrooks7 »

I am truly a newbie to PHP but I was hoping someone could help me with this coding problem.

Code: Select all

<?php 
$date = '11.27.07';
$test = '<a class="entry" href="#"><img src="/htdocs/Images/Portfolio/<?php print($date)?>.F0<?php print($count)?>.jpg" width="60" height="45" border="0"/><span><img src="/htdocs/Images/Portfolio/<?php print($date)?>.F0<?php print($count)?>.jpg" border="0"/><br /></span></a><br />';
   
for($count=1; $count<=8; $count++) {
   echo ($test);
}
?>
What I basically want is for the loop to exho $test, but of course it doesn't read the php code within the echo as php, just reads it as text. I know this is not the way to structure the code I just thought it would help get my end goal across. What in essence I am going for is when it loops it prints $date for the image file name and prints $count for the increasing number so each time it loops I get the next image.
Example: 11.27.07.F01.jpg, 11.27.07.F02.jpg, 11.27.07.F03.jpg.....and so on.

Any ideas on how to structure this code? Thanks.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Need help writing a simple function

Post by s.dot »

Code: Select all

function printNames($date, $iterations)
{
    for ($i = 0; $i < $iterations; $i++)
    {
        echo '<a class="entry" href="#"><img src="/htdocs/Images/Portfolio/' . $date . 'F0' . $i . '>.jpg" width="60" height="45" border="0"/><span><img src="/htdocs/Images/Portfolio/' . $date . 'F0' . $i . '.jpg" border="0"/><br /></span></a><br />';
    }
}
:)

Please use when writing code in your posts. :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
chrisbrooks7
Forum Newbie
Posts: 4
Joined: Thu Jul 17, 2008 7:51 pm

Re: Need help writing a simple function

Post by chrisbrooks7 »

Will do. Thank you so much for the help!
chrisbrooks7
Forum Newbie
Posts: 4
Joined: Thu Jul 17, 2008 7:51 pm

Re: Need help writing a simple function

Post by chrisbrooks7 »

probably something simple that I am doing incorrectly but the function is not echoing anything:

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thickbox</title>
</head>
<body>
<div style="height: 10px;"></div>
<div id="thickboxEntry">
<?php
$date = '11.27.07';
$iterations = '8';
function printNames($date, $iterations)
{
    for ($i = 0; $i < $iterations; $i++)
    {
        echo '<a class="entry" href="#"><img src="/htdocs/Images/Portfolio/' . $date . 'F0' . $i . '>.jpg" width="60" height="45" border="0"/><span><img src="/htdocs/Images/Portfolio/' . $date . 'F0' . $i . '.jpg" border="0"/><br /></span></a><br />';
    }
}
?>
</div>
</body>
</html>
 
I look at the source code and in the <div id="thickboxEntry"> </div> where all the code should be printed there is nothing, it's just blank. Any ideas?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Need help writing a simple function

Post by s.dot »

You have to call the function. :)

Code: Select all

<div id="thickboxEntry">
<?php
 
//defining the function
function printNames($date, $iterations)
{
    for ($i = 0; $i < $iterations; $i++)
    {
        echo '<a class="entry" href="#"><img src="/htdocs/Images/Portfolio/' . $date . 'F0' . $i . '>.jpg" width="60" height="45" border="0"/><span><img src="/htdocs/Images/Portfolio/' . $date . 'F0' . $i . '.jpg" border="0"/><br /></span></a><br />';
    }
}
 
//calling the function
printNames('11.27.07', 8);
?>
</div>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
chrisbrooks7
Forum Newbie
Posts: 4
Joined: Thu Jul 17, 2008 7:51 pm

Re: Need help writing a simple function

Post by chrisbrooks7 »

Works like a champ. I apologize for my lack of knowledge in PHP, but thank you for being patient and working with me.
Post Reply