SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation + Simple PHP Number Question

    Hey guys & gals,

    I'm working on a Wordpress template and have come across a problem within my PHP.

    To get the # of posts within a category you use...

    Code:
    <?php the_category_count(146); ?>
    Now I want to do a test and if the number is less then 100 add 00 in front of it or if it's < 1000 add one zero in front of it.

    I tried this...

    Code:
    <?php if((the_category_count(146)) < 1000){ echo "0"; } ?>
    <?php the_category_count(146); ?>
    but it keeps giving me this error...

    Code:
    Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /nfs/c02/h06/mnt/15401/domains/idesigniphone.com/html/wp-content/themes/default/header.php on line 29
    Is there a way to convert the_category_count(146) into a plain number before running a test on it so that it doesnt give me any problems?

    Thank you for looking,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  2. #2
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $c = the_category_count(146);
    
    if( $c < 100 )
        $c = '00' . $c;
    else if( $c < 1000 )
        $c = '0' . $c;
    
    // ...
    
    echo $c;

  3. #3
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure I understand what you are trying to accomplish. Try this:

    PHP Code:
    $posts the_category_count(146);
    for (
    $i 1$j $posts strlen($posts); $i $j$i++)
    {
        echo 
    '0';
    }
    echo 
    $posts

  4. #4
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $number str_pad((string)the_category_count(146), 4'0'STR_PAD_LEFT); 
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you kommando & risoknop.

    I have tried the first method and it works like a charm.

    I will give the seccond one a shot.

    Thanks again for the help guys.

    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  6. #6
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome.

    I think my way requires less processing overhead, too, even if the others also work.


Bookmarks

Posting Permissions

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