QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Storing Key Value Pairs in CSV using PHP

Storing Key Value Pairs in CSV using PHP

In this tutorial I will provide code to store key/value pair data in a CSV file and access it. If you are familiar with WordPress then you must have used update_option(), get_option() and delete_option() to store key/value pairs, in this tutorial I will provide an implementation of these functions for PHP.

In WordPress key/value pairs are stored in MySQL but in this tutorial we will be storing them in a CSV file therefore you can use them without MySQL also.

get_option, update_option and delete_option Functions

Here is the implementation code for these functions

<?php
   
    function update_option($name, $value)
    {
        if(file_exists("php-options.csv"))
        {
            $file = fopen("php-options.csv","r");

            $exists = false;
            $csv = "";

            while(!feof($file))
            {
                $data_array = fgetcsv($file);

                if($data_array[0] == $name)
                {
                    $exists = true;
                }
                else
                {
                    $csv = $csv . $data_array[0] . "," . $data_array[1] . "\n";
                }
            }

            fclose($file);

            $csv = substr($csv, 0, -2);

            if($exists)
            {
                $csv = $csv . $name . "," . $value . "\n";
                file_put_contents("php-options.csv", $csv);
            }
            else
            {
                $csv = file_get_contents("php-options.csv") . $name . "," . $value . "\n";
                file_put_contents("php-options.csv", $csv);
            }
        }
        else
        {
            $csv = $name . "," . $value . "\n";
            file_put_contents("php-options.csv", $csv);
        }
    }

    function get_option($name)
    {
        $file = fopen("php-options.csv","r");

        while(!feof($file))
        {
            $data_array = fgetcsv($file);

            if($data_array[0] == $name)
            {
                fclose($file);
                return $data_array[1];
            }
           
        }

        fclose($file);
        return null;
    }

    function delete_option($name)
    {
        if(file_exists("php-options.csv"))
        {
            $file = fopen("php-options.csv","r");
            $csv = "";

            while(!feof($file))
            {
                $data_array = fgetcsv($file);

                if($data_array[0] == $name)
                {}
                else
                {
                    $csv = $csv . $data_array[0] . "," . $data_array[1] . "\n";
                }
            }

            fclose($file);

            $csv = substr($csv, 0, -2);

            file_put_contents("php-options.csv", $csv);
        }
    }

delete_option is used to delete keys if its present in the CSV file.

update_option is used to update value of a key. If key is not present then it creates a key.

get_option is used to retrieve value of a key. It returns null if key is not found.

Code Example

Here is an example of how to call the above functions

<?php
    update_option("Name", "Narayan Prusty");
    echo get_option("Name");
    delete_option("Name");
Dec 27, 2014Narayan Prusty
Find Admin Menu Item's Page URL in WordPressTwitter Auto Favorite WordPress Plugin

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

8 years ago Web Development
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
  • get_option, update_option and delete_option Functions
  • Code Example
Related Articles
  • How to Store Objects in HTML5 localStorage
  • How to Store Arrays in HTML5 localStorage
  • Retrieve MySQL Queries Programatically in WordPress
  • wp_load_alloptions() Function
  • WordPress $notoptions Array
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license