Reputation: 2184
I am trying to access the elements of an array that is returned from MySQL
function CategorySlug(){
$category_slug = $this->f3->get('PARAMS.category_slug');
$categories = new Categories($this->db);
$cat = $categories->getBySlug($category_slug);
$this->f3->set('category',$cat);
$this->f3->set('view','mainCategory.html');
echo \Template::instance()->render('index.html');
}
public function getBySlug($category_slug)
{
$this->load(array('category_slug=?', $category_slug));
return $this->query;
}
When I am trying to access the elements of an array like so {{ @category[category_title] }}
or {{ @category.category_title }}
I have an empty page.
I have done a {{ var_dump(@category) }} just to see if If I have any response from DB at all and I get the following:
array(1) {
[0]=>
object(Categories)#15 (11) {
["db":protected]=>
object(DB\SQL)#9 (8) {
["uuid":protected]=>
string(12) "onvh9nsstjk8"
["pdo":protected]=>
object(PDO)#10 (0) {
}
["dsn":protected]=>
string(54) "mysql:host=localhost;dbname=dev.sp19789"
["engine":protected]=>
string(5) "mysql"
["dbname":protected]=>
string(26) "dev.sp19789"
["trans":protected]=>
bool(false)
["rows":protected]=>
int(1)
["log":protected]=>
string(381) " (0.6ms) SELECT `category_id`,`category_title`,`category_nav_title`,`category_slug`,`parent_category_id` FROM `categories` WHERE category_slug='Products'
"
}
["engine":protected]=>
string(5) "mysql"
["source":protected]=>
string(10) "categories"
["table":protected]=>
string(12) "`categories`"
["_id":protected]=>
NULL
["fields":protected]=>
array(14) {
["category_id"]=>
array(9) {
["type"]=>
string(16) "int(10) unsigned"
["pdo_type"]=>
int(1)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(true)
["value"]=>
int(1)
["initial"]=>
int(1)
["changed"]=>
bool(false)
["previous"]=>
int(1)
}
["category_title"]=>
array(8) {
["type"]=>
string(12) "varchar(255)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(15) "Products"
["initial"]=>
string(15) "Products"
["changed"]=>
bool(false)
}
["category_nav_title"]=>
array(8) {
["type"]=>
string(12) "varchar(255)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(0) ""
["initial"]=>
string(0) ""
["changed"]=>
bool(false)
}
["category_slug"]=>
array(8) {
["type"]=>
string(12) "varchar(100)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(14) "products"
["initial"]=>
string(14) "products"
["changed"]=>
bool(false)
}
["parent_category_id"]=>
array(9) {
["type"]=>
string(16) "int(10) unsigned"
["pdo_type"]=>
int(1)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(true)
["value"]=>
int(1)
["initial"]=>
int(1)
["changed"]=>
bool(false)
["previous"]=>
int(1)
}
}
["adhoc":protected]=>
array(0) {
}
["props":protected]=>
array(0) {
}
["query":protected]=>
array(1) {
[0]=>
object(Categories)#12 (11) {
["db":protected]=>
object(DB\SQL)#9 (8) {
["uuid":protected]=>
string(12) "onvh9nsstjk8"
["pdo":protected]=>
object(PDO)#10 (0) {
}
["dsn":protected]=>
string(54) "mysql:host=localhost;dbname=dev.sp19789"
["engine":protected]=>
string(5) "mysql"
["dbname":protected]=>
string(26) "dev.sp19789"
["trans":protected]=>
bool(false)
["rows":protected]=>
int(1)
["log":protected]=>
string(381) " (0.6ms) SELECT `category_id`,`category_title`,`category_nav_title`,`category_slug`,`parent_category_id` FROM `categories` WHERE category_slug='Products'
"
}
["engine":protected]=>
string(5) "mysql"
["source":protected]=>
string(10) "categories"
["table":protected]=>
string(12) "`categories`"
["_id":protected]=>
NULL
["fields":protected]=>
array(14) {
["category_id"]=>
array(9) {
["type"]=>
string(16) "int(10) unsigned"
["pdo_type"]=>
int(1)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(true)
["value"]=>
int(1)
["initial"]=>
int(1)
["changed"]=>
bool(false)
["previous"]=>
int(1)
}
["category_title"]=>
array(8) {
["type"]=>
string(12) "varchar(255)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(15) "Products"
["initial"]=>
string(15) "Products"
["changed"]=>
bool(false)
}
["category_nav_title"]=>
array(8) {
["type"]=>
string(12) "varchar(255)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(0) ""
["initial"]=>
string(0) ""
["changed"]=>
bool(false)
}
["category_slug"]=>
array(8) {
["type"]=>
string(12) "varchar(100)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(14) "products"
["initial"]=>
string(14) "products"
["changed"]=>
bool(false)
}
["parent_category_id"]=>
array(9) {
["type"]=>
string(16) "int(10) unsigned"
["pdo_type"]=>
int(1)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(true)
["value"]=>
int(1)
["initial"]=>
int(1)
["changed"]=>
bool(false)
["previous"]=>
int(1)
}
}
["adhoc":protected]=>
array(0) {
}
["props":protected]=>
array(0) {
}
["query":protected]=>
array(0) {
}
["ptr":protected]=>
int(0)
["trigger":protected]=>
array(0) {
}
}
}
["ptr":protected]=>
int(0)
["trigger":protected]=>
array(0) {
}
}
}
It looks to me that there are 2 elements in the array that are identical, when there should only be one.
ATM, I can only display Category Title like so {{ @category[0][category_title] }}
Any suggestion to simplify it would be helpful
Upvotes: 0
Views: 741
Reputation: 1660
To return plain object, you should perform cast
public function getBySlug($category_slug)
{
$this->load(array('category_slug=?', $category_slug));
return $this->cast();
}
Upvotes: 1
Reputation: 3908
The method getBySlug()
should return $this
, nothing else:
public function getBySlug($category_slug)
{
$this->load(array('category_slug=?', $category_slug));
return $this;
}
Upvotes: 1
Reputation: 4821
It appears that the query returned two results. If this is the case, then {{ @category[0][category_title] }}
is indeed the correct solution. You might also want to use a limit on your database query.
I'd make sure to use a Model to interact with the database, and return the 0 index result to the controller.
Upvotes: 0
Reputation: 144
when you fetch rows from the db you get array with keys as count of the rows returned from the db, the values is the data from the db.. so also if you choose to fetch only one row, the array will contain one key(0) and categories value, so the data will fetch via
@category[0][category_title]
just handle it in var like:
$category = @category[0]
Upvotes: 0