Monday, 4 January 2021

Perfect Ranking With Ties Using By PHP Array

 


<?php

#Ex:  Short Set Ranking Hosting Provider Founded By Year

// INPUT

$Hosting_Years = array(

    'Bluehost' => 2003,

    'Dreamhost' => 1996,

    'Hostinger' => 2004,

    'HostGator' => 2002,

    'A2 Hosting' => 2001,

    'GreenGeeks' => 2008,

    'WP Engine' => 2010,

    'Inmotion' => 2001,

    'SiteGround' => 2004,

    'Nexcess' => 2000,

);


function setRanking($standings) {

    $rankings = array();

    arsort($standings);

    $rank = 1;

    $tie_rank = 0;

    $prev_score = -1;

    foreach ($standings as $name => $score) {

        if ($score != $prev_score) {  //this score is not a tie

            $count = 0;

            $prev_score = $score; 

            $rankings[$name] = array('score' => $score, 'rank' => $rank);

        } else { //this score is a tie

            $prev_score = $score;

            if ($count++ == 0) {

                $tie_rank = $rank - 1;

            }

            $rankings[$name] = array('score' => $score, 'rank' => $tie_rank);

        }

        $rank++;

    }

    return $rankings;

}


// OUTPUT 

$set_Host_Ranking = setRanking($Hosting_Years);

foreach($set_Host_Ranking as $host => $value){

    echo "<br> Rank: ".sprintf("%03d", $value['rank']). " &emsp; Founded in  : ".$value['score']. "&emsp; Host: ".$host;

}

?>

According To Question:

#Ranking/Position on value of array in PHP

#Rank Of An Element In An Array

#What Is Rank Of An Array

#Array Ascending Order In PHP

#PHP Sort Multidimensional Array By Value Alphabetically

#Ksort Multidimensional Array PHP

#PHP Sort Multidimensional Array By Key

Sunday, 3 January 2021

Formatting A Number With Leading Zeros In PHP


<?php

#Ex:  Make 4 Digit Integer Or Zerofill An Integer In PHP

// INPUT

$start_number = 1;

$end_number = 100;

for($i=$start_number; $i <= $end_number; $i++ ){

    echo "<br> #: ".sprintf("%04d",  $i);  

}

// OUTPUT

#: 0001

#: 0002

#: 0003

#: 0004

#: 0005

#: 0006

#: 0007

#: 0008

#: 0009

#: 0010

.

.

.

#: 0100

?> 

According To Question:

#add zero after number in php

#php number format: 0001, 0002, 0003 ...

#make number 2 digit php

#php leftpad number

#php force two digit number

#php sprintf

#str_pad in php

#php left fill

Friday, 1 January 2021

How Can I Remove A Specific Item From An Array by jQuery?




//INPUT
var array = [2020, 2021, 2022, 2023];

console.log(array);

var index = array.indexOf(2020);
if (index > -1) {
  array.splice(index, 1);
}

// OUTPUT  : array = [2021, 2022, 2023]
console.log(array);

Note: Run This Code In Browser Console For Testing

According To Question:
#Remove Specific Element From Array Javascript
#Javascript Remove Object From Array By Value
#Remove Element From Array Jav
#Array Filter
#Splice Javascript
#Jquery Remove Item From Array By Index
#Lodash Remove Item From Array
#Remove Object From Json Array Javascript

Thursday, 10 December 2020

Create A Folder If It Doesn't Already Exist In WordPress Or PHP


 <?php 

if (!file_exists('home/public_html/example.com/wp-content/uploads/blog')) {

    mkdir('home/public_html/example.com/wp-content/uploads/blog', 0777, true);

}

?>


According To Question:

#mkdir php

#check if directory exists php

#how to create directory dynamically in php

#create directory in php file upload

#create folder inside folder in php

#php mkdir not working

#php mkdir 777

#php mkdir()

Friday, 23 October 2020

WordPress Post Views Count Without Plugin

 Step1"
Add this code in current WordPress active theme in function.php file

<?php

function get_PostViews($postID){
    $count_key = 'post_views_count';
    $post_count = get_post_meta($postID, $count_key, true);
    if($post_count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $post_count.' Views';
}

function set_PostViews($postID) {
    $count_key = 'post_views_count';
    $post_count = get_post_meta($postID, $count_key, true);
    if($post_count==''){
        $post_count = 0;
       delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $post_count++;
        update_post_meta($postID, $count_key, $post_count);
    }
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
?>

 Step2:
Now add this function code in active themes in single.php file within the loop. It will manage the views and set the post views of each post.

<?php
 set_PostViews(get_the_ID());
?>

 Step3:
Add this code line in inside the post loop where you want to display post count.

<?php
 echo get_PostViews(get_the_ID());
 ?>