Location: PHPKode > projects > MyNotebook > mynotebook/notes.select.php
<?php
// Selects all notes that contain $tags and which have $keywords (search-text)
// returns the result of mysql_query
function selectNotes ($inTags, $inKeywords = NULL)
{

  global $db_tableName;
  global $db_tagTableName;
  global $db_noteTagsTableName;
  global $allNotesTagName;
  
  if (strstr ($inTags, $allNotesTagName) === false)
  {
    $tags = str_replace ("-", " ", $inTags);
    $tags = trim ($tags, " ");
    $tags = str_replace ("  ", " ", $tags);
    $tags = trim ($tags);
  
  
    $tags = "tagname='". $tags;
    $tags = str_replace (" ", "' OR tagname='", $tags . "'");
    $tags = "WHERE (" . $tags . ")"; 
    $query = "SELECT * FROM $db_noteTagsTableName INNER JOIN $db_tagTableName ON $db_noteTagsTableName.tagid = $db_tagTableName.id INNER JOIN $db_tableName ON noteid=$db_tableName.id " . $tags . " GROUP BY $db_tableName.id ORDER BY $db_tableName.datetime DESC";
  }
  else
  {
    $query = "SELECT * FROM $db_tableName WHERE $db_tableName.id=$db_tableName.id ORDER BY datetime DESC";
  }
  if ($inKeywords != NULL)
  {
    $keywords = ereg_replace ("([ ]+)", " ", $inKeywords);
    $query2 = str_replace (" ", "%' AND $db_tableName.note LIKE '%", $keywords);
    $query = ereg_replace ("WHERE", "WHERE $db_tableName.note LIKE '%" . $query2 . "%' AND", $query);
  }
  //echo $query;    // Uncomment this to see the final SQL-Query
  $result = mysql_query ($query);
  return $result;

}


function displayNotes ()
{
  global $tags;
  global $defaultTags;
  global $search;
  if (isset ($tags))
  {
    echo getNotes (selectNotes($tags, $search), $search);
    exit;
  }
  
}

function getNotesJSON()
{
	global $tags;
	global $defaultTags;
	global $search;
	if (isset ($tags))
	{
		$jsonStrings = array();
		$i = 0;
		foreach (getNotes (selectNotes($tags, $search), $search) as $note)
		{
			$value[$i]["id"] = $note->getId();
			$value[$i]["note"] = $note->getNote();
			$value[$i]["tags"] = $note->getTags();
			$value[$i]["date"] = $note->getDate();
			$value[$i]["innerHTML"] = $note->getInnerHTML();
			$i++;
		}
		$json = new Services_JSON();
		echo $json->encode($value);
		exit;
	}
}

// Returns all notes in an array
// if $keywords is specified all text containing $keynotes will be highlighted inside a note
function getNotes ($result, $keywords = NULL)
{
	global $showNew;
	global $note;
	global $db_tableName;
	global $db_tagTableName;
	global $db_noteTagsTableName;
    
	$notes = array();
	
	// Iterate through all notes
	while (($row = mysql_fetch_array ($result)) != null)
	{
		// Get all tags of this note
		$tags = "";
		$query = "SELECT * FROM $db_noteTagsTableName INNER JOIN $db_tagTableName ON $db_tagTableName.id=$db_noteTagsTableName.tagid INNER JOIN $db_tableName on $db_tableName.id=$db_noteTagsTableName.noteid WHERE $db_tableName.id=" . $row["id"];
		$result2 = mysql_query ($query);
		while ( ($row2 = mysql_fetch_array ($result2)))
		$tags .= $row2["tagname"] . " ";

		$tags = trim ($tags);
    
  	$note = $row["note"];
  	$rawNote = $row["note"];

		$newNote = new Note ($row["id"], $note, $rawNote, $tags, $row["datetime"]);
		$newNote->createHTML ($keywords);
    $notes[] = $newNote;	
	}
	return $notes;
}
?>
Return current item: MyNotebook