Jump to content

Sort by date query


peat

Recommended Posts

Hi,

I am a php complete newbie. I basically know nothing.  I have a gallery website that has a search facility and im trying to work out how to amend the php so the results are shown by newest photo first.

This is the code.  I dont even know where to start looking.  I looked at the usort section but I didnt see anything that made sense to me to amend.

Can anyone help?

Cheers

 

<?php

//****************************************************************************************
// FotoPlayer Search Engine v0.1
// Author : Dhinakaran Annamalai
// Protected under Creative Commons License. Attribution 2.0 United Kingdom
// Please refer readme.txt file for the usage details.
//****************************************************************************************

error_reporting(E_ALL ^ E_NOTICE);

global $albumpath,$itemcounts,$searchfor,$searchfound,$searchtags,$searchtitle,$searchdesc,$searchmeta,$searchmode;

$searchfor ="     ";$searchtags ="Y";$searchtitle="N";$searchdesc="N";$searchmeta="N";$searchmode="";


if (isset($_REQUEST["searchtags"])) {
$searchtags= $_REQUEST['searchtags'];
}

if (isset($_REQUEST["searchtitle"])) {
$searchtitle= $_REQUEST['searchtitle'];
}

if (isset($_REQUEST["searchdesc"])) {
$searchdesc= $_REQUEST['searchdesc'];
}

if (isset($_REQUEST["searchmeta"])) {
$searchmeta= $_REQUEST['searchmeta'];
}


if (isset($_REQUEST["searchmode"])) {
$searchmode= $_REQUEST['searchmode'];
}


if (isset($_REQUEST["searchfor"])) {
$searchfor = utf8_decode($_REQUEST['searchfor']);
$searchfor = strip_tags($searchfor);
$searchfor = stripslashes($searchfor);
$searchfor = str_replace(" +"," ",$searchfor);
    $searchfor = str_replace("'","",stripslashes($searchfor));
    $searchfor = str_replace('"','',stripslashes($searchfor));
$searchfor = trim($searchfor);

}
$albumpath = "";

if (isset($_REQUEST["items"])) {
$itemcounts = $_REQUEST['items'];
}
else {
$itemcounts = 999999;
}

//****************************************************************************************************************
function startElement($parser, $name, $attrs)
{
   global $depth, $albumpath, $itemarray,$bufferstring;
   global $currentTag, $currentAttribs,$searchfor,$searchfound,$searchtags,$searchtitle,$searchdesc,$searchmeta,$searchmode ;
   $currentTag = $name;
   $currentAttribs = $attribs;

   for ($i = 0; $i < $depth[$parser]; $i++) {
       echo "  ";
   }
   if ($name == "IMAGE") {
     if (isset($itemarray)) {
	 	     reset($itemarray);
	     unset($itemarray);
     }
    global $itemarray;
  	    $itemarray = array();
    $bufferstring= "";

    if ($albumpath != "") {
	$THIS_URL = $THIS_URL.$albumpath;
    }
    	array_push($itemarray,"\n");


	if( $searchtags =="Y" && generic_search($attrs['TAGS'], $searchfor, $searchmode)   ){
		$searchfound ="Y";
	} else {$searchfound ="N"; };


	if( $searchtitle =="Y" && generic_search($attrs['TITLE'], $searchfor, $searchmode)   ){
		$searchfound ="Y";
	}

	if ($searchfound == "Y") {
		$imagestring = "<image    title=\"". $attrs['TITLE']. "\"  img=\"". $albumpath.$attrs['IMG']."\"  thmb=\"". $albumpath. $attrs['THMB']."\"   downloadimg=\"". $albumpath.$attrs['DOWNLOADIMG']."\"   size=\"". $attrs['SIZE']."\"  type=\"". $attrs['TYPE']."\"  dt=\"". $attrs['DT']."\"  thumbw=\"". $attrs['THUMBW']."\"  thumbh=\"". $attrs['THUMBH']."\" tags=\"". utf8_encode($attrs['TAGS'])."\""  ;
		if ($attrs['WIDTH'] != "" && $attrs['HEIGHT'] != "")
			$imagestring.= " width=\"". $attrs['WIDTH']."\"  height=\"". $attrs['HEIGHT']."\">";
		else if ($attrs['PRICEVARIANCE'] != "")
				$imagestring.= " pricevariance=\"". $attrs['PRICEVARIANCE']."\">";
			else
				$imagestring.= ">";
		array_push($itemarray,$imagestring);
		array_push($itemarray,"<description>");
	}
	else {
		$bufferstring  = "<image    title=\"". $attrs['TITLE']. "\"  img=\"". $albumpath.$attrs['IMG']."\"  thmb=\"". $albumpath. $attrs['THMB']."\"   downloadimg=\"". $albumpath.$attrs['DOWNLOADIMG']."\"   size=\"". $attrs['SIZE']."\"  type=\"". $attrs['TYPE']."\"  dt=\"". $attrs['DT']."\"  thumbw=\"". $attrs['THUMBW']."\"  thumbh=\"". $attrs['THUMBH']."\" tags=\"". utf8_encode($attrs['TAGS'])."\"" ;
		if ($attrs['WIDTH'] != "" && $attrs['HEIGHT'] != "")
			$bufferstring .= " width=\"". $attrs['WIDTH']."\"  height=\"". $attrs['HEIGHT']."\"><description>";
		else if ($attrs['PRICEVARIANCE'] != "")
				$bufferstring.= " pricevariance=\"". $attrs['PRICEVARIANCE']."\"><description>";

			$bufferstring.= "><description>";
	}

   };
   if ($name == "DESCRIPTION") {
     if (isset($itemarray) && $searchfound=="Y") {
  		  array_push($itemarray,"<![CDATA[");
	}
     if (isset($itemarray) && $searchfound=="N") {
  		  $bufferstring .="<![CDATA[";
	}

   }

   if ($name == "EXIF") {
     if (isset($itemarray) && $searchfound=="Y") {
  		  array_push($itemarray,"<exif    date=\"". $attrs['DATE']. "\"  resolution=\"". $attrs['RESOLUTION']."\"  flash=\"". $attrs['FLASH']."\"   focallength=\"". $attrs['FOCALLENGTH']."\"   exposuretime=\"". $attrs['EXPOSURETIME']."\"  aperture=\"". $attrs['APERTURE']."\"  focaldistance=\"". $attrs['FOCALDISTANCE']."\"  meteringmode=\"". $attrs['METERINGMODE']."\"  cameramake=\"". $attrs['CAMERAMAKE']."\"   cameramodel=\"". $attrs['CAMERAMODEL']."\"  sensortype=\"". $attrs['SENSORTYPE']."\"  iso=\"". $attrs['ISO']."\"         >" );
	}

	else {

		 if ($searchmeta =="Y")  {
				 $attr_merged = array_to_string($attrs);
				 if (isset($itemarray) && $searchfound=="N" && generic_search($attr_merged, $searchfor, $searchmode) ) {
					 array_push($itemarray,$bufferstring);
					 array_push($itemarray,"<exif    date=\"". $attrs['DATE']. "\"  resolution=\"". $attrs['RESOLUTION']."\"  flash=\"". $attrs['FLASH']."\"   focallength=\"". $attrs['FOCALLENGTH']."\"   exposuretime=\"". $attrs['EXPOSURETIME']."\"  aperture=\"". $attrs['APERTURE']."\"  focaldistance=\"". $attrs['FOCALDISTANCE']."\"  meteringmode=\"". $attrs['METERINGMODE']."\"  cameramake=\"". $attrs['CAMERAMAKE']."\"   cameramodel=\"". $attrs['CAMERAMODEL']."\"  sensortype=\"". $attrs['SENSORTYPE']."\"  iso=\"". $attrs['ISO']."\"         >" );
					 $searchfound="Y" ;
				 }
		}
	}


    if (isset($itemarray) && $searchfound=="N") {
    	$bufferstring .= "<exif    date=\"". $attrs['DATE']. "\"  resolution=\"". $attrs['RESOLUTION']."\"  flash=\"". $attrs['FLASH']."\"   focallength=\"". $attrs['FOCALLENGTH']."\"   exposuretime=\"". $attrs['EXPOSURETIME']."\"  aperture=\"". $attrs['APERTURE']."\"  focaldistance=\"". $attrs['FOCALDISTANCE']."\"  meteringmode=\"". $attrs['METERINGMODE']."\"  cameramake=\"". $attrs['CAMERAMAKE']."\"   cameramodel=\"". $attrs['CAMERAMODEL']."\"  sensortype=\"". $attrs['SENSORTYPE']."\"  iso=\"". $attrs['ISO']."\"         >";
	}


   }


   if ($name == "IPTC") {
     if (isset($itemarray) && $searchfound=="Y") {
  		  array_push($itemarray,"<iptc   keywords=\"". utf8_encode($attrs['KEYWORDS']). "\"  category=\"". utf8_encode($attrs['CATEGORY'])."\"  author=\"". utf8_encode($attrs['AUTHOR'])."\"   copyright=\"". utf8_encode($attrs['COPYRIGHT'])."\"   >" );
	}


	else {

		 if ($searchmeta =="Y")  {

					 $attr_merged = array_to_string($attrs);
					 if (isset($itemarray) && $searchfound=="N" && generic_search($attr_merged, $searchfor, $searchmode) ) {
						 array_push($itemarray,$bufferstring);
						 array_push($itemarray,"<iptc   keywords=\"". utf8_encode($attrs['KEYWORDS']). "\"  category=\"". utf8_encode($attrs['CATEGORY'])."\"  author=\"". utf8_encode($attrs['AUTHOR'])."\"   copyright=\"". utf8_encode($attrs['COPYRIGHT'])."\"   >" );
						 $searchfound="Y" ;
					}
		}
	}



   }



   $depth[$parser]++;
}

//****************************************************************************************************************

function endElement($parser, $name)
{
   global $depth, $currentTag, $currentAttribs, $itemarray, $mainsort,$searchfound,$bufferstring ;
   $depth[$parser]--;

   $currentTag = "";
   $currentAttribs = "";
   if ($name == "IMAGE" ) {
     if (isset($itemarray) && $searchfound=="Y") {
	     array_push($itemarray,"</image>");
	     array_push($mainsort,$itemarray);
     }

   };

   if ($name == "DESCRIPTION") {
     if (isset($itemarray) && $searchfound=="Y") {
	     array_push($itemarray,"]]></description>");
     }
     if (isset($itemarray) && $searchfound=="N") {
	     $bufferstring .= "]]></description>";
     }

   };

   if ($name == "EXIF" ) {
     if (isset($itemarray) && $searchfound=="Y") {
	     array_push($itemarray,"</exif>");
     }

     if (isset($itemarray) && $searchfound=="N") {
	     $bufferstring .= "</exif>";
     }
   };

   if ($name == "IPTC" ) {
     if (isset($itemarray) && $searchfound=="Y") {
	     array_push($itemarray,"</iptc>");
     }

   };
}
//****************************************************************************************************************
function characterData($parser, $data)
     {
         global $currentTag, $itemarray,$searchfound,$bufferstring,$searchfor,$searchdesc,$searchmode ;
         switch ($currentTag) {
         case "DESCRIPTION":
         	if ($searchfound =="Y") {
     		array_push($itemarray,utf8_encode($data));
     	}

         	if ( $searchfound =="N" && $searchdesc =="Y"  && generic_search($data, $searchfor, $searchmode)   ){
		array_push($itemarray,$bufferstring);
//	  		array_push($itemarray,"<![CDATA[");
     		array_push($itemarray,utf8_encode($data));
		$searchfound="Y" ;
     	}

         	if ($searchfound =="N") {
     		$bufferstring .= utf8_encode($data);
     	}


            break;
         default:
             break;
         }
     }

//****************************************************************************************************************
function processalbum($file,$folpath) {
global $depth, $mainsort,$itemcounts,$albumpath,$searchfound ;
$albumpath = $folpath ;
$depth = array();
$fp = "";$data ="";$mainsort= array();
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
   die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
   	if (!xml_parse($xml_parser, $data, feof($fp))) {
      	 die(sprintf("XML error: %s at line %d",
             xml_error_string(xml_get_error_code($xml_parser)),
             xml_get_current_line_number($xml_parser)));
   		}
}


usort($mainsort, 'compare');

$tempcount=count($mainsort);
if ($itemcounts < $tempcount) {
	$tempcount=$itemcounts;
};

for ( $row = 0; $row < $tempcount; $row++ ) {

	 for ( $column = 1; $column < count($mainsort[$row]); $column++ ) {
		echo utf8_decode($mainsort[$row][$column]);
	};
 };

fclose($fp);
xml_parser_free($xml_parser);

}  //processalbum function ends here
//****************************************************************************************************************

function compare($x, $y)
{
 if ( $x[0] == $y[0] )
  return 0;
 else if ( $x[0] < $y[0] )
  return 1;
 else
  return -1;
}

//****************************************************************************************************************

function generic_search($searchable, $search_term, $search_type)
{
 $new_search_term = split(" ", $search_term);
 $num_search_terms = count($new_search_term);

 $include_search_words = array();
 $exclude_search_words = array();
 $include_search_count = 0;
 $exclude_search_count = 0;


 for ($j = 0; $j < $num_search_terms; $j++)
     {
     	if (strpos($new_search_term[$j], '-') ===0 ) {
     	$exclude_search_words[$exclude_search_count] = trim(substr($new_search_term[$j],1));
     	$exclude_search_count++;
     	}
     	else {
     	$include_search_words[$include_search_count] = trim($new_search_term[$j]);
     	$include_search_count++;
     	}
     }

$search_term = trim(array_to_string($include_search_words));
$searchable  = utf8_decode($searchable);
if (isset($_REQUEST["debug"])) {
	echo "<searchfor>".$search_term."</searchfor><searchin>".$searchable."</searchin>\r\n";
};

     if ($search_type == "")
     {
          if (eregi($search_term, $searchable)) {

          foreach ($exclude_search_words as $term)
          {
               if (eregi($term, $searchable)) { return FALSE; }
          }
          return TRUE;
          }
          else return FALSE;
     }

     elseif ($search_type == "AND")
     {
          foreach ($include_search_words as $term)
          {
               if (!eregi($term, $searchable)) { return FALSE; }
          }

          foreach ($exclude_search_words as $term)
          {
               if (eregi($term, $searchable)) { return FALSE; }
          }

          return TRUE;
     }
     elseif ($search_type == "OR")
     {
     	  $return_flag = 0;
          foreach ($include_search_words as $term)
          {
               if (eregi($term, $searchable)) { $return_flag = 1;break; }
          }

          if ($return_flag == 0) return FALSE;
          else
          {
		  foreach ($exclude_search_words as $term)
		  {
			   if (eregi($term, $searchable)) { return FALSE; }
		  }
		  return TRUE;
          }

     }
}

//****************************************************************************************************************
function array_to_string($array) {
   $retval = '';
   $null_value = "^^^";
   foreach ($array as $index => $value) {
       if (!$value)
           $value = $null_value;
       $retval .= trim($value) . ' ';
   }
   return $retval;
}
//****************************************************************************************************************

$URL_IMG = 'http://' . $_SERVER['SERVER_NAME'];
$URL_IMG.= dirname($_SERVER['PHP_SELF']);
$tm=time(); $tm=date("D, d M Y H:i:s",$tm);
$tm=$tm. " GMT"; // you can add your format

if (isset($_REQUEST["debug"]))
header("Content-Type: text/text; charset=utf-8");
else
header("Content-Type: text/xml; charset=utf-8");


echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
echo "<gallery>";

global $mainsort;
$mainsort = array();

if ($albumpath != "" && $albumpath != "all/") {
$xmlpath = $albumpath."photos.xml";
      if (file_exists($xmlpath)) {
  		processalbum($xmlpath,$albumpath);
      }

}
if ($albumpath == "" ) {
$xmlpath = "photos.xml";
if (file_exists($xmlpath)) {
	processalbum($xmlpath,$albumpath);
}
map_dirs(".");


}
function map_dirs($path) {

       if(is_dir($path)) {
               if($contents = opendir($path)) {
                       while(($node = readdir($contents)) !== false) {
		       if( (!strpos($node,"."))  && $node!="res" && $node !="images" && $node != "slides" && $node !="thumbs" && $node !="rss_styles" && $node!= "." && $node!= ".." ) {

					if ($path==".") {
						$folpath = $node."/";
						if (file_exists($folpath."photos.xml")) {
							processalbum($folpath."photos.xml",$folpath);
						}
					}
					else {
						$folpath = $path."/".$node."/";
						$folpath = ltrim($folpath,'./');
						if (file_exists($folpath."photos.xml")) {
							processalbum($folpath."photos.xml",$folpath);
						}


					}

                                    map_dirs("$path/$node");
                         }
                       }
               }
       }
}

echo("</gallery>");

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/50352-sort-by-date-query/
Share on other sites

to do this with SQL, use: "SELECT * FROM table WHERE x=y ORDER BY date_field_name_goes_here DESC"

 

I've not read through all that code, so i dunno how you're grabbing the photos but databases are the best thing to use for something like that (that's what i think anyway).

Link to comment
https://forums.phpfreaks.com/topic/50352-sort-by-date-query/#findComment-247224
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.