How to retrieve specific value from zendframework 2.0 database - php

Hello i have a question about the zend framework. I have a database with a column having values yes and no. I wish to be able to retrieve from that column only yes values.
This is my code but no success:
public function getListi(array $filterBy = array())
{
$id = 'yes';
$select = $this->sql->select();
$select->from(self::TABLE);
$select->columns(['security_maintenance']);
$select->where(array(
'security_maintenance' => $id
));
$statement = $this->sql->prepareStatementForSqlObject($select);
return $statement->execute();
}
view :
<div class="value-self"><?php
foreach ($domainii as $row) {
echo $row['security_maintenance'];
}
?></div>
controller:
public function indexAction()
{
return new ViewModel(array(
'domainii' => $this->getDomainModel()->getListi(),
));
}
The column is called security_maintenance. the above does not work but when i query by id it works but that does not help me as querying by id will not help me get yes values from the security_maintenance column.

Hello i got it working after doing some research into some zend documentation. This is my code:
public function getListii(array $filterBy = array())
{
$select = $this->sql->select();
$select->from(self::TABLE);
$select->columns(['security_maintenance']);
$select->where->like('security_maintenance', 'yes%');
$statement = $this->sql->prepareStatementForSqlObject($select);
return $statement->execute();
}

You need to specify the columns you want to select.
try adding the below code
$select->columns(['security_maintenance']);

Related

How to get result based on 2 query in CI

sorry for the confusing title as I really did not know what to type. I'm new to CI and now I'm trying to convert my code to CI and stuck here.
here is my original code:
$query_domain = $konek->prepare("SELECT * FROM `domain` WHERE `id` = :id");
$query_domain->bindParam(":id", $id);
$query_domain->execute();
$data_domain = $query_domain->fetch();
$query_owner = $konek->query("SELECT * FROM `people` WHERE `id` = $data_domain->ownerid");
$data_owner = $query_owner->fetch();
So basically it request a domain where the domain id is X
and then it request data of the owner based on an owner id that is in domain table.
I'm not really sure what to put in controller or model
but here is my current model:
public function get_domain($id){
$this->db->get_where('domain', array('id' => $id));
}
public function get_domain_owner($ownerid){
$this->db->get_where('client', array('id' => $ownerid));
}
In your (Client) model:
public function get_owner_with_domain($domain)
{
$query = $this->db->select('c.*')->
from('client c')->
join('domain d', 'd.ownerid = c.id')->
where('d.name',$domain)->get();
if ($query) {
return $query->row_array();
// Or, ideally return a client if you have a Client model
// return $query->row(0,'Client');
} else {
// log error?
return false;
}
}
In your controller:
public function client($domain) // or whatever your function might be called
{
$this->load->model('Client');
$client = $this->Client->get_owner_with_domain($domain)
// Do something with the client
// var_dump($client);
$view_data['client'] = $client;
$this->load->view('client_info',$view_data);
}
Further reading:
CodeIgniter ActiveRecord reference
CodeIgniter Results reference
You can do simply by using a single query
SELECT people.* FROM people, domain WHERE people.id = domain.ownerId AND domain.id = :id

Codeigniter php MVC

I have a table called 'News' with three columns: 'id', 'title' and 'details'
I have a function ('get_entry') inside a codeigniter model class (called 'News_model')
function get_entry()
{
$this->load->database();
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
return $data['newsarray'];
}
I am connecting to the db so that is not the problem.I want to return an iterable array from get_entry() by calling the function from a controller file with the followlwing code. I want to push it into another array (called '$data['theNews']') using the code below.
foreach ($this->News_model->get_entry() as $key => $value){
array_push($data['theNews'],$value->title);
}
I have been using the code on this (https://www.codeigniter.com/user_guide/general/models.html) as a template (in particular the function 'get_last_ten_entries()' but I think I am close with the code I posted above. I would appreciate any help.
About your code:
You have two 'return' in your get_entry function:
function get_entry()
{
$this->load->database();
// First
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
// Second
return $data['newsarray'];
}
Change it to:
function get_entry()
{
$this->load->database();
$query = $this->db->select('id,title,details')->from('news');
$data['newsarray'] = $query->row_array();
return $data['newsarray'];
}
It should work now.
Some advices:
Don't use Codeigniter 2 anymore. Version 3 is alive.
If you plan to return whole table columns, i suggest you to use the following code for the query:
$query = $this->db->get('news', 1, 20);
Where 1, 20 is the limit.
Now you can get the result:
return $query->result();
A simple example:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news', 1, 20);
return $query->result();
}
This method returns the query result as an array of objects that you can print like so in your controller:
$news_array = $this->News_model->get_entry();
foreach ($news_array as $news)
{
echo $news->id;
}
Look at CI 3 Query Builder query builder for more examples.
One more suggestion, just autoload the database library in application/config/autoload.php if you need it globally.
Changing the code to this in the function worked:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news');
//return $query->result();
foreach ($query->result() as $row)
{
echo "</br>";
echo $row->id;
echo "</br>";
echo $row->title;
echo "</br>";
echo $row->details;
echo "</br>";
}
}
Calling the function like so prints it out:
$news_array = $this->News_model->get_entry();

generating querying result for row() in codeigniter:

I am newbie in codeigniter. If I have a model like this :
public function get_data_for_reminder($id) {
$this->db->select('nama_user, keluhan, email, addresed_to');
$query = $this->db->get_where('tbl_requestfix', array('id_request' => $id));
return $query->row();
}
And I try to accessed it from may controller :
public function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
How to Generating Query Results, thanks for the help.
EDIT
I am new in CI, my question is : let's say I want to get the 'nama_user' into an a variable like this :
foreach ($data as $d) {
$name = $d['nama_user'];
}
echo json_encode($name);
I use firebug, it gives me null. I think my foreach is in a problem
In order to return an array from your model call you can use result_array() as
public function get_data_for_reminder($id) {
$this->db->select('nama_user, keluhan, email, addresed_to');
$query = $this->db->get_where('tbl_requestfix', array('id_request' => $id));
return $query->result_array();//<---- This'll always return you set of an array
}
Controller File with your query code
public function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
//Generating Query Results like this because you use $query->row();
$data->nama_user;
$data->keluhan;
$data->email;
$data->addresed_to;
$info_json = array("nama_user" => $data->nama_user, "keluhan" => $data->keluhan, "email" => $data->email, "addresed_to" => $data->addresed_to);
echo json_encode($info_json);
}
MODEL.PHP
public function get_data_for_reminder($id) {
$this->db->select('nama_user, keluhan, email, addresed_to');
$query = $this->db->get_where('tbl_requestfix', array('id_request' => $id));
return $query->row_array();
}
//Generating Query Results if use $query->row_array(); in model file
Controller.php
function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
foreach($data as $row)
{
$myArray[] = $row;
}
echo json_encode($myArray);
You fetched data for selected id it means you get a single row, if you want to get "nama_user" then in your controller:
public function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
$name = $data->nama_user;
echo json_encode($name);
}

ZF2 Executing DB query from Controller

Hi i am new to ZF2 and not familiar with some of the changes in ZF2. I would like to know how can i execute SQL query directly from Controller.
I have the following code:
public function indexAction()
{
$db = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$sql = "SELECT * FROM books";
$statement = $db->query($sql);
$res = $statement->execute();
if($res instanceof ResultInterface && $res->isQueryResult()){
$resultSet = new ResultSet;
$resultSet->initialize($res);
foreach($resultSet as $row){
echo $row->title . PHP_EOL;
}
}
exit;
/*
return new ViewModel(array(
'books' => $this->getBooksTable()->fetchAll(),
));
*/
}
When the controller above is opened in web browser, it does not show anything. If i echo "Blahh.." before the if statement, it displays the "Blahh.." text correctly.
Does anyone know why it does not display the query result properly? Thx.
try to add this in the top of your controller :
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\ResultSet;

mysql queries does not work in zend framework

class Application_Model_DbTable_Email extends Zend_Db_Table_Abstract
{
protected $_name = 'memberdetail';
function getUserid($email)
{
$subquery = $this->select()
->from('memberdetail', array('memberid'))
->where('email = ?', $email);
$select = $this->select()
->from('usertable', array('userid'))
->join('memberdetail', 'usertable.userid = memberdetail.memberid')
->where('usertable.userid = ?', $subquery);
$row = $select->query()->fetch();
if (!$row) {
echo "User id not found";
} else {
return $userid = $row['userid'];
}
}
}
Hi, I am trying to return the userid from the above queries. However, the queries does not seemed to be executed as I always get refreshed whenever I call this function.
P.S this set of queries were given to me by another member.
it looks like this is being over thought. According to the info provided usertable.userid = memberdetail.memberid with this being the case your function is simple.
/** this function assumes one and only one email will match a memberid
* this function can be improved by validating $email as existing in DB
* prior to querying DB, should be done at form level but could be accomplished here
* with Zend_Validate_Db_RecordExists()
*/
public function getUserIdFromEmail($email) {
$select = $this->select();
$select->where('email = ?',$email);
$row = $this->fetchRow($select);//fetch a single row
if (!is_null($row) {//fetchRow returns null if no row matched
return $row->memeberid;//return memberid as string/integer = usertable.userid
} else {
//handle error
}
}
It would have been useful to tell people you are using Zend framework.
You need to establish a connection to the database for $this as described in steps 1 and 2 in this link:
http://framework.zend.com/manual/en/zend.db.select.html/
You can try this, if it helps:
function getUserid($email){
$select = $this->select()
->setIntegrityCheck(false)
->from(array('m' => 'memberdetail'), array('b.userid'))
->join(array('b' => 'usertable'), 'b.userid = m.memberid')
->where('m.email = ?', $email);
$row = $this->getAdapter()->fetchAll($select);
if (!$row) {
throw new Exception("User id not found");
} else {
return $row->toArray();
}
}

Categories