Creating Subtables in Piwik - plugins

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.

Related

how to convert mysql_fetch_array in codeigniter?

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;

Zend Paginate - find a specific record within the result

I appreciate that this may not be possible, but is there a way to make Zend Paginate go to a specific item (record)?
The result I would like would allow me to seek a specific record in a tabled list of results, and display the appropriate page (within all available pages) combined with a name anchor tag to display the specific record.
To clarify: If I had the results as a Zend_Db_Table_Rowset_Abstract I would use the seek() method in a similar fashion to $rowset->seek(8); Although I don't believe the result returned by the DbSelect adapter is a SeekableIterator?
The code within my Mapper (using the Table Data Gateway pattern):
public function paginate($where = array(), $order = null)
{
$select = $this->getDbTable()->select()->from($this->getTableName(), $this->getTableFields());
foreach ($where as $key => $value) {
$select->where($key, $value);
}
$select->order($order);
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$paginator = new Zend_Paginator($adapter);
return $paginator;
}
Within my controller:
$cache_id = sha1('list');
$mapper = new Application_Model_Galleries_Mapper();
if(!($data = Zend_Registry::get('cache')->load($cache_id))) {
$data = $mapper->paginate(array(), $sort);
Zend_Registry::get('cache')->save($data, $cache_id, array('list'), 7200);
}
$data->setCurrentPageNumber($this->_getParam('page'));
$data->setItemCountPerPage(30);
$this->view->paginator = $data;
To return a Zend_Paginator with a seekable iterator (Zend_Db_Table_Rowset) use the Zend_Paginator_Adapter_DbTableSelect() as it returns a rowset object, as opposed to Zend_Paginator_Adaoter_DbSelect() which returns an array().
Zend_Paginator

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

Zend Framework Table Relationships - select from multiple tables within app

I hope I'm asking this question in an understandable way. I've been working on an app that has been dealing with 1 table ( jobschedule ). So, I have models/Jobschedule.php, models/JobscheduleMapper.php, controllers/JobscheduleController.php, view/scripts/jobschedule/*.phtml files
So in my controller I'll do something like this:
$jobnumber = $jobschedule->getJobnum();
$jobtype = $jobschedule->getJobtype();
$table = $this->getDbTable();
public function listAction()
{
$this->_helper->layout->disableLayout();
$this->view->jobnum = $this->getRequest()->getParam( 'jobnum', false );
$this->view->items = array();
$jobschedule = new Application_Model_Jobschedule();
$jobschedule->setJobnum( $this->view->jobnum );
$mapper = new Application_Model_JobscheduleMapper();
$this->view->entries = $mapper->fetchAll ( $jobschedule );
}
and then in my mapper I I do something like:
$resultSet = $table->fetchAll($table->select()->where('jobnum = ?', $jobnumber)->where('jobtype = ?', $jobtype) );
$entries = array();
foreach ($resultSet as $row) {
$entry = new Application_Model_Jobschedule();
$entry->setJobnum($row->jobnum)
->setJobtype($row->jobtype)
->setJobdesc($row->jobdesc)
->setJobstart($row->jobstart)
->setJobend($row->jobend)
->setJobfinished($row->jobfinished)
->setJobnotes($row->jobnotes)
->setJobid($row->jobid);
$entries[] = $entry;
}
return $entries;
}
Then in my view I can manipulate $entries. Well, the problem I'm coming across now is that there is also another table called 'jobindex' that has a column in it called 'jobno'. That 'jobno' column holds the same record as the 'jobnum' column in the 'jobschedule' table. I need to find the value of the 'store_type' column in the 'jobindex' table where jobindex.jobno = joschedule.jobnum ( where 1234 is the jobno/jobnum for example ). Can someone please help me here? Do I need to create a jobindex mapper and controller? If so, that's done ... I just don't know how to manipulate both tables at once and get the record I need. And where to put that code...in my controller?
If I understand you correctly this is the SQL query you need to extract the data from database:
SELECT `jobschedule`.* FROM `jobschedule` INNER JOIN `jobindex` ON jobindex.jobno = jobschedule.jobnum WHERE (jobindex.jobtype = 'WM')
Assembling this SQL query in Zend would look something like this:
$select->from('jobschedule', array('*'))
->joinInner(
'jobindex',
'jobindex.jobno = jobschedule.jobnum',
array())
->where('jobindex.jobtype = ?', $jobtype);
Let us know if that's what you are looking for.
If I'm understanding you correctly, you'll want to join the 'jobindex' table to the 'jobschedule' table.
...
$resultSet = $table->fetchAll(
$table->select()->setIntegrityCheck(false)
->from($table, array('*'))
->joinLeft(
'jobindex',
'jobindex.jobno = jobschedule.jobnumber',
array('store_type'))
->where('jobnum = ?', $jobnumber)
->where('jobtype = ?', $jobtype)
->where('jobindex.store_type = ?', $_POST['store_num'])
);
....
Depending on how 'jobschedule' is related to 'jobindex', you may want an inner join (joinInner()) instead.
The setIntegrityCheck(false) disables referential integrity between the tables, which is only important if you are writing to them. For queries like this one, you can just disable it and move on (else it will throw an exception).

How to copy DataGridView contents to Dataset?

i'm not good with ADO.NET
so used the following code that i got from Internet
but i get the error "There is no row at position 0." # the marked line(*)
even though i can see a value is being passed using breakpoints
DataSet ds = new DataSet();
DataTable dt = new DataTable("ProdFromDGV");
ds.Tables.Add(dt);
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col.HeaderText, typeof(string));
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
*dt.Rows[row.Index][cell.ColumnIndex] = cell.Value.ToString();*
}
}
dt.WriteXml("table.xml");
You need to first create a DataRow type and add it into your DataTable before you can start assigning to it.
So your code will now be something like:
DataSet ds = new DataSet();
DataTable dt = new DataTable("ProdFromDGV");
ds.Tables.Add(dt);
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col.HeaderText, typeof(string));
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
// This will not work, but it's something similar to this that you need here...
DataRow row = new DataRow();
dt.RowCollecion.Add(row);
// Now you can assign to the row....
dt.Rows[row.Index][cell.ColumnIndex] = cell.Value.ToString();
}
}
dt.WriteXml("table.xml");
Hope this helps some..
// This will not work, but it's something similar to this that you need here...
Just change
DataRow row = new DataRow();
to:
DataRow row = dt.NewRow();
and it will work.