Page 1 of 1
Need help writing a simple function
Posted: Thu Jul 17, 2008 7:57 pm
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.
Re: Need help writing a simple function
Posted: Thu Jul 17, 2008 8:02 pm
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.

Re: Need help writing a simple function
Posted: Thu Jul 17, 2008 8:06 pm
by chrisbrooks7
Will do. Thank you so much for the help!
Re: Need help writing a simple function
Posted: Thu Jul 17, 2008 8:23 pm
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?
Re: Need help writing a simple function
Posted: Thu Jul 17, 2008 8:31 pm
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>
Re: Need help writing a simple function
Posted: Thu Jul 17, 2008 8:38 pm
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.