KIT - Vsechno spravne.
Prvni pouziti pdo, takze zatim nemam moc prehled, jak napsat, co potrebuji a jak to scrozumitelne pojmenovat.
Ano, volajici musi znat strukturu. Tudiz to muze byt pro kazdy program trochu jinaci, treba, kdybych chtel logovat automaticky, kdo ten radek menil a tak. to byl ucel. A taky, aby se to dalo zamenit mysql prikazy. Z meho pohledu si PDO vymyslelo strasne nesrozumitelnou syntaxi, zvlast pro novacka. Nevim, co je zkratka SMTH, ale vim, co je slovo result nebo handle.
Příspěvky odeslané z IP adresy 2001:718:2601:1f7:a9f9:41...–
Serazovani muzes resit takto
// --- sort - multi colunm ---
function arraySortMulti(&$arr,$order=array())
{
$func_conv = function($s) {return (float) preg_replace('~[^.0-9]~',"", (string) $s);};
$func_sort = array(
'STRING' => function($a,$b){if ($a < $b) {return -1;}; if ($a > $b) {return 1;}; if ($a == $b) {return 0;}; return false;},
'STRNUM' => function($a,$b) use ($func_conv) {return $func_conv($a) - $func_conv($b);},
'NUMERIC' => function($a,$b){return $a - $b;}
);
$func_order = array(
'ASC' => function($q){return $q;},
'DESC' => function($q){return -$q;}
);
usort($arr, function ($a, $b) use ($order,$func_sort,$func_order)
{
$result = 0;
foreach ($order as $key => $value)
{
$o = isset($value['order']) && isset($func_order[$value['order']]) ? $func_order[$value['order']] : $func_order['ASC'];
$s = isset($value['sort']) && isset($func_sort[$value['sort']]) ? $func_sort[$value['sort']] : $func_sort['STRING'];
$result = $o($s($a[$key],$b[$key]));
if ($result!==0)
{
return $result;
}
}
return $result;
});
return $arr;
}
// ---
$order = array(
// 2 => array('order'=>'DESC','sort'=>'NUMERIC'),
1 => array('order'=>'DESC','sort'=>'STRNUM'),
0 => array('order'=>'ASC' ,'sort'=>'STRING')
);
$files = array(
array(0=> 'nazev', 1=> 'datum', 2=>'velikost')
)
arraySortMulti($files,$order);
Ty tam sice takove pole nemas, ale jiste si to svedes upravit pro tvuj pripad. Ze to treba explodujes podle _ a porovnas rok, mesic, den, hodinu - v tomto poradi
Nebo, proste muzes cyklem smazat rovnou :) opet pouzijes explode a porovnavas rok, mesic, den, hodin
Nebo, kdyz to mas serazene, tak to datum muzes najit rucne, ne? a vse nad nim smazes.
}
Nn, ja nemyslim odtrhnout dotaz z mista pouziti. Jen si proste na sql postavit vlastni tridu. Jestli je uvnitr prikaz pdo nebo mysql je pak jedno. Kdyz to budes chtit pak upgradovat, tak staci prepsat tu tridu. Sice je to mozna trochu pracnejsi to odladit nez resit na miste, ale jevi se mi to elegantnejsi.
Treba, ja ted mam neco takoveho. Neni to asi jeste vsechno vyladne. Ale, kde to pouzivam, tam mi to zatim staci.
<?php
class classSql
{
public $conn = null;
//public $db = null;
public $sth = null;
public $sth_exec = null;
private $cfg = null;
//private $log = null;
function __construct($params)
{
$this->createConfig($params);
$this->connect();
$this->query("SET NAMES utf8");
// $this->log = array(
// 'logdate'=>'logdate',
// 'loguser'=>'loguser'
// );
}
function __destruct()
{
$this->disconnect();
}
function createConfig($params)
{
$cfg = array(
'host' => '',
'user' => '',
'psw' => '',
'db' => '',
'debug' => false
);
foreach($cfg as $key=>$value)
{
$cfg[$key] = isset($params[$key]) ? $params[$key] : $value;
}
//$cfg['query'] = ;
$this->cfg = $cfg;
// if ($this->cfg['debug'])
// {
// $this->query = function ($query) {$this->queryDebug($query);};
// }
// else {
// $this->query = function ($query) {$this->querySafe($query);};
// }
//var_dump($this->query);
// return (!$this->cfg['debug']) ? $this->query1($query) : $this->query2($query);
//echo $this->query = $this->($this->query);
// $this->query = $this->{$this->query};
//($query);
//function($query) {}
}
function connect()
{
//phpinfo();
$this->conn = new PDO('mysql:host='.$this->cfg['host'].';dbname='.$this->cfg['db'], $this->cfg['user'], $this->cfg['psw'])
or $this->error();
// $x = $this->cfg['query'];
// $x = $this->{$x};
// $x($query);
}
public function disconnect()
{
if (isset($this->conn))
{
$this->conn = null;
}
}
// function free($result)
// {
// mysql_free_result($result);
// }
public function query($query)
{
// $x = $this->cfg['query'];
// $x = $this->{$x};
// $x($query);
if ($this->cfg['debug'])
{
return $this->queryDebug($query);
}
else {
return $this->querySafe($query);
}
}
public function querySafe($query)
{
$this->sth = $this->conn->prepare($query);
$this->sth_exec = $this->sth->execute();
return $this->sth;
}
public function queryDebug($query)
{
echo "\n<div class=\"query\">query = ".$query."</div>";
$this->querySafe($query)
or die('Chyba pdo_query: ' . $this->sth->errorCode().' '.implode(" - ",$this->sth->errorInfo()));
}
public function fetch($sth=null)
{
// var_dump( $this->sth->fetch(PDO::FETCH_ASSOC)); //$sth
$sth = !$sth ? $this->sth : $sth;
return $sth->fetch(PDO::FETCH_ASSOC); //$sth
}
// public function numRows($sth)
// {
// return $sth->query('SELECT FOUND_ROWS()')->fetchColumn();
// }
public function createInsert($_table,$_data,$_idname='')
{
if ($_idname!='' && isset($_data[$_idname])) {unset($_data[$_idname]);}
$keys = array();
$values = array();
foreach ($_data as $key=>$value)
{
$keys[] = $this->escapeKey($key);
$values[] = $this->escape($value);
}
return "INSERT INTO ".$this->escapeKey($_table)." (".implode(", ",$keys).") VALUES (".implode(", ",$values).")";
}
public function createUpdate($_table,$_data,$_idname)
{
$_id = array($_idname=>$_data[$_idname]);
unset($_data[$_idname]);
$values = array();
$where = array();
foreach ($_data as $key=>$value)
{
$values[] = $this->escapeKey($key)."=" . $this->escape($value);
}
foreach ($_id as $key=>$value)
{
$where[] = $this->escapeKey($key)."=" . $this->escape($value);
}
return "UPDATE ".$this->escapeKey($_table)." SET ".implode(", ",$values)." WHERE ".implode(' AND ',$where);
}
public function createDelete($_table,$_id)
{
$where = array();
foreach ($_id as $key=>$value)
{
$where[] = $this->escapeKey($key)."=" . $this->escape($value);
}
return "DELETE FROM ".$this->escapeKey($_table)." WHERE ".implode(' AND ',$where);
}
public function escape($value='')
{
return $this->conn->quote($value);
}
public function escapeKey($value='')
{
return "`".str_replace("`","``",$value)."`";
}
public function escapeSelect($value='')
{
$m = strlen($value);
$value = preg_replace('~UPDATE|REPLACE|DELETE|CREATE~i','',$value);
$n = strlen($value);
return $m==$n ? $value : '';
}
};
/*
$sql = new classSql(array(
'host' => 'localhost',
'user' => 'root',
'psw' => '',
'db' => 'urb_news',
'debug' => true
));
*/
?>
Problem byvaji obvykle float a position v css/style. Vse ostatni umi zkontrolovat validator. Pripadne si najdi pro svuj prohlizec doplnek validator html.
https://validator.w3.org/
Je dobre si udelat tridu, ktera se stara jen o sql. Kdyz uvazis kolik tech sql ruznych je, abys to nemusel pozdeji prepisovat v celem programu.