How To Get NON-Duplicate Values using Zend DB model? - php

fetchresultsAction
public function fetchresultsAction() {
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
//$form_data = $this->getRequest()->getPost();
//$examcatID = $form_data['examcatID'];
$examID = 'Exam_ID_001';
$Exam_Results = new Admin_Model_Examresults();
$results = $Exam_Results->fetchExamResults($examID);
foreach ($results as $value) {
print_r($value['subjectID']);
echo '<br/>';
}
}
Model > Examresults
public function fetchExamResults($examID) {
$sel = $this->select();
$sel->distinct();
$sel->where('examID=?', $examID);
$result = $this->fetchAll($sel); // fetch the rows to the array called result
return $result;
}
This Code is not work.I need have to get NON-Duplicate values of subjectID from mysql database.How Can I do that.Please help me to solve this problem.
Note : This is Zend 1.12 version

Try using print_r($value->subjectID)

Related

Navigate an array from controller in Codeigniter

Good I'm starting with php and codeigniter, I have the following problem I'm running an array from my model, which is the result of a query, how can I read one by one my records in the controller?
function getCustomers() {
$sql = "SELECT * FROM customers";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$i = 0;
foreach($query->result() as $row) {
$img[$i]['id'] = $row->id;
$img[$i]['name'] = $row->name;
$img[$i]['Location'] = $row->Location;
$img[$i]['telephone'] = $row->telephone;
$i++;
}
return $img;
}
}
You can return array to the controller by loading the model into the controller like this.
$this->load->model('yourmodel');
$array = $this->yourmodel->yourmodelsmethod();
First you have to return this array to your controller like
$this->load->model('yourmodel');
$array['test'] = $this->yourmodel->yourmodelsmethod();
foreach($test as $key=>$value)
{
echo $value;
}
By doing this you can easily print your data from model in controller

Update multiple rows of database in Laravel

I want a code for update multiple rows of database, somthing like this:
UPDATE values SET data='{"options":["male","female"],"default":"male"}' where project_id=1 and id=1;
UPDATE values SET data='{"options":["male","female"],"default":"male"}' where project_id=1 and id=2;
UPDATE values SET data='{"options":["male","female"],"default":"male"}' where project_id=1 and id=3;
After some hours i could get result with something like this in laravel framework:
$values = Value::where('project_id', $id)->get();
$sql = "";
foreach($request->fields as $field) {
if(!empty($field->default)) { //New default value is set
foreach($values as $value) {
$data = json_decode($value->data, true); /**data column as json object in mysql database **/
$data["default"] = $field->default;
$data = json_encode($data);
$sql .= "update ".DB::connection()->getDatabaseName().".values set data='".$data."' where id="."$value->id;";
}
}
}
DB::unprepared($sql);
but this code is not a good practice!
So my question is
Is there any ORM way to do this better?!
Here is an easy way to do it.
$values = Value::where('project_id', $id)->update(['data'=>$data]);
I found it from this link
Hope it helps.
$values = Value::where('project_id', $id)->get();
$sql = "";
foreach($request->fields as $field) {
if(!empty($field->default)) { //New default value is set
foreach($values as $value) {
$tmp=$value->data;
$tmp->default = $field->default;
$value->data = $tmp;
$value->save();
}
}
}
And in Value model use Mutator and Accessor, like this
public function getDataAttribute($value)
{
return json_decode($value);
}
public function setDataAttribute($value)
{
$this->attributes['data'] = json_encode($value);
}
See documentation http://laravel.com/docs/4.2/eloquent#accessors-and-mutators
Work - https://yadi.sk/i/BnC3HYozehoy2

how to return full table data bu using function in php

I want to return the multiple records in table but do not know how. To return all values of table and return ?
please tell how can i do it.
function get_semester_data($id) {
$query_1="SELECT sem.smstr_id,sem.smstr_no,sem.start_mnth,sem.start_year,sem.end_mnth,sem.end_year,sem.status FROM `semesters` sem,sessions s where s.s_id=sem.s_id and s.s_id=$id";
$result=mysql_query($query_1);
while($row=mysql_fetch_array($result)) {
}
return $row;
}
i want to return the multiple records in table but do not know how. to return all values of table and return ?
please tell how can i do it.
try like this
while($row=mysql_fetch_array($result)){
$data[]=$row;
}
return $data;
You can return an array of rows!
function get_semester_data($id) {
$query_1="SELECT sem.smstr_id,sem.smstr_no,sem.start_mnth,sem.start_year,sem.end_mnth,sem.end_year,sem.status FROM `semesters` sem,sessions s where s.s_id=sem.s_id and s.s_id=$id";
$result=mysql_query($query_1);
$all_rows = array();
while($row=mysql_fetch_array($result)) {
$all_rows[] = $row;
}
return $all_rows;
}
//Get the values like this
$rows = get_semester_data(12);
//print all smstr_no like this
foreach ($rows as $row) {
echo $row[1];
}
First the code:
function get_semester_data($id) {
$query_1="SELECT sem.smstr_id,sem.smstr_no,sem.start_mnth,sem.start_year,sem.end_mnth,sem.end_year,sem.status FROM `semesters` sem,sessions s where s.s_id=sem.s_id and s.s_id=$id";
$result=mysql_query($query_1);
$result = array();
while($row=mysql_fetch_array($result)) {
array_push($result, $row);
}
return $result;
}
Just declare the array, add the rows and return it.
Suggestions:
the extension you are using is old and will be deprecated, use mysqli
or pdo instead
be careful and sanitize your input because you are prone to SQL
Injection on the $id parameter. Use this:
$query_1="SELECT sem.smstr_id, sem.smstr_no, sem.start_mnth, sem.start_year, sem.end_mnth, sem.end_year, sem.status FROM semesters sem,sessions s where s.s_id=sem.s_id and s.s_id = '" . htmlentities($id, ENT_QUOTES) . "'";

How i get data and returned in json format in zf2?

I used mongo odm query for getting data according to id i want to get query data and return this data in json format,how i do this?
here is my code:
public function loadAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
$dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
$data = $this->dm->createQueryBuilder('Calendar\Document\Event')
->field('calendar_id')->equals($id)->getQuery()->execute();
//$count_tags = $eventdata->count();
$array = array();
if($data && !is_null($data) && is_object($data)){
foreach($data as $key=>$value) {
$array[] = $value;
}
}
return $this->getResponse()->setContent(Json::encode($array));
}
You can accomplished this by following
return new JsonModel($arrayVaraible);
you might need to import
Zend\View\Model\JsonModel;
Hope That helps

How to through multidimensional array in Codeigniter?

I'm trying to get my head around grabbing all variable I need in Codeigniter and then passing those values to the view.
I understand the concept, but am getting stuck on iterating through an array and adding on another array to each of its keys.
The $data['items'] array contains data passed from a model via the get_all() function. The query is basically "Select * from items".
Each item has multiple features and pictures. In order to get the appropriate values I need the id of an item. I can grab this from the first array ($data['items']) which I can then pass to a model function.
I keep getting various errors when trying to do this via the code below. I've tried defining the features and pictures arrays before the foreach loop - still get errors.
I'm sure my syntax is just wrong. Any help is welcome.
$data['items'] = $this->amazon->get_all();
foreach ($data['items'] as $data )
$id = $data->id;
$data['features'] = $this->amazon->get_features($id);
$data['pictures'] = $this->amazon->get_pictures($id);
}
Edit
Based on feedback I've updated the code to this (lines 24 - 30 of the code):
$items = $this->amazon->get_all();
for($i=0; $i<count($items);$i++) {
$data = $items[$i];
$id = $data->id;
$items[$i]['features'] = $this->amazon->get_features($id);
$items[$i]['pictures'] = $this->amazon->get_pictures($id);
}
PHP is complaining with this:
PHP Fatal error: Cannot use object of type stdClass as array in /var/www/ci/application/controllers/main.php on line 28
Here are the functions from the amazon model:
function get_all()
{
$query = $this->db->get('items');
return $query->result();
}
function get_pictures($id) {
$query = $this->db->query("SELECT link FROM pictures WHERE item_id='$id' AND type IN('thumb_set')");
$style = "border-style:solid; border-width:1px";
$class = "modal-thumb";
$results = '';
foreach ($query->result() as $row) {
$results .= "<li style='$style' class='$class'><img src='$row->link' alt=''/></li>";
}
return $results;
}
function get_features($id) {
$query = $this->db->query("SELECT content FROM features WHERE item_id='$id' ORDER BY feature_num DESC");
$results = '';
foreach ($query->result() as $row) {
$results .= "<li>";
$results .= $row->content;
$results .= "</li>";
}
return $results;
}
I'm thinking i need to use 'results_array()' instead of 'results()'? Are my results returned as an object instead of an array the way things are now?
As you said, you have syntax error in the foreach statement, you are redefining the $data array.
foreach ($data['items'] as $data )
Try this:
$items = $this->amazon->get_all();
for($i=0; $i<count($items);$i++){
$data = $items[$i];
$id = $data->id;
$items[$i]['features'] = $this->amazon->get_features($id);
$items[$i]['pictures'] = $this->amazon->get_pictures($id);
}

Categories