Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
PHP
<?php


/*
 * examples/mysql/loaddata.php
 *
 * This file is part of EditableGrid.
 * http://editablegrid.net
 *
 * Copyright (c) 2011 Webismymind SPRL
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://editablegrid.net/license
 */



/**
 * This script loads data from the database and returns it to the js
 *
 */

require_once('config.php');
require_once('EditableGrid.php');

/**
 * fetch_pairs is a simple method that transforms a mysqli_result object in an array.
 * It will be used to generate possible values for some columns.
*/
function fetch_pairs($mysqli,$query){
    if (!($res = $mysqli->query($query)))return FALSE;
    $rows = array();
    while ($row = $res->fetch_assoc()) {
        $first = true;
        $key = $value = null;
        foreach ($row as $val) {
            if ($first) { $key = $val; $first = false; }
            else { $value = $val; break; }
        }
        $rows[$key] = $value;
    }
    return $rows;
}


// Database connection
$mysqli = mysqli_init();
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
$mysqli->real_connect($config['db_host'],$config['db_user'],$config['db_password'],$config['db_name']);

// create a new EditableGrid object
$grid = new EditableGrid();

/*
*  Add columns. The first argument of addColumn is the name of the field in the databse.
*  The second argument is the label that will be displayed in the header
*/
$grid->addColumn('id', 'ID', 'integer', NULL, false);
$grid->addColumn('name', 'Name', 'string');
$grid->addColumn('firstname', 'Firstname', 'string');
$grid->addColumn('age', 'Age', 'integer');
$grid->addColumn('height', 'Height', 'float');
/* The column id_country and id_continent will show a list of all available countries and continents. So, we select all rows from the tables */
$grid->addColumn('id_continent', 'Continent', 'string' , fetch_pairs($mysqli,'SELECT id, name FROM continent'),true);
$grid->addColumn('id_country', 'Country', 'string', fetch_pairs($mysqli,'SELECT id, name FROM country'),true );
$grid->addColumn('email', 'Email', 'email');
$grid->addColumn('freelance', 'Freelance', 'boolean');
$grid->addColumn('lastvisit', 'Lastvisit', 'date');
$grid->addColumn('website', 'Website', 'string');

$result = $mysqli->query('SELECT *, date_format(lastvisit, "%d/%m/%Y") as lastvisit FROM demo LIMIT 100');
$mysqli->close();

// send data to the browser
$grid->renderXML($result);
Posted

See past answers[^] and this trip: .NET Code Conversion - Convert your code[^].

I'd suggest to understand what code does and re-write it in c#. See php documentation[^].
 
Share this answer
 
 
Share this answer
 
Comments
Maciej Los 25-Apr-15 18:10pm    
Rajeesh, i won't say your answer is wrong, but both links referenced in your answer are referenced in ".NET Code conversion ..." article i added in my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900