Mostrando las entradas con la etiqueta PHP. Mostrar todas las entradas
Mostrando las entradas con la etiqueta PHP. Mostrar todas las entradas

martes, 2 de marzo de 2010

Comparando fechas

Este post es uno de esos que me evitarán la hueva de buscar este código de nueva cuenta:

$exp_date = "2006-01-16"; 
$todays_date = date("Y-m-d"); 

$today = strtotime($todays_date); 
$expiration_date = strtotime($exp_date); 

if ($expiration_date > $today) {
     $valid = "yes";
} else {
     $valid = "no";
}

lunes, 23 de febrero de 2009

Outputting images in a directory in alphabetic file order


function print_gallery($directorio) {
$dir_galerias="/images/galerias/";
$dir=$_SERVER['DOCUMENT_ROOT'].$dir_galerias.$directorio;

if (is_dir($dir)) {
if ($gd = opendir($dir)) {
while (($archivo = readdir($gd)) !== false) {
if ($archivo != '.' && $archivo !='..') $pics[]=$archivo;
}
closedir($gd);
}
}
sort($pics);


for ($c = 0; $c < count($pics); $c++) {
echo "<p align='center'><img src='$dir_galerias".$directorio."/".$pics[$c]."' /><p>";
}

//print_r($pics);

}

miércoles, 26 de noviembre de 2008

Paginate, paginate, paginate


Paginate your queries, be happy, be hippie...


<?

$docs=10;

if (empty($_GET[pg])) { $_GET[pg]=1; }
if ($_GET[pg] < 1) { $pg=0; } else { $pg=$_GET[pg]-1; }

$ini=$pg*$docs;
$fin=$ini+$docs;

$sel_docs="SELECT id_doc,titulo FROM docs ORDER BY id_doc DESC ";
if (isset($_GET[query]) && $_GET[query]!='') {
$sel_docs.="WHERE titulo LIKE '%".$_GET[query]."%'";
}
$sel_docs.=" LIMIT $ini,$fin";

$qdocs=mysql_query($sel_docs);

while ($rdocs=mysql_fetch_array($qdocs)) {
/// do your stuff
}


echo "<p align='center'>";
/////////////
$sql_count="SELECT id_doc FROM docs ";
if (isset($_GET[query]) && $_GET[query]!='') { $sel_docs.="WHERE titulo LIKE '%".$_GET[query]."%'"; }
$q_count=mysql_query($sql_count);
$num=mysql_num_rows($q_count); // cuántos artículos hay

$anterior=$_GET[pg]-1;
$siguiente=$_GET[pg]+1;
$pg_max=ceil($num/$docs); // número máximo de páginas

$num_list=24; // número de páginas listadas -- de 0 a $num_list

if ($pg!=0) {
echo "<a href='index.php?p=".$_GET[p]."&pg=".$anterior."&query=".$_GET[query]."'>Anterior</a> ";
}

/// define desde que página inicia la numeración
if (empty($_GET[query])) {
if ($_GET[pg]<=10) {
$start_at=1;
} else if ($_GET[pg]>($pg_max-$num_list)) {
$start_at=$pg_max-$num_list;
} else {
$start_at=$_GET[pg]-10;
}
}

$pg_tmp=$start_at;
if ($num_list>$pg_max) { $num_list=$pg_max; }

//////////// paginado
for ($i = 0; $i < $num_list; $i++) {
if ($pg_tmp==$_GET[pg]) { echo "<b>$pg_tmp</b> | ";
} else {
echo "<a href='index.php?p=".$_GET[p]."&pg=".$pg_tmp."&query=".$_GET[query]."'>".$pg_tmp."</a> | "; }
$pg_tmp++;
}

if ($_GET[pg]!=$pg_max) {
echo "<a href='index.php?p=".$_GET[p]."&pg=".$siguiente."&query=".$_GET[query]."'>Siguiente</a> ";
}

echo "</p>";


?>


sorry for the spanglish...

sábado, 22 de noviembre de 2008

Great PHP image resize code 4 thumbnails


This works just great!! check it out. Thumbs are created from those very images displayed with lightbox!

This will resize your image but it won't stretch it and it'll keep right proportions. Basically this crops your image to fit the best.

function createThumb($source,$dest) {
$thumb_size = 230; // square thumbs
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];

// you might comment this to make regular thumbs and add a thumb width var. keep this for square thumbs
if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} else if ($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}

$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
imagejpeg($new_im,$dest,100);
}