Skip to content

Instantly share code, notes, and snippets.

@ijmorgado
ijmorgado / import_csv.sql
Created January 27, 2013 07:29
That's a small sql script to import a csv file without headers...just a bunch of data...I don't know why I had a trouble with the server when I tried other samples....
LOAD DATA local INFILE '/path/to/file.csv'
INTO TABLE name_scheme.name_of_table
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
IGNORE 0 LINES
(field1,field2,field3,field4);
@ijmorgado
ijmorgado / zf2_join.php
Created January 28, 2013 04:58
That's a small function for a Table Gateway model that executes a SQL Join between data of 2 tables and returns the resultset as an array....simply but useful when we work with multiples tables and relationships......It works for ZF2....
public function getOrderedByCategory(){
$sql = new Sql($this->tableGateway->getAdapter());
$select = $sql->select('alb_album')->where('alb_album.activo = 1 ORDER BY alb_album.id_categoria ASC');
$join = $select->join('alb_categoria','alb_album.id_categoria = alb_categoria.id_categoria',array('nombre_categoria'=>'nombre'));
$statement = $sql->prepareStatementForSqlObject($join);
$result = $statement->execute();
$rows = array_values(iterator_to_array($result));
return $rows;
}
@ijmorgado
ijmorgado / create_thumbnail_function.php
Last active February 12, 2019 01:49
This is a function to create thumbnail image(png, jpeg, jpg or gif) with specific height...is useful when we need to make tables with the same height for each row with image previews...if you really need the same width and whatever height, just change the order of params in the aspect ratio operations (lines 17 and 18)...
public function createThumbnail($source_folder, $thumbs_folder, $source_file, $extension, $thumbHeight){
if ($extension == 'gif') {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
}else if($extension == 'jpg' || $extension == 'jpeg'){
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
}else if ($extension == 'png') {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
@ijmorgado
ijmorgado / zf_route.php
Last active December 11, 2015 21:58
This is a snippet of route in ZF2 to handle url's like /controller/action, /controller/action/param1/value1, /controller/action/param1/value1/param2/value2...perhaps that's not the best way to do that but comming soon I would like to handle url's with undefined number of params with just one routing rule....
<?php
// ...the other options before.....
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
@ijmorgado
ijmorgado / modx.tv.image.resourcefield.html
Created February 5, 2013 04:42
This is a way that I've used to retrieve an image template variable with output type=text and filtered by phpthumbof on the fly.....Very useful when we have tv's and wants to use the same image in different places and sizes.....
<img src="[[getResourceField:phpthumbof=`w=444&h=284&zc=1`? &id=`[[*slide1_home]]` &isTV=`1` &processTV=`1` &field=`imagen_nota`]]" alt="Imagen1" />
@ijmorgado
ijmorgado / date_validator.php
Created February 9, 2013 20:21
This is a small snippet to check if a string is a correct date.
$compare = "31-10-1991";
// first validate the format...it could be: 24-05-2013,24/05/2013,24.05.2013,24-5-2013,24.5.2013,24/5/2013...or something like that(at this point we've just validated the format...not the date)
if(preg_match('/^(3[0-1]|[0-2]?[0-9])[\/.-](1[0-2]|0?[0-9])[\/.-][0-9]{4}$/',$compare)){
$date_parsed = preg_split('/[\/.-]/', $compare);
//if the format is correct we're going to see if the date is valid....for example: 31-02-2013 should be invalid....
if(!checkdate($date_parsed[1],$date_parsed[0],$date_parsed[2])){
@ijmorgado
ijmorgado / sql_like.php
Created February 25, 2013 05:15
This is another really good way to make a custom sql query in ZF2 with table gateway in the model...in this case a LIKE statement... :)
$adapter = $this->tableGateway->getAdapter();
$select = new Select();
$select->from('tax_tags')
->limit(5)
->where->like('tag', "%".$query."%");
$statement = $adapter->createStatement();
$select->prepareStatement($adapter, $statement);
$result = $statement->execute();
@ijmorgado
ijmorgado / or_predicate_with_like.php
Created February 26, 2013 23:18
This is a small piece of code to create a select statement with 2 like clauses connected by "OR". It was made in ZF2. :)
$adapter = $this->tableGateway->getAdapter();
$select = new Select();
$select->from('alb_imagen')
->columns(array('itemId' => 'id_imagen',
'value' => 'titulo'))
->limit(12)
->join(array('t' => 'alb_thumbnail'),'t.id_imagen = alb_imagen.id_imagen',array('imagen' => 'nombre'))
->where('t.tipo = \'MINI_ICON50X50\' and alb_imagen.activo =1')
->where(array(new PredicateSet(array(new Like('titulo', "%".$query."%"),
new Like('descripcion', "%$query%")
@ijmorgado
ijmorgado / imagecreatefrombmp.php
Created March 2, 2013 22:38
Currently php doesn't have support to create images from bmp sources, this is a small function that solves that problem...extracted from php docs forums (http://www.php.net/manual/en/function.imagecreate.php#53879)
public function ImageCreateFromBMP($p_sFile)
{ $file = fopen($p_sFile,"rb");
$read = fread($file,10);
while(!feof($file)&&($read<>""))
$read .= fread($file,1024);
$temp = unpack("H*",$read);
$hex = $temp[1];
$header = substr($hex,0,108);
if (substr($header,0,4)=="424d")
{
@ijmorgado
ijmorgado / git_commands
Last active December 15, 2015 09:49
A small list of most common git commands I use (Is really important keep it saved, because sometimes it save us time of search on official documentations and forums.... :))
----- git rm -r --cached <folder_name>
It discard of git index but not from local working copy...useful when by accident we pushed something to remote repository and then we have to remove it from that repo.
----- git remote set-url <remote_name> <new_url_here>
It can set a new url for the current repository, useful when we change the name of the repository on Github for example, as Github also change the url for this.