Sony Rawat
Sony Rawat

Reputation: 1

how can i generate a form contaning all field from a excel sheet in php

Hi I have a excelsheet contaning column name and salary with multiple data now I want to generate a form contaning these field from excel sheet respective to each rows. please help me how can I do this in php.I do not want to use mysql for this.please help.

Upvotes: 0

Views: 435

Answers (3)

Xavier Barbosa
Xavier Barbosa

Reputation: 3947

if you are able to use csv files, you can use fgetcsv and fputcsv : http://www.php.net/manual/fr/function.fgetcsv.php

check this sample.

<?php

define('INPUT_FILENAME', '/path/to/your/csv/file');

define('OUTPUT_FILENAME', '/path/to/your/csv/file');
define('SEPARATOR', ',');
define('ENCLOSURE', '"');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['entries'])) {
        $entries = $_POST['entries'];
    }

    // do the validation stuff here ...

    if ($ok) {
        // save the entries into the DESTINATION file

        $handle = fopen(OUTPUT_FILENAME, 'w');

        foreach ($entries as $entry) {
            $fields = array(
                $entry['name'],
                $entry['salary']
            );
            fputcsv($handle, $fields, SEPARATOR, ENCLOSURE);
        }

        exit('done');
    }
}

if (!isset($entries)) {
    // fetch the entries
    $entries = array();
    if (($handle = fopen(INPUT_FILENAME, 'r')) !== false) {
        while (($row = fgetcsv($handle, 1000, SEPARATOR, ENCLOSURE)) !== false) {
            list($name, $salary) = $row;
            $entries[] = array(
                'name' => $name,
                'salary' => $salary
            );
        }
        fclose($handle);
    }
}

// display the form

print '<form action="#" method="post">';

foreach ($entries as $i => $entry) {
    print '<fieldset>';
    print '<label>Name';
    printf('<input type="text" name="entries[%s]name" value="%s" />', $i, htmlspecialchars($entry['name']));
    print '</label>';
    print '<label>Name';
    printf('<input type="text" name="entries[%s]salary" value="%s" />', $i, htmlspecialchars($entry['salary']));
    print '</label>';
    print '</fieldset>';
}
print '<input type="submit" />';
print '</form>';

And one form per entry :

<?php

define('INPUT_FILENAME', '/path/to/your/csv/file');

define('OUTPUT_FILENAME', '/path/to/your/csv/file');
define('SEPARATOR', ',');
define('ENCLOSURE', '"');

$entries = array();

// Allways prefetch entries
if (($handle = fopen(INPUT_FILENAME, 'r')) !== false) {
    while (($row = fgetcsv($handle, 1000, SEPARATOR, ENCLOSURE)) !== false) {
        list($name, $salary) = $row;
        $entries[] = array(
            'name' => $name,
            'salary' => $salary
        );
    }
    fclose($handle);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $position = null;
    if (isset($_POST['position'])) {
        $position = (int) $_POST['position'];
    }

    $name = null;
    if (isset($_POST['name'])) {
        $name = $_POST['name'];
    }

    $salary = null;
    if (isset($_POST['salary'])) {
        $salary = $_POST['salary'];
    }


    // do the validation stuff here ...

    if ($ok) {
        // merge the entry into the list
        $entries[$position] = array(
            'name' => $name,
            'salary' => $salary
        );

        // save the entries into the DESTINATION file

        $handle = fopen(OUTPUT_FILENAME, 'w');

        foreach ($entries as $entry) {
            $fields = array(
                $entry['name'],
                $entry['salary']
            );
            fputcsv($handle, $fields, SEPARATOR, ENCLOSURE);
        }

        exit('done');
    }
}

// display

foreach ($entries as $i => $entry) {
    // one form per entry
    print '<form action="#" method="post">';
    print '<fieldset>';
    print '<label>Name';
    printf('<input type="text" name="name" value="%s" />', htmlspecialchars($entry['name']));
    print '</label>';
    print '<label>Name';
    printf('<input type="text" name="salary" value="%s" />', htmlspecialchars($entry['salary']));
    print '</label>';
    printf('<input type="hidden" name="position" value="%s"', $i);
    print '</fieldset>';
    print '<input type="submit" />';
    print '</form>';
}

Upvotes: 0

shikhar
shikhar

Reputation: 2469

Save the file as a webpage and then write a little bit of PHP script in it. If you don't want mysql, use Sqlite to store the values. Sqlite will be just like a file.

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212412

To start with, you need to be able to read the Excel data. Which version of Excel? xls or xlsx? I'd suggest using the PHPExcel library for this, although there are alternatives.

include 'PHPExcel/IOFactory.php';

$inputFileName = './sampleData/example1.xls';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

will give you the worksheet data in a standard PHP array.

Upvotes: 1

Related Questions