<?php

error_reporting(1);
ini_set('max_execution_time', 0);
ini_set('post_max_size', '64M');
ini_set('upload_max_filesize', '64M');

if (!session_start()) {
    session_start();
}


function dblink()
{
    $server = 'localhost';
    $user   = 'gdhhsxmy_mawa';
    $pwd    = 'gdhhsxmy_mawa';
    $dbname = 'gdhhsxmy_mawa';

    return $con = mysqli_connect('localhost', $user, $pwd, $dbname);
}

function get_option($table, $field = 'name', $were = 'id!=0')
{
    $datais[0] = 'Select';

    $data = findQuery("select $field,id from $table where $were");
    for ($i = 0;$i < count($data);$i++) {
        $datais[$data[$i]['id']] = $data[$i][$field];
    }

    return $datais;
}

function get_option_without_select($table, $field = 'name', $were)
{
    $data = findQuery("select $field,id from $table where $were");
    for ($i = 0;$i < count($data);$i++) {
        $datais[$data[$i]['id']] = $data[$i][$field];
    }

    return $datais;
}


function findQuery($sql = '', $link = '')
{
    $fieldtype  = [];
    $fieldvalue = [];
    $type       = [];
    $data       = [];

    if (!$link) {
        $link = dblink();
    }
    $rs           = mysqli_query($link, $sql) ;
    $countrow     = mysqli_num_rows($rs);
    $countoffield = mysqli_num_fields($rs);
    $finfo        = $rs->fetch_fields();

    foreach ($finfo as $val) {
        array_push($fieldtype, $val->name);
        array_push($type, $val->type);
    }
    for ($i = 0;$i < $countrow;$i++) {
        $fieldvalue[$i] = mysqli_fetch_row($rs);
        $data[$i]       = array_combine($fieldtype, $fieldvalue[$i]);
    }

    return $data;
}

function san_input($input)
{
    $link  = dblink();
    $input = mysqli_real_escape_string($link, $input);

    return $input;
}

function saveArray($table, $arr, $id = 0, $link = '')
{
    $fieldis   = keyvalueis($arr);
    $valueis   = keyvalueis($arr, 1);
    $stordata  = implode(',', $valueis);
    $fieldname = implode(',', $fieldis);

    if (!$link) {
        $link = dblink();
    }

    if ($id == 0) {
        $sql = "insert into $table($fieldname)values($stordata)";
        mysqli_query($link, $sql) or die(mysqli_error($link));

        return mysqli_insert_id($link);//$lastid = lastid();
    } else {
        updateArray($table, $fieldis, $valueis, $id);

        return $id;
    }
}

function keyvalueis($arr, $t = 0)
{
    $rarry = [];

    foreach ($arr as $key => $value) {
        $value = "'" . mysql_ready($value) . "'"  ;

        if ($t == 0) {
            $value = $key;
        }array_push($rarry, $value);
    }

    return $rarry;
}

function mysql_ready($var)
{
    $link = dblink();
    $var  = mysqli_real_escape_string($link, $var);

    return $var;
}


function updateArray($table, $f, $v, $id)
{
    $link = dblink();
    for ($i = 0;$i < count($f);$i++) {
        $key    = $f[$i];
        $uvalue = $v[$i];
        $query  = "update $table set $key=$uvalue where id=$id";
        mysqli_query($link, $query) ;
    }
}

function updateQuery($query)
{
    $link = dblink();
    mysqli_query($link, $query) ;
}
