I am fetching data from the database and the result is multiple rows but it is only showing 1st row in echo. please let me know where is the problem so that I can get all matched results.
public function review_email()
{
$date= date("Y-m-d");
$this->load->model("site_model");
$query = $this->db->get_where('review_email', array("date"=>$date));
$row = $query->row();
if (isset($row))
{
echo $name=$row->name;
}
}
Try This --
public function review_email()
{
$date= date("Y-m-d");
$this->load->model("site_model");
$query = $this->db->get_where('review_email', array("date"=>$date));
//$row = $query->row();
$query->result();
$row = $query->row_array();
foreach ($row as $c)
{
echo $c->name;
}
}
Your code $query->row(); means one row as an object, you also have $query->result(); that will give you all the results as an object too, then $query->row_array(); will give you one as an array, finally you have $query->result_array(); which will give you all the results as an array.
Related
I'm a newbie in Codeigniter, I have a problem when I create model in codeigniter with mysql_fetch array. I don't know how to convert mysql_fetch_array in codeigniter?
my model:
$auto=mysql_query("select * from penjualan order by nonota desc limit 1");
$no=mysql_fetch_array($auto);
$angka=$no['nonota']+1;
Try CodeIgniters query builder, there are really good docs here
For your example I suggest the following:
$query = $this->db->order_by('nonota', 'DESC')
->limit(1)
->get('penjualan');
if( $query->num_rows() > 0) {
$result = $query->result(); //or $query->result_array() to get an array
foreach( $result as $row )
{
//access columns as $row->column_name
}
}
try this
$auto=$this->db->select('*')
->order_by('nonota','desc')
->limit(1)
->get('penjualan ');
$no = $auto->result_array();
$angka = $no[0]['nonota']+1;//for first element in the array index 0;
I have a database table called input_types with a various input types:
[id] [input_type_Name]
1 text
2 radio
3 checkbox
4 select
... ...
I want to retrieve the names from the table and put them in an array so that I can then use form_dropdown to show them to the user in a dropdown.
The problem with how I'm doing it now is that I create optiongroups.
How I do it now:
Model
function get_inputTypes() {
$this->db->select('input_type_name');
$query = $this->db->get('input_types');
if($query->num_rows() > 0) {
return $query->result_array();
}
else {
return false;
}
}
Controller
$results = $this->survey_model->get_inputTypes();
$data['inputTypes'] = $results;
View
<label for="inputType">Input type:</label>
<?php echo form_dropdown('inputType', $inputTypes); ?>
This however doesn't create the desired effect. My dropdown gets populated, but because I have a multidimensional array the dropdown has optgroups.
I just want to have my selected data in a simple array. Why is this so freaking hard in CodeIgniter and php in general.
C# is much easier :/
Solution
The solution is very simple, use a foreach to loop through the multidimensional array in the model:
foreach($query->result() as $input_type) {
$data[] = $input_type->input_type_name;
}
return $data;
What do you mean by "This however doesn't create the desired effect" ?
Make sure you pass the $data to your view:
$this->load->view('viewname', $data);
The solution is very simple, use a foreach to loop through the multidimensional array in the model:
foreach($query->result() as $input_type) {
$data[] = $input_type->input_type_name;
}
return $data;
How would I write an Zend DB query to select all from the column ID?
So far I have tried:
public function getLatestUserID()
{
$ids = $this->select()
->where('id = ?');
return $ids;
}
But to no avail.
You just want the id column,
You failed to call an execute command.
try:
//assuming you are using a DbTable model
public function getLatestUserID()
{
$ids = $this->fetchAll('id');
return $ids;
}
I would do it like this, because I use the select() object for everything:
public function getLatestUserID()
{
$select = $this->select();
//I'm not sure if $this will work in this contex but you can out the table name
$select->from(array($this), array('id'));
$ids = $this->fetchAll($select);
return $ids;
}
The first two examples should return just the id column of the table, now if you actually want to query for a specific id:
public function getLatestUserID($id)
{
$select = $this->select();
$select->where('id = ?', $id);
//fetchAll() would still work here if we wanted multiple rows returned
//but fetchRow() for one row and fetchRowset() for multiple rows are probably
//more specific for this purpose.
$ids = $this->fetchRow($select);
return $ids;
}
make sure your class containing getLatestUserID does extend Zend_Db_Table_Abstract also :
$ids = $this->select()->where('id = ?'); can't work because where('id = ?'); expects an id value like where('id = ?', $id);
if what you want is the latest inserted row's Id use :
$lastInsertId = $this->getAdapter()->lastInsertId();
(however if you are using an oracle database this will not work and you should use $lastInsertId = $this->getAdapter()->lastSequenceId('USER_TABLE_SEQUENCE'); )
This should be straight forward if the row count is 0 I want to echo a message. Here is what I have:
public function getClubComment($id) {
$id = (int) $id;
$row = $this->fetchRow('club_id = ' . $id);
if (!$row) {
echo 'No comments';
}
return $row->toArray();
var_dump($row);
}
maybe try something like:
//not sure if this will work as I don't do this kind of request anymore
public function getClubComment($id) {
$id = (int) $id;
$row = $this->fetchRow('club_id = ?', $id);
if (!$row) {echo 'No comments';}
return $row->toArray();
var_dump($row);
}
I think you'll be happier doing something like this, takes most of the guess work out.
public function getClubComment($id) {
$id = (int) $id;
//create instance of Zend_Db_Select object
$select = $this select();
$select->where('club_id = ?', $id);
//fetchRow using Zend_Db_Select object
$row = $this->fetchRow($select);
//fetchRow() returns NULL so may as well test for that.
if ($row === NULL) {
throw new Zend_Db_Table_Exception();
}
return $row->toArray();
var_dump($row);
}
Zend_Db_Select is really useful to use in the models as it normally takes care of properly quoting values and it very easy to build a fairly complex sql query with any sql. In this example I used discrete lines for each part of the select() but I could as easily have strung them all together. I personally like each line separate so I can change or troubleshoot easily.
fetchRow returns an object, even if there is no results, that is why the condition in the if statement is always false, try this
if (!count($row))
I get a really anoying error when I try to edit an entry from a table, in tutorial they always use getTable()->find(), but I need to verify that the person logged in is the owner of that entry here what I did:
In the action:
public function executeEdit(sfWebRequest $request)
{
$id = $request->getParameter('id');
$userid = $this->getUser()->getGuardUser()->getId();
$ad = Doctrine_Core::getTable('BambinbazarArticles')->getMyAd($id, $userid);
$this->forward404Unless($ad, sprintf('Object bambinbazar_articles does not exist (%s).', $request->getParameter('id')));
$this->form = new BambinbazarArticlesForm($ad);
}
In the model:
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid);
return $q->execute();
}
I tried it with and without the ->execute(), did doctrine clean, cleared cache, rebuilded model,
Always get the same error 'The "%s" form only accepts a "%s" object.
If I use the Doctrine_Core::getTable('BambinbazarArticles')->find() it work, but of course, i need more than that..
I am becoming crazy over this.
execute() can return multiple rows; effectively you're getting a recordset back, rather than the individual object that your form is expecting. Try fetching a single object, using, e.g.:
return $q->execute()->getFirst();
or
return $q->fetchOne();
Its probably because your query is returning a Doctrine_Collection, not the actual Doctrine_Record youre expecting. Instead of execute use fetchOne.
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid)
->limit(1);
return $q->fetchOne();
}