How to correctly count items?

2

How can I change this code below?

It takes the numeric values placed in MySQL in the comma separated table by placing them in the 1,2,3,4,5,6,7,8,9,10 table. Then it counts the total of items placed in this table separated by the comma. If it has more than one value separated by a comma the message appears in the plural otherwise it appears in the singular. But I'm having a problem: let's say the value entered in MySQL is like this in the medias : 1, table. It puts the message in the plural rather than the singular. To be in the singular it is necessary that the inserted value be without the comma on the side, like this: 1 .

How can I fix this?

<?php $contagem = explode(',', $episodios);
$count = count($contagem); ?>
<? if ($count == 1) { ?><div class="separadores"><div class="separador3">Ultimo Episodio Postado</div></div><? }else { ?><div class="separadores"><div class="separador3">Ultimos Episodios Postados</div></div><? } ?>
    
asked by anonymous 14.11.2014 / 20:39

5 answers

5

If you have string in the variable $episodios ending with a , this will generate an empty entry in the array.

One solution involves using the array_filter () function that lets you clear empty entries in the array:

$contagem = explode(',', "1,");

if (count(array_filter($contagem)) == 1) {
    echo '
    <div class="separadores">
        <div class="separador3">Ultimo Episodio Postado</div>
    </div>';
}
else {
    echo '
    <div class="separadores">
        <div class="separador3">Ultimos Episodios Postados</div>
    </div>';
}

See example on Ideone .

    
14.11.2014 / 21:02
5

As you have a comma at the end of the string, PHP will count 1, as two items, the first being 1 , and the second empty.

The ideal would be to organize the DB to not have this type of inconsistency, but for existing data, the trim() function comes in handy because it removes characters from the endpoints of the strings, so the result will work fine with or without comma:

<?php
   $episodios = trim( $episodios, ', ' );
   $contagem = explode( ',', $episodios);
   $count = count($contagem);

   if ($count == 1) { ?>
      <div class="separadores"><div class="separador3">Ultimo Episodio Postado</div></div>
<? }else { ?>
      <div class="separadores"><div class="separador3">Ultimos Episodios Postados</div></div>
<? } ?>


Optimizing the code:

Since what changes is only the plural, it is possible to simplify in several ways. Here's a readable thing:

<?php
   $episodios = trim( $episodios, ', ' );
   $contagem = explode( ',', $episodios);
   $pl = ( count($contagem) == 1 ) ? '' : 's';

   echo '<div class="separadores"><div class="separador3">';
   echo "Ultimo$pl Episodio$pl Postado$pl";
   echo '</div></div>';
?>

Thus, if singular, $pl is empty, and if it is plural, $pl gets the letter s . You can do it without using variables, simply by changing the contents of echo with the same logic.


If you prefer, use $count < 2 so that zero and one remain in the singular.

Discussion on the plural of "zero":
link
link

    
14.11.2014 / 20:53
3

Counting quantities on strings is not a very recommended way.

Ideally, you should place the ID of each episode in an Array. This way you check the length of the Array. If the length is greater than one you put in the plural.

If $ count is a vector, you can get its length with the function count , like this:

count($contagem);

This also makes it easier to handle the case where there is no episode (zero length).

Good luck!

    
14.11.2014 / 20:54
2
<?php
    $contagem = explode(',', $episodios);
    $count = count($contagem);
    $mensagem = $count > 2 ? "Ultimos Episodios Postados" : "Ultimo Episodio Postado"; 
    ?>
    <div class="separadores"> <div class="separador3"> <?php echo $mensagem; ?> </div></div>
    
14.11.2014 / 20:49
1

I want to suggest a more efficient approach that solves with a single line (good, more than one because of the readability here in the post) using preg_split () :

$parts = preg_split(

    '/(\d+),+/', $str, -1,

    PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);

With this, just count the returned array and make the condition.

Now the explanation.

The difference between using preg_split () and using explode () is that the maximum control that explode () offers is the amount of breaks it will perform.

Since preg_split () after breaking it already allows you to automatically filter empty values through bitmask flag PREG_SPLIT_NO_EMPTY combined as PREG_SPLIT_DELIM_CAPTURE

The pattern you are looking for is the number (% cos_de%) separated by a comma, but in the resulting array we only want the numbers, then we add a group ( \d+ ) so that the first flag has the to capture.

Only with this we already have a success break, but with several empty indexes and that's where the second flag comes in to ignore them.

And it does not matter if there is a comma after the last number in the list or not and if the string is badly formed with more than one comma between each number, the output will always be the same.     

15.11.2014 / 12:51