how to convert mysql_fetch_array in codeigniter? - codeigniter-3

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;

Related

Show All Product in Magento 2

I want to show all product if it is enable or disabled doesn't matter.
with this
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
return $collection;
I only get enabled product please help me with that to get disabled product also.
Found two solution on this, please try first one, if it does not work for you, then you can try 2nd one.
You can use disable stock check on your collection by this:
$productCollection = $this->_productFactory->create()->getCollection();
$productCollection->setFlag('has_stock_status_filter', false);
Or else you can use this:
$collection = $this->_productCollectionFactory->create()
->addAttributeToSelect('*')
->load();
// Patch to alter load and get disabled products too
$collection->clear();
$where = $collection->getSelect()->getPart('where');
foreach ($where as $key => $condition)
{
if(strpos($condition, 'stock_status_index.stock_status = 1') !== false){
$updatedWhere[] = 'AND (stock_status_index.stock_status IN (1,0))';
} else {
$updatedWhere[] = $condition;
}
}
$collection->getSelect()->setPart('where', $updatedWhere);
$collection->load();

How to fetch data as array in codeigniter

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.

Is this a valid way to check if db_row exists?

I am working with Zend and I needed to check whether a row in the DB already exists (A simple solution to get rid of the duplicate key error I was getting). I tried several things but nothing seemed to work... (for example the Zend_Validate_Db_NoRecordExists method)
So I wrote the following the code and I was wondering if this is a valid way to do it, or if I should do things differently:
In the model:
$where = $condition = array(
'user_id = ' . $user_id,
'page_id = ' . $page_id
);
$check = $this->fetchRow($where);
if(count($check) > 0) {
return null;
}else{
// Here I create a new row, fill it with data, save and return it.
}
And then in my view:
if($this->result != null) { /* do stuff */ }else{ /* do other stuff */ }
It does work but it does seem to take more time (duh, because of the extra query) and I am a bit unsure whether I should stick with this..
Any recommendation is welcome :)
Assuming you have coded your function in your controller
$row = $this->fetchRow($where); //If no row is found then $row is null .
if(!$row)
{
$row = $dbTb->createNew($insert); //$insert an associative array where it keys map cols of table
$row->save();
$this->view->row_not_found = true;
}
return $row;
In your view you can do this
if($this->row_not_found)
{
}else {
}

Zend Db query to select all IDs

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'); )

Creating Subtables in Piwik

I am new to piwik.Please help me in my issue.
Issue: I have to create 4 levesl of subtables.Currently I can able to create upto 2nd level,I mean table with one subtable per row.
Basically If I click on a table row I shoud get the subtable.If I click on subtable row again it should show inner subtable.
I need to create Table->subtable->subtable->subtable.
My code :
function getpageViewsLevel1($idSite, $date, $period)
{
$query = "select * from....";
$result = Piwik_FetchAll($query, array($idSite, $dateStart, $dateEnd));
// convert this array to a DataTable object
$dataTable = new Piwik_DataTable();
//Add subtable to each result
foreach ($result as $arr){
$piwik_row = new Piwik_DataTable_Row;
$piwik_row->setColumns($arr);
$subDataTable = new Piwik_DataTable();
$piwik_row->addSubTable($subDataTable);
$dataTable->addRow($piwik_row);
}
return $dataTable;
}
function getpageViewsLevel2($idSite, $date, $period, $idSubtable)
{
// Find selected parent row and retrieve data
$dataTable_old = $this->getpageViewsLevel1($idSite, $date, $period);
$row_old = new Piwik_DataTable_Row;
$row_old=$dataTable_old->getRowFromIdSubDataTable($idSubtable+1);
$tmp=$row_old->getColumns();
//Using $actionName in DB Query
$actionName=$tmp['pageTitle'].'%';
//db query
$query = "select * ....";
$result = Piwik_FetchAll($query, array($idSite, $dateStart, $dateEnd, $actionName));
// convert this array to a DataTable object
//$dataTable = new Piwik_DataTable();
foreach ($result as $arr){
$piwik_row = new Piwik_DataTable_Row;
$piwik_row->setColumns($arr);
$subDataTable = new Piwik_DataTable();
$piwik_row->addSubTable($subDataTable);
$dataTable->addRow($piwik_row);
}
return $dataTable;
}
Till here works fine.For the 3rd level,I am not able to apply the same logic as I dont have the 2ng level table ID.
function getpageViewsLevel3($idSite, $date, $period, $idSubtable)
{
// Find selected parent row and retrieve data
$dataTable_old = $this->getpageViewsLevel2($idSite, $date, $period, ___???????????????);
Please help me how can I proceed with this issue.Please let me know is there any other solution to do this.
Though if I pass some number like '1' for testing,
$dataTable_old = $this->getpageViewsLevel2($idSite, $date, $period, 1);----->this is not working.
I need to use parent row info in my DB query.
Thanks in advance for your help.