I have a movie website and I want to count the links of each box but the first the count(); output is 0 and after that it will continue the real numbers and it’s terrible this is my code:
<div class="links" style="display: none;">
<?php
$c = 1;
$post_something = $link['links'];
//This will count the number of links
$number = count($post_something);
foreach($post_something as $links){
//This is the output of the count ?>
<p><?php echo $number ?></p>
<a class="download-button" href="<?php echo $links['link_of_field'] ?>">
<?php
if(!empty($links['name_field'])){
echo $links['name_field'];
$c++;
} else {?>
قسمت <?php echo $c;
$c++;
?>
<?php }
?>
</a>
<? }
?>
</div>
</div>
<?php }?>
The problem is at the first it has the output of 0!
Here is the resault of var_dump
array(6) {
[0]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E01_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” }
[1]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E02_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” }
[2]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E03_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” }
[3]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E04_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” }
[4]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E05_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” }
[5]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E06_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” } }
array(6) {
[0]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E01_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” }
[1]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E02_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” }
[2]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E03_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” }
[3]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E04_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” }
[4]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E05_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” }
[5]=> array(2) { [“name_field”]=> string(0) “” [“link_of_field”]=> string(176) “dl/1/354830/4272/12426/303e08ccc88e2dd4428117619f226fe19a/series/game_of_thrones/Game_of_Thrones_S08E06_10bit_x265_720p_WEBRip_2CH_PSA_30NAMA.mkv” } }
Well, technically, in computer language, all counts start from [0]
and not [1]
. So there is nothing wrong with your code. It is working fine if it starts from 0.
This line $number = count($post_something);
in your code should return the actual number of results returned.
Let me try to explain with an example;
In a code like this;
<?php
$cars=array("Volvo","BMW","Toyota");
$totalCount = count($cars);
print_r($cars); // this will print Array ( [0] => Volvo [1] => BMW [2] => Toyota )
echo $totalCount; // this will return 3
?>
so in the example above, the array prints from 0 but the count produces the number of items in the array which is 3.
I hope this clarifies things for you. If not am willing to explain further or sort this out with you.