Display default value in the Grocery display table
- Single Page
Posted 30 August 2012 - 16:37 PM
I am new to Grocery Curd,
I have like it very much on the first time when i looked in to the CURD,
I am learning more on this,
I have got a issue with displaying the user defined default value for the query,
I have table A which is related to table B
which has fo rex:
Table A
id
iduser
idname
Table B
Id_b
iduser
price
table b may have value with iduser from table A but for some reason table A of corresponding iduser deleted,
So i am display a table with no action on it as of now, Just displaying
Iduser as user and price as price
And I want to add the default values say as "Deleted" if there is no iduser in Table 'A'
Ex : Currently i am getting as,
User Price
ABC 1
DEF 2
XYZ 3
4
UVW 5
expexted :
User Price
ABC 1
DEF 2
XYZ 3
DELETED 4
UVW 5
I want to display a default value as "deleted" in the place of missing users
Thanks in advance,
Posted 03 September 2012 - 07:16 AM
function example()
{
....
$crud->callback_column('idname',array($this,'user_name'));
....
}
function user_name($value, $row=NULL)
{
// var_dump($row);
return $row->idname?$row->idname:"DELETED";
}
Posted 03 September 2012 - 12:34 PM
Thanks for you reply,
I have missed relation to mention, Because of that may be the above code did not work for me.
Let me explain correctly.
I have a tables
user
&
affiliates
I have below structure for the both
User :
iduser
name
affiliate :
idaffiliate
iduser
when ever we delete user we are not removing corresponding entry from affiliate table
so i have below examples:
User tables;
1 ABC
2 DEF
4 XYZ
Affiliate function
idaffiliate iduser
1 1
2 2
3 3
4 4
where we don't have iduser 3 in the user table and below is my function
[php]function get_affiliate_CRUD() { $crud = new grocery_CRUD(); $crud->set_table('affiliate'); $crud->set_subject('Affiliate Report'); //$crud->set_relation('iduser', 'user', 'firstname'); $crud->callback_column('firstname',array($this,'user_name')); $crud->display_as('iduser', 'User'); $crud->unset_add(); $crud->unset_delete(); $crud->unset_edit(); $output = $crud->render(); return $output; } function user_name($value, $row=NULL) { //print_r($value);die(); return $row->firstname?$row->firstname:"DELETED"; }[/php]
Please let me know on this,
Thanks in advance.
Posted 03 September 2012 - 19:56 PM
Posted 03 September 2012 - 21:10 PM
Doesn't really work callback function for this columns. But there is a solution ))
public function test()
{
$crud = new grocery_CRUD();
$crud->set_table('test_affiliate');
// add a new column "test column"
$crud->columns('id','affiliate_id','price','test_colomm');
$crud->set_subject('Affiliate Reportt');
$crud->set_relation('user_id','test_user','firstname');
$crud->callback_column('test_colomm',array($this,'user_name'));
$output = $crud->render();
... inner code
}
function user_name($value, $row)
{
return $row->se8701ad4?$row->se8701ad4:'<h3 style="color:red">DELETED</h3>';
}
If you look at the query the database, you will see what you need field has a different name:
SELECT `test_affiliate`.*, je8701ad4.firstname AS se8701ad4
FROM (`test_affiliate`)
LEFT JOIN `test_user` as je8701ad4 ON `je8701ad4`.`iduser` = `test_affiliate`.`user_id`
Use this name for the callback function:
$row->se8701ad4
you may have a different name, be careful
Posted 03 September 2012 - 21:21 PM
Posted 03 September 2012 - 21:24 PM
--
-- Table structure for table `test_affiliate`
--
CREATE TABLE IF NOT EXISTS `test_affiliate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`affiliate_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`price` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `test_affiliate`
--
INSERT INTO `test_affiliate` (`id`, `affiliate_id`, `user_id`, `price`) VALUES
(1, 1, 1, 0),
(2, 2, 2, 0),
(3, 3, 3, 0),
(4, 4, 4, 0);
-- --------------------------------------------------------
--
-- Table structure for table `test_user`
--
CREATE TABLE IF NOT EXISTS `test_user` (
`iduser` int(11) NOT NULL AUTO_INCREMENT,
`firstname` text NOT NULL,
PRIMARY KEY (`iduser`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `test_user`
--
INSERT INTO `test_user` (`iduser`, `firstname`) VALUES
(1, 'ABC'),
(2, 'DEF'),
(4, 'XYZ');
helped you?
Posted 04 September 2012 - 16:02 PM
Great work.
Thanks for the clarification.
It worked fine for me.
Thanks a lot.
Posted 14 February 2014 - 11:23 AM
does not work