Php scope question (referencing a function inside of another function) - php

I have the following code:
function process_bulk_action() {
if (isset($_GET['locations'])) {
$location_ids = ( is_array( $_GET['locations'] ) ) ? $_GET['locations'] : array( $_GET['locations'] );
global $wpdb;
switch ( $this->current_action() ) {
case 'edit':
bulk_edit($location_ids);
break;
case 'delete':
bulk_delete($locations_ids);
break;
default:break;
}
}
}
function bulk_delete($ids) {
foreach ( $ids as $id ) {
$id = absint( $id );
$sql = "DELETE FROM wp_nc_location WHERE location_id = $id";
$delete = $wpdb->query( $sql );
}
}
function bulk_edit($ids) {
foreach ( $ids as $id ) {
$id = absint( $id );
$sql = "SELECT name FROM wp_nc_location WHERE location_id = $id";
$select = $wpdb->query( $sql );
echo 'select: '. $select. ',';
print_r($select);
}
}
However I am getting the following error message when I try to call either bulk_edit or bulk_delete from inside that switch statement above:
Fatal error: Call to undefined function bulk_delete
I realize I am getting something wrong with the scope but I'm not sure where to put the functions bulk_edit or bulk_delete...

I'm guessing from your use of $this-> in various places that those functions belong to a class? In this case, you have to call the function like... $this->bulk_delete(..arguments..);

Related

Add result of php function to WP database

I have the following function to create a member_id:
add_action('user_register', 'generate_member_id');
function generate_member_id($user_id){
$unique_id = 1000 + get_current_user_id();
$chapter_id = fetch_chapter();
$member_id = "FAL-" . $chapter_id . "-" . $unique_id;
return $member_id;
}
It appears to work fine and I can call the function from a shortcode and get the correct value.
Now I want to add the returned value to a database and have tried using
update_user_meta()
$wpdb->update
$wpdb->insert
$wpdb->query
all with necessary arguments but nothing seems to work.
I expected this to do it, but it just generates a generic error that I am not able to debug (I have a problem with error logging):
global $wpdb;
$users = $wpdb->get_results( "SELECT ID FROM $wpdb->users" );
$generated_id = generate_member_id()
if( $users ) {
foreach ( $users as $user ) {
update_user_meta( $user->ID, 'member_id', generate_member_id() );
}
}
However if I change generate_member_id() to a string like 'memberId', it works and the database is updated. So I don't know what I'm doing wrong. Why can't I add' the result of the generate_member_id() function and to a WP database?
EDIT
add_action('user_register', 'fetch_chapter');
function fetch_chapter(){
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
global $wpdb;
$result = $wpdb->get_results('SELECT meta_value FROM usermeta WHERE meta_key = \'chapter_name\' AND user_id = '. $current_user->ID .' LIMIT 1');
if ($result[0]->meta_value == 'Peregrine') {
$result[0]->meta_value = 'PG';
}elseif ($result[0]->meta_value == 'Barbary') {
$result[0]->meta_value = 'BB';
}
return $result[0]->meta_value;
}
There are a few things I notice that could cause problems:
You have this line that (1) has no ; at the end and (2) it doesn't look like you need it anyway - you don't use $generated_id anywhere:
$generated_id = generate_member_id()
You are not passing the user_id into generate_member_id for your users in the loop calling update_user_meta...
... but even if you did pass in the user_id, generate_member_id isn't using it anyway - it is using the current user id. That is the wrong value for the users in your loop.
You need to fix your generate_member_id function to work with the user_id parameter. I assume you want this function to work without one too, so it still gets the get_current_user_id is no user id is passed in:
add_action('user_register', 'generate_member_id');
function generate_member_id($user_id){
// If no user_id is passed in, use the current user id
if (!$user_id) $user_id = get_current_user_id();
$unique_id = 1000 + $user_id;
$chapter_id = fetch_chapter_id();
$member_id = "FAL-" . $chapter_id . "-" . $unique_id;
return $member_id;
}
And then remove the erroneous $generated_id... line and pass the user_id to the generate_member_id in your update_user_meta:
global $wpdb;
$users = $wpdb->get_results( "SELECT ID FROM $wpdb->users" );
if( $users ) {
foreach ( $users as $user ) {
update_user_meta( $user->ID, 'member_id', generate_member_id($user->ID) );
}
}

PHP reproduce exact working sql query but no result

I've got a stored procedure which looks like this:
-- GET INVITES
DROP PROCEDURE IF EXISTS pda.get_invite;
DELIMITER //
CREATE PROCEDURE pda.get_invite ( code varchar(100), invitator_id bigint )
BEGIN
SELECT * FROM pda_invites
WHERE (pda_invites.invitator_id = invitator_id || invitator_id IS NULL)
AND (pda_invites.code = code || code IS NULL);
END //
CALL get_invite ( 'cec95191-23db-11e9-86d7-26374278e97f', NULL );
That works in phpmyadmin and deliver the desired result.
Now on php side, I've got some kind of query builder in a procedure class with registered arguments, which looks like this:
public function toSQL ( ) {
// Make sure params are set
if ( $this->ready == false ) {
throw new Exception ( 'Called unready procedure ' . $this->PROCEDURE_NAME . '.' );
return false;
}
// Build query
$sql= 'CALL ' . $this->PROCEDURE_NAME . '( ';
$i = 0;
foreach ( $this->args as $arg ) {
$param = $this->params[ $i ][ $arg['NAME'] ];
// Apply valid null values
if ( $param == null || $param == 'null' ) {
$sql = $sql . 'NULL';
// Apply varchar strings
} else if ( $arg['TYPE'] == 'varchar' ) {
$sql = $sql . '\'' . $param . '\'';
// Apply numeric values
} else {
$sql = $sql . $param;
}
if ( ($i+1) < $this->args_count ) {
$sql = $sql . ', ';
}
$i++;
}
$sql = $sql . ' );';
return $sql;
}
In my DB Service I trigger that by calling:
$sql = $procedure->toSQL();
$result = $con->query($sql);
echo($sql);
echo json_encode($result);
That results in following equivalent query string which is also reflected by the mysql log:
CALL get_invite( 'cec95191-23db-11e9-86d7-26374278e97f', NULL );
But the problem and question is that it produces an empty result:
{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
Have anyone a clue why it coulds behave this way?

mysqli_multi_query database class error

I am sure of this that this question hasn't asked anywhere even on the blog of creator of this class.
since morning i have started using php database class of bennettstone. I was trying to add MYSQLI_MULTI_QUERY into this class to handle multiple queries at once.
till now, I have been able to modify up to below extent. But it is not working. I am extremely poor with classes & this is my first attempt ever !!
I am getting error at two lines & don't know how i will access the result set of multiple queries. let's say 3 queries.
1) Error at while statement And similar error at mysqli_free_result statement
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given on line 352
Request some help:
public function multi_select_query( $query )
{
$i = 0;
$row = array();
$query = $this->link->multi_query( $query );
if ( mysqli_error( $this->link ) ) {
$this->log_db_errors( mysqli_error( $this->link ), $query, 'Fatal' );
return false;
} else {
do {
$i ++;
$result[$i] = mysqli_store_result($this->link);
while ( $r = mysqli_fetch_array( $query, MYSQLI_ASSOC )) {
$row[$i][] = $r;
}
mysqli_free_result( $query );
} while ( $this->link->more_results() && $this->link->next_result() );
return $row[$i];
}
}
Finally, I made it myself somehow.
Below is the final working class example which can be easily accessed by foreach loop using following statement.
$result = $dbc->multi_select_query($query);
public function multi_select_query( $query )
{
$row = array();
$i = 0;
$query = $this->link->multi_query( $query );
if( mysqli_error( $this->link ) ){
$this->log_db_errors( mysqli_error( $this->link ), $query, 'Fatal' );
return false;
} else {
do{
$i ++;
if($result[$i] = $this->link->store_result())
{
while ($r[$i] = $result[$i]->fetch_row())
{
$row[$i][] = $r[$i];
}
$result[$i]->free();
}
} while( $this->link->more_results() && $this->link->next_result() );
return $row;
}
}

access to var from template in php before compile

I want in template file can limit count of result, something like this :
file.tpl
{$box_count = 10}
{foreach $box}
.
.
.
{/foreach}
how can i handle and get this variables in php to limit result in database ?
example :
<?php
$smarty = new Smarty;
$count = ... //HOW CAN GET ?
$data = array();
$q = mysql_query( "SELECT * FROM `users` LIMIT 0,$count" );
while( $r = mysql_fetch_array( $q ) )
{
$data[] = $r;
}
$smarty->assign( 'box' , $data );
$smarty->dipslay( 'file.tpl' );
?>
note :
because this var just defined in tpl file, in getTemplateVars() not exists .
thanks
i solved this problem with registerObject :
<?php
$smarty = new Smarty;
class Test
{
public function setCount( $params , &$smarty )
{
$count = $params['count'];
$data = array();
$q = mysql_query( "SELECT * FROM `users` LIMIT 0,$count" );
while( $r = mysql_fetch_array( $q ) )
{
$data[] = $r;
}
$smarty->assign( 'box' , $data );
}
}
$S = new Test;
$smarty->registerObject( 'Test' , $S , array( 'setCount' ) )
$smarty->dipslay( 'file.tpl' );
?>
file.tpl :
{Test->setCount count="10"}
{foreach $box}
.
.
.
{/foreach}

function returning only once, why?

During my coding I really got stuck into this problem.
I ran a foreach loop and for every item I had to get a certain value from a function.
But I got only one returned. I could not figure out what was happening. I hope you guys surely will.
Below is the short version of my program.
Database structure is given at last.
<?php
function opendb() {
mysql_connect("localhost", "root", "root");
mysql_select_db("something_db");
}
function sql_query($sql) {
$datas = array();
if ($res = mysql_query($sql)) {
$x = 0;
while ( $data = mysql_fetch_assoc($res) ) {
$datas[$x] = $data;
$x += 1;
}
}
return $datas;
}
function get_parent_id($table, $parent, $cid) {
// cid=>child id
$sql = "SELECT * FROM $table WHERE id=$cid";
$datas = sql_query($sql);
$pid = $datas[0]['parent'];
$p_id = $datas[0]['id'];
if ($pid != 0) {
get_parent_id($table, $parent, $pid);
} else {
return $p_id;
}
}
opendb();
$datas_pkg = sql_query("SELECT * FROM tbl_packages WHERE 1");
foreach ( $datas_pkg as $data_pkg ) {
echo $data_pkg['destination_id'] . '-->';
echo $parent_id = get_parent_id('tbl_destinations', 'parent', $data_pkg['destination_id']);
echo '<br/>';
}
?>
Database structure..
tbl_destinations
+--------+-------------------------+-----------+
| id(int)|destination_name(Varchar)|parent(int)|
+--------+-------------------------+-----------+
tbl_packages
+-------+---------------------+-------------------+
|id(int)|package_name(varchar)|destination_id(int)|
+-------+---------------------+-------------------+
If I did not clear my question please let me know so that I can help you to help me.
if($pid!=0)
{
get_parent_id($table,$parent,$pid);
}
You call the function, but never use its value.

Categories