The reason why the code doesn't work is that the same array index is being set twice. In that case, only the last value set is kept. That code is exactly equivalent to the following one.
function mymodule_menu() {
$items['myUrl/dboperation'] = array(
'title' => 'some custom db operation for delete',
'page callback' => 'mydboperation2',
'access callback' => TRUE,
'file' => 'my_dboperation.inc',
);
return $items;
}
If you want to make it work, you have two possibilities: Define two different paths, or use wildcard arguments.
In the first case, you need to make the code similar to the following one.
function mymodule_menu() {
$items['myUrl/dboperation/insert'] = array(
'title' => 'Insert data in the database',
'page callback' => 'mymodule_database_insert',
'access callback' => TRUE,
'file' => 'my_dboperation.inc',
);
$items['myUrl/dboperation/delete'] = array(
'title' => 'Delete data from the database',
'page callback' => 'mymodule_database_delete',
'access callback' => TRUE,
'file' => 'my_dboperation.inc',
);
return $items;
}
I changed the callback names because in Drupal it is preferable that functions implemented by a module are prefixed with the machine name of the module, to avoid conflicts with other modules.
The second alternative is using wildcard arguments, which allows to pass arguments to the page callback, making it more helpful. It also make extending the code easier, and less error-prone, since there isn't the need to add a new route for new operations.
In the following code, I am using the first argument to decide the operation that must be done on the database, while the second argument is a generic one, and it could be used to decide on which data the operation must be performed.
function mymodule_menu() {
$items['myUrl/dboperation/%/%'] = array(
'title' => 'Change data in the database',
'page callback' => 'mymodule_database_operation',
'page arguments' => array(2,3),
'access callback' => TRUE,
'file' => 'my_dboperation.inc',
);
return $items;
}
function mymodule_database_operation($op, $arg) {
switch $op {
case 'insert':
// Insert data.
break;
case 'delete':
// Delete data.
break;
}
}
/myFile.php?op=insert
so I just wanted to check how the drupal hook works.