WordPress.org

Forums

Can't output the number 0 in widget (2 posts)

  1. agon024
    Member
    Posted 11 months ago #

    I have an input box where a user can input a number and then that number will be outputted into an html page. It all works fine except when you enter in the number 0 it doesn't output anything but it needs to say 0. This is a live baseball game scoring widget so I need "Inning 1" to read "0" if no runs were scored. Here is the gist of my code:

    $t1Inning1 = ( $instance['t1Inning1'] ) ? $instance['t1Inning1'] : '-';
    
    <div><?php echo $t1Inning1 ?></div>
    <label for="<?php echo $this->get_field_id('t1Inning1'); ?>">
      <input id="<?php echo $this->get_field_id('t1Inning1'); ?>"
      name="<?php echo $this->get_field_name('t1Inning1'); ?>"
      value="<?php echo esc_attr( $instance['t1Inning1'] ); ?>"
      maxlength="2" size="3" />
    </label>

    You can see the output at http://juniorregionals.com/ in the middle left. The '-' is just the default value which doesn't change when you input 0 in the widget and save it but again I need it to read 0 if you input 0.

    If I change it to :
    $t1Inning1 = isset( $instance['t1Inning1'] ) ? $instance['t1Inning1'] : '-';
    it does output a 0 if thats what you put in but then the res of the inning don't show a default slash. the output is empty. So reiterate I need to be able to enter a 0 and have it output a 0 and if the input is empty I need it to have a dash (-).

    Let me know if I need to be more specific.

  2. agon024
    Member
    Posted 11 months ago #

    Someone from the WordPressStackexchange site had me add an exact comparison:

    $t1Inning1 = isset( $instance['t1Inning1'] ) && $instance['t1Inning1'] !== '' ? $instance['t1Inning1'] : '-';

    and that works flawlessly.

Reply

You must log in to post.

About this Topic

Tags