...

View Full Version : echo + function?



martynball
01-15-2010, 09:53 PM
I want a function to execute in my echo, but it will not. it is just writing the function.

Can this be done?

Echo


echo "<tr class=\"<?php odd_even()?>\"/>


Function


<?php
function odd_even() {
$number = 1;
if( $odd = $number%2 )
{
echo "oddFile";
$number++;
}
else
{
echo "evenFile";
$number++;
}
}
?>

Rowsdower!
01-15-2010, 09:59 PM
Try something like this instead:

<?php
function odd_even() {
$number = 1;
$result="";
if( $odd = $number%2 )
{
$result="oddFile";
$number++;
}
else
{
$result="evenFile";
$number++;
}
return $result;
}
?>

And then implement like this:

echo "<tr class=\"".odd_even()."\"/>";

martynball
01-15-2010, 10:05 PM
Awesome, cheers. Works perfect.

martynball
01-15-2010, 10:11 PM
Wait, it isnt. Its only returning "oddFile".

:/

Rowsdower!
01-15-2010, 10:18 PM
Wait, it isnt. Its only returning "oddFile".

:/

That's a problem with your "if" statement then. How are you setting the value of $odd?

martynball
01-15-2010, 10:48 PM
I have just changed it, but still doesn't work. Here is the test page:
http://martynleeball.x10hosting.com/portfolio/admin/test.php

and the code:


<!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>Test</title>
</head>
<body>
<?php
function odd_even() {
$number = 1;
$OEresult="";
if( $number%2 == 0 )
{
$OEresult="evenFile";
}
else
{
$OEresult="oddFile";
}
return $OEresult;
}
?>
<?php while ($number <= 5) {echo odd_even()."<br />"; $number++; } ?>
</body>
</html>

Dormilich
01-15-2010, 10:57 PM
a) the variable $number in odd_even() is always reset to 1
b) unless $number is a global (or a closure) incrementing $number outside of odd_even() is futile

probably the best approach would be passing $number as parameter:

function odd_even($number)
{
if ($number%2 == 0)
{
return "evenFile";
}
return "oddFile";
}
$number = 0;
while (++$number < 6)
{
echo odd_even($number), "<br>";
}

SilverBulletUK
01-15-2010, 10:59 PM
function odd_even(){
static $switch = 0;
$switch++;
return (0 === ($switch % 2)) ? 'odd' : 'even' ;
}


echo '<tr class="', odd_even(), '"/>';

:thumbsup:

martynball
01-15-2010, 11:20 PM
function odd_even(){
static $switch = 0;
$switch++;
return (0 === ($switch % 2)) ? 'odd' : 'even' ;
}


echo '<tr class="', odd_even(), '"/>';

:thumbsup:

Used this, works. Thanks :)

Edit:
Save making another thread, here is another question.

When I delete a row from my database I want the image linked to it deleted as well, but this image is in a different directory. unlink() keeps on giving me errors.

I have the script running from:

homedir/admin/delete.php

But the images are stored in:

homedir/uploads/images/

I have tryed unlink("../uploads/images/".$filename).
But I just get errors.

JAY6390
01-15-2010, 11:55 PM
echo '<tr class="'.(($counter++ % 2) ? 'evenFile' : 'oddFile').'">';
is much shorter and will do the trick



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum