Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post

    echo + function?

    I want a function to execute in my echo, but it will not. it is just writing the function.

    Can this be done?

    Echo
    PHP Code:
    echo "<tr class=\"<?php odd_even()?>\"/>
    Function
    PHP Code:
    <?php
    function odd_even() {
    $number 1;
        if( 
    $odd $number%)
        {
            echo 
    "oddFile";
            
    $number++;
        }
        else
        {
            echo 
    "evenFile";
            
    $number++;
        }
    }
    ?>

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Try something like this instead:
    PHP Code:
    <?php
    function odd_even() {
    $number 1;
    $result="";
        if( 
    $odd $number%)
        {
            
    $result="oddFile";
            
    $number++;
        }
        else
        {
            
    $result="evenFile";
            
    $number++;
        }
        return 
    $result;
    }
    ?>
    And then implement like this:
    PHP Code:
    echo "<tr class=\"".odd_even()."\"/>"
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    martynball (01-15-2010)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Awesome, cheers. Works perfect.

  • #4
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Wait, it isnt. Its only returning "oddFile".

    :/

  • #5
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by martynball View Post
    Wait, it isnt. Its only returning "oddFile".

    :/
    That's a problem with your "if" statement then. How are you setting the value of $odd?
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #6
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    I have just changed it, but still doesn't work. Here is the test page:
    http://martynleeball.x10hosting.com/...admin/test.php

    and the code:
    PHP 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%== )
        {
            
    $OEresult="evenFile";
        }
        else
        {
            
    $OEresult="oddFile";
        }
        return 
    $OEresult;

    ?>
    <?php 
    while ($number <= 5) {echo odd_even()."<br />"$number++; } ?>
    </body>
    </html>

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    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:
    PHP Code:
    function odd_even($number
    {
        if (
    $number%== 0)
        {
            return 
    "evenFile";
        }
        return 
    "oddFile";
    }
    $number 0;
    while (++
    $number 6)
    {
        echo 
    odd_even($number), "<br>";

    Last edited by Dormilich; 01-15-2010 at 11:03 PM.

  • Users who have thanked Dormilich for this post:

    martynball (01-15-2010)

  • #8
    New Coder
    Join Date
    Jan 2009
    Location
    North-East, UK
    Posts
    31
    Thanks
    0
    Thanked 2 Times in 2 Posts
    PHP Code:
    function odd_even(){
        static 
    $switch 0;
        
    $switch++;
        return (
    === ($switch 2)) ? 'odd' 'even' ;

    PHP Code:
    echo '<tr class="'odd_even(), '"/>'
    Last edited by SilverBulletUK; 01-15-2010 at 11:02 PM.

  • Users who have thanked SilverBulletUK for this post:

    martynball (01-15-2010)

  • #9
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by SilverBulletUK View Post
    PHP Code:
    function odd_even(){
        static 
    $switch 0;
        
    $switch++;
        return (
    === ($switch 2)) ? 'odd' 'even' ;

    PHP Code:
    echo '<tr class="'odd_even(), '"/>'
    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.
    Last edited by martynball; 01-15-2010 at 11:32 PM.

  • #10
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    PHP Code:
    echo '<tr class="'.(($counter++ % 2) ? 'evenFile' 'oddFile').'">'
    is much shorter and will do the trick
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •