Skip to content

Instantly share code, notes, and snippets.

@morningtoast
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save morningtoast/93bed54332a2e07d1def to your computer and use it in GitHub Desktop.

Select an option

Save morningtoast/93bed54332a2e07d1def to your computer and use it in GitHub Desktop.
chaining
<pre>
<?php
include("_dummy.php");
class Lookup {
function __construct() {
$this->base =& get_instance();
$this->ids = array();
$this->mode = "topic";
$this->settings = array();
$this->result = false;
$this->d = new dummy;
}
function find($table, $needle, $haystack=false, $fields="*", $sort=false) {
if (!$haystack) {
$columns = $this->base->db->field_data($table);
$haystack = "RecordID";
foreach ($columns as $field) {
if ($field->primary_key) {
$haystack = $field->name;
break;
}
}
}
if (!is_array($needle)) {
$needle = array($needle);
}
if (!$fields) { $fields = "*"; }
$this->base->db->select($fields);
$this->base->db->where_in($haystack, $needle);
if ($sort) {
$this->base->db->order_by($sort);
}
$result = $this->base->db->get($table);
return($result->result_array());
}
function _convertIds($id) {
if (!is_array($id)) {
$this->ids = array($id);
} else {
$this->ids = $id;
}
return($id);
}
function _convertJson($str) {
if (strpos($str, "{") === 0) {
return(json_decode($str, true));
} else {
return($str);
}
}
function _setMode($mode) {
$this->mode = $mode;
switch ($this->mode) {
case "topic":
$this->settings = array(
"meta_table" => "TopicsMeta",
"meta_key" => "meta_topicid",
"media_table" => "TopicsMedia",
"media_key" => "media_topicid"
);
break;
case "user":
$this->settings = array(
"meta_table" => "MembersMeta",
"meta_key" => "meta_userid"
);
break;
}
}
// Alias for each table lookup
function users($id) {
$this->_convertIds($id);
$this->_setMode("user");
//$this->result = $this->find($this->db->table["Members"], $this->ids, "user_id");
// debug start
$c = count($this->ids);
for ($a=0; $a < $c; $a++) {
$this->result[] = array(
"user_id" => $this->ids[$a],
"user_name" => $this->d->username(),
"user_mail" => $this->d->email()
);
}
// debug end
return($this);
}
function meta($fields=false) {
if ($fields and !is_array($fields)) {
$fields = array($fields);
}
$query = array();
foreach ($this->result as $id => $record) {
// DB call to get meta records with ID of $id
/*
$this->db->where("meta_topicid", $id);
if ($fields) { $this->db->where_in("meta_key", $fields); }
$result = $this->db->get($this->modelTable);
$query = $result->result_array();
*/
// debug start
$query = array(
array(
"meta_key" => "State",
"meta_value" => $this->d->state(),
"meta_type" => ""
),
array(
"meta_key" => "Age",
"meta_value" => $this->d->number(),
"meta_type" => ""
),
array(
"meta_key" => "Age",
"meta_value" => $this->d->number(),
"meta_type" => ""
),
array(
"meta_key" => "Address",
"meta_value" => '{"street":"'.$this->d->street().'","zip":"'.$this->d->zipcode().'"}',
"meta_type" => ""
),
array(
"meta_key" => "TopicPin",
"meta_value" => '{"url":"/path/to/thing1","image":"/path/to/image1"}',
"meta_type" => "list"
),
array(
"meta_key" => "TopicPin",
"meta_value" => '{"url":"/path/to/thing2","image":"/path/to/image2"}',
"meta_type" => "list"
)
);
// debug end
foreach ($query as $query_record) {
$this->result[$id][$query_record["meta_key"]][] = $this->_convertJson($query_record["meta_value"]);
}
}
return($this);
}
function media() {
if ($this->result) {
// $this->result = $this->find($this->settings["media_table"], $id, $this->settings["media_key"]);
$r = array();
foreach ($this->result as $id => $record) {
// debug start
$query = array(
array(
"media_url" => "/path/to/image",
"media_caption" => $this->d->name(),
"media_type" => "image"
),
array(
"media_url" => "/path/to/image",
"media_caption" => $this->d->name(),
"media_type" => "image"
),
array(
"media_url" => "/path/to/youtube",
"media_caption" => $this->d->name(),
"media_type" => "video"
),
);
// debug end
foreach ($query as $query_record) {
$this->result[$id]["media"][] = $this->_convertJson($query_record);
}
}
}
return($this);
}
function result() { return($this->result); }
}
$Lookup = new Lookup();
//echo $a->addA(123)->addB()->getStr();
//$foo = $lookup->topics(99)->meta()->media();
$foo = $Lookup->users(array(12,43,901))->meta()->result();
//$foo = $lookup->meta(array(12,34,21),"user");
print_r($foo);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment