N
N
Newn2018-05-24 15:02:34
PHP
Newn, 2018-05-24 15:02:34

How to update records in a table in a wordpress loop?

Good afternoon. Need help with loop, can't figure out how to update records in wp table.
I am getting data from a table. In this manner

$t_name = $wpdb->prefix . 'buy_';
$query = $wpdb->get_results("SELECT id,cours,perc FROM ".$t_name." WHERE perc_on='yes'");

If you output data in a loop using foreach, then output
Did it like this
$var = '';
  foreach ($query as $q ):
    $var .= $q->perc;
    $id .=$q->id;
endforeach;

Should be updated like this
$update = $wpdb->update(''.$t_name.'',array('cours'=>$course*$var),array('id'=>$id]));

In this case, the final variables go to 1 line and the request is incorrect, or the result will be only 1 value of the last element, if you remove the variable $var = '';
I can not figure out that the data would be updated for all id that are in the id variable. Help))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2018-05-25
@Newn

$id = array_map(function($row) { return $row->id; }, $query);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question