-1

I have an ACF field of paid_attendance - I only want it to display the field if the value is greater than 0. The below code only works if I swap >0 out for an actual number (and that number matches the number set on the backend for a particular post(s).

Also, if I were to wrap the below in a div with an <h4> of "Paid Attendance" - how can I also make sure that the <h4> only shows up if the value is greater than 0 as well? In other words, if I get this working so it only displays paid_attendance if value is greater than 0, I dont want to have "Paid Attendance" still showing up for certain posts with nothing next to it (the posts where paid_attendance = 0). Thanks in advance.

<?php
// Different versions of a number
$unformatted = get_field('paid_attendance');
{
    $integerValue = str_replace(",", "", $unformatted);  // Remove commas from string
    $integerValue = intval($integerValue);               // Convert from string to integer
    $integerValue = number_format($integerValue);        // Apply number format
    $floatValue = str_replace(",", "", $unformatted);    // Remove commas from string
    $floatValue = floatval($floatValue);                 // Convert from string to float
    $floatValue = number_format($floatValue, 2);         // Apply number format
if ( '0' == $unformatted ){
    //do nothing

} elseif ( '>0' == $unformatted ) {
    echo $integerValue; 
}} 
?>

Update:

Changed ( '>0' == $unformatted ) to ( $unformatted >0 ) and now its working. However, would appreciate if anyone has insight on my note above regarding only displaying the h4 text if the value is >0 as well. thanks

Update: This does the trick:

elseif ( $unformatted >0 ) { 
echo '<h4 class="paid_tix">Paid Tickets: '; echo '</h4>';echo ' '; echo '<h4 class="integer">'; echo $integerValue; echo '</h4>';
  • paid_attendance is number or text field? – Vasim Shaikh Feb 8 at 6:27
  • There is no h4 in your code. Could you show bigger chunk of it so we know what’s wrong with it and what you want to achieve? – Krzysiek Dróżdż Feb 8 at 6:32
  • paid_attendance is a number field but for some reason, I kept getting an error that it was being read as a string (I encountered this when I was trying to get the comma to work between every 3 digits (ex, 19,000) - hence the reason my code begins with converting the string to a number. Also, sorry about the h4 explanation, at the point I originally asked the question, I had not added that in yet because honestly I was not sure how...I then realized it can just be part of the echo statement. – James Feb 8 at 16:29
  • also, I want to improve on asking questions here as I value everyone's time. I see my question has been downvoted, can anyone offer any insight on how I could have improved on asking this question? thanks – James Feb 8 at 17:02
0

I see you've found your own answer, but I wanted to offer some advice for making your code a little more succinct.

<?php
// Different versions of a number
$unformatted  = get_field('paid_attendance');
$cleaned      = str_replace(',', '', $unformatted);  // Remove commas from string
$integerValue = absint( $cleaned );                 // Convert to absolute integer.
$floatValue   = number_format($cleaned, 2); // Apply number format.
// Note that number_format returns a string, so if you want
// an actual float, call floatval _now_:
$floatValue = floatval( $floatValue );

if ( 0 < $integerValue ) {
    echo $integerValue;
}

So, a few things to unpack here:

  • You can use the "cleaned" (comma-removed) string for getting both your integer and float
  • absint is a WordPress function that will convert a value to an absolute integer.
  • number_format returns a string, so calling it after floatval or intval was superfluous.
  • Your initial if statement didn't do anything, so you can omit it.
  • You're better off checking the $integerValue as it's an int instead of $unformatted or $cleaned since you're doing a numeric comparison.
    • Suppose $unformatted were the string 'a'. In PHP, zero is not less than 'a':
php > var_dump( 0 < 'a' );
bool(false)
  • thanks for your time. this method worked but the comma in numbers has gone away (ex, it now says 19000 instead of 19,000). – James Feb 8 at 17:23
  • Right, that is because of the floatval line. floatval will convert the value to double, which does not have commas. If you want the comma to stay, and treat the value as a string, just remove the call to floatval – phatskat Feb 8 at 20:57
0
$integerValue = str_replace(",", "", $unformatted);  // Remove commas from string
$integerValue = intval($integerValue);               // Convert from string to integer
$integerValue = number_format($integerValue);        // Apply number format

$floatValue = str_replace(",", "", $unformatted);    // Remove commas from string
$floatValue = floatval($floatValue);                 // Convert from string to float
$floatValue = number_format($floatValue, 2);

//Consider that  $unformatted value is converted to integer or float

if ( $integerValue > 0 ||  $floatValue > 0.00){
echo '<h4 class="paid_tix">Paid Tickets: </h4> <h4 class="integer">'.$integerValue/'</h4>';
}

Description:

  • Instead of $unformatted, You must check numeric value so you get proper output of boolean.
  • You don't need to make multiple echo in single statement bind variable in single statement.
  • if still hide then you must need to check css or jquery. class might be display :none or visibility:hidden.
  • this method worked but not with your echo statement as you have it written. thanks – James Feb 8 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.