0

I have a number i'm taking out of database, basically votes.

So numbers are going to be separate.

For example [ ] = A div styled box.

And $foo = 100

it will display like this:

[ 1 ] [ 0 ] [ 0 ].

        <?php
        for($i=0; $i<strlen($counter); $i++) {
            echo "<div class='votebg'>$counter[$i]</div>";
        }
        ?>

But I made this work automatically, just by taking the INT from the database.

Now my int is empty, so I put up a checker to check if its empty, if it is, it will make it 0.

if (empty($rcvotes)) {
$rcvotes = 0;
}

Now my page only displays a zero

[ 0 ]

How do I make it so it automatically fills the missing number with a zero if not exists?

For example, we have 7 votes.

The number will display like this:

[ 0 ] [ 0 ] [ 7 ]

We have 88 votes, the number will display like this:

[ 0 ] [ 8 ] [ 8 ]

What is exactly the problem?

Thanks!

mysql_query('SELECT FROM * `vote_count`');
$rcvotes = $row['vote_number'];
if (empty($rcvotes)) {
$rcvotes = 0;
}

$counter = strval($rcvotes);
Grijesh Chauhan
  • 55,177
  • 19
  • 133
  • 197
Jony kale
  • 85
  • 1
  • 1
  • 6

1 Answers1

1

You can use string padding:

$counter = str_pad($rcvotes, 3, "0", STR_PAD_LEFT);
jeroen
  • 90,003
  • 21
  • 112
  • 129