How to update data with the help of PODSCMS? - php

Hello All,
I know how to fetch record with the help of podscms,
But I would like to update the record fetched by podscms.
like
$somePod = pods('somepod')->update($my_array);
Anybody have some suggestion,

This is available in pods()->save() - http://pods.io/docs/save/
<?php
// Get the book item with an ID of 5
$pod = pods( 'book', 5 );
// Set the author (a user relationship field)
// to a user with an ID of 2
$pod->save( 'author', 2 );
// Set a group of fields to specific values
$data = array(
'name' => 'New book name',
'author' => 2,
'description' => 'Awesome book, read worthy!'
);
// Save the data as set above
$pod->save( $data );
// Or the shorthand
$id = pods( 'yourpod', $id )->save( $data );
Also available is add() - http://pods.io/docs/add/

Related

How to add nested data into MySQL tables using PHP

I have 3 tables and I'm working with wordpress too: wp7s_g_bookings, wp7s_g_tests and wp7s_g_clients. This is a study project, not to sell.
I want to be able to save 1 booking with X clients and Y tests.
I read about LAST_INSERT_ID() but I don't think it will work in this case and I'm lost.
Possible scenarios:
You are a partner, then the data is beeing pulled, so we have the ID because it exist already;
In this case we are adding other users data inside repeatable fields like name="client_birth[]" ... // client_vat only show/exist if the partner don't exist.
You are a normal client, in this case you fill the same client data plus client_vat (so I'm sure when adding into database that only the guy that have client_vat is the booking_client)
if (!empty($_POST)) {
$wpdb->query('START TRANSACTION');
//All this fields are repeatable inputs, except client_vat that can only be the 1st record. I think it's missing a foreach
$default = array(
'client_name' => $_POST['client_name'],
'client_birth' => $_POST['client_birth'],
'client_sns' => $_POST['client_sns'],
'client_vat' => $_POST['client_vat'],
'client_city' => $_POST['client_city'],
'client_email' => $_POST['client_email'],
'client_phone' => $_POST['client_phone']
);
$item = shortcode_atts( $default, $_REQUEST );
$addClients = $wpdb->insert( 'wp7s_g_clients', $item );
$default = array(
'test_client' => ???, //ID of previous client that have client_vat filled [0]
);
$item = shortcode_atts( $default, $_REQUEST );
$addTests = $wpdb->insert( 'wp7s_g_tests', $item );
$collectDate = date('Y-m-d H:i:s', strtotime($_POST['booking_collect_date']));
$default = array(
'booking_status' => 1,
'booking_partner' => $_POST['booking_partner'], //ID of the partner, if not empty
'booking_client' => ???, //If partner don't exist -> ID of client that have client_vat filled
'booking_type' => $_POST['booking_type'],
'booking_clients' => ???, //Array of all IDs previously added in wp7s_g_clients table
'booking_city' => $_POST['booking_city'],
'booking_address' => $_POST['booking_address'],
'booking_collect_date' => $collectDate,
'booking_payment' => $_POST['booking_payment'],
'booking_amount' => $_POST['booking_amount'],
'booking_obs' => nl2br($_POST['booking_obs']),
);
$item = shortcode_atts( $default, $_REQUEST );
$addBookings = $wpdb->insert( 'wp7s_g_bookings', $item );
if($addClients && $addTests && $addBookings) {
$wpdb->query('COMMIT');
$msg = "Success";
wp_redirect( esc_url( get_page_link( 6 ) ) );
exit;
}
else {
$wpdb->query('ROLLBACK');
$msg = "Error";
}
}
My issues are adding this properly into database the repeatable fields and using a previous created ID(s).
I tried to explain everything and comment so it's better to understand.
After you perform a query with $wpdb you can access the $wpdb->insert_id to get the last inserted id. You can find more details on the documentation page of wpdb
If you need more ids because u have more $wpdb->insert all you need to do is store $wpdb->insert_id after each insert, like:
...
$item = shortcode_atts( $default, $_REQUEST );
$addClients = $wpdb->insert( 'wp7s_g_clients', $item );
$clientId = $wpdb->insert_id;
....
$default = array(
'test_client' => ???, //ID of previous client that have client_vat filled [0]
);
$item = shortcode_atts( $default, $_REQUEST );
$addTests = $wpdb->insert( 'wp7s_g_tests', $item );
$testId = $wpdb->insert_id;
...
rest of your code

Programmatically update Menu Item in Component in joomla

I'm creating a component in Joomla, where among other things, can create and update menus, and I have successfully used this answer to create a new menu item.
https://joomla.stackexchange.com/questions/5104/programmatically-add-menu-item-in-component?newreg=1e7c576205354c0795a55607bc3e2508
$menuTable = JTableNested::getInstance('Menu');
// which menu you want to add to -
$menutype = 'thisismymenusname';
// this is heading menu item but what data you have and require will vary per case - just look at an appropriate row in yr menu table
$menuData = array(
'menutype' => $menutype,
'title' => $table->alias,
'alias' => $table->alias,
'path' => $table->alias,
'type' => 'heading',
'component_id' => 0,
'language' => '*',
'published' => 1,
);
// this item is at the root so the parent id needs to be 1
$parent_id = 1;
$menuTable->setLocation($parent_id, 'last-child');
// save is the shortcut method for bind, check and store
if (!$menuTable->save($menuData))
{
$this->setError($menuTable->getError());
return false;
}
But I'm unable to find the correct way to update a menu item, like the title and alias. But it is unsuccessful. I have tried using this with no success.
$menu_title = $_POST['title'];
$menu_alias = JFilterOutput::stringURLSafe($menu_title);
$menuTable = JTableNested::getInstance('Menu');
// this is heading menu item but what data you have and require will vary per case - just look at an appropriate row in yr menu table
$menuData = array(
'title' => $menu_title,
'alias' => $menu_alias,
);
// this item is at the root so the parent id needs to be 1
$parent_id = ($_POST['parent'] != "" ? $_POST['parent'] : '1');
$menuTable->setLocation($parent_id, 'last-child');
// save is the shortcut method for bind, check and store
if (!$menuTable->set($_POST['menuid'],$menuData)) {
$this->setError($menuTable->getError());
exit;
}
Can anyone help?

Wordpress - Update post by meta information

I'm looking for a solution, where I can update posts by meta information and not by id.
I got my content from an external resource via jsonp. I save the content into the database.
The only way I have found is, to update a post by id - but I have no id. What I have, is an article ID from the other resource. I save this article id to every wp post as meta information.
So, is there any way, to update a post by it's meta information?
Ok,
here is some code and what I have done right now:
public function saveArticleInWpDatabase($json_data){
for ($i = 0; $i < count($json_data); $i++) {
$articleId = $json_data[$i]['articleId'];
$img = $json_data[$i]['img'];
$articleHeadline = $json_data[$i]['articleContent']['headline'];
$articleContent = $json_data[$i]['articleContent']['content'];
$termsInput = $json_data[$i]['articleContent']['terms'];
$terms = explode(',',$termsInput);
$creationDate = $json_data[$i]['articleCreationDate'];
$modifiedDate = $json_data[$i]['articleModifiedDate'];
//Check if post with article ID in postmeta exists
$existingId = $this->wpdb->get_var("SELECT post_id FROM ".$this->wpdb->postmeta." WHERE (meta_key = 'article_id' AND meta_value = '".$articleId."')");
//Posts doesn't exist
if ($existingId == null) {
$my_post = array(
'post_date' => $creationDate,
'post_modified' => $modifiedDate,
'post_title' => $articleHeadline,
'post_content' => $articleContent,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(0, $categoryId)
);
$last_id = wp_insert_post($my_post);
add_post_meta($last_id, 'article_id', $articleId, true);
wp_set_post_categories($last_id, array(0,$categoryId), true);
wp_set_post_terms( $last_id,$terms, 'post_tag', true );
//$this->_import_photo($last_id, $img);
}
//Post exists
else{
$my_post = array(
'ID' => $existingId,
'post_date' => $creationDate,
'post_modified' => $modifiedDate,
'post_title' => $articleHeadline,
'post_content' => $articleContent,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(0, $categoryId)
);
//Get last modified date and compare it with the new one
$lastUpdate = $this->wpdb->get_var("SELECT post_modified FROM " . $this->wpdb->posts . " WHERE ID = ".$existingId);
if(strcmp($lastUpdate, $modifiedDate) != 0){
var_dump("Update article with id: " . $existingId);
$last_id = wp_update_post($my_post);
}
else{
var_dump("Article: " . $existingId . " Nothing changed");
}
}
}
}
As you can see, I save the unique article number from the other CMS if the article doesn't exists.
If the article with the meta_key: article_id and the current value exist, I check if the modified date is the same or not. If not, I will update the posts.
I call this function via a unix cron job every minute. But I think, that this is not the best way (performance)(too many db requests?).
The other think is, that the post_modified value is always the same as post_date, but I got two different values in my json.
e.g.
"articleCreationDate": "2012-05-21 14:38:29", "articleModifiedDate": "2016-02-11 14:52:01"
But it only saved the articleCreationDate in both columns(post_date, post_modified).
So, did anyone has a better solution or idea? And did anyone know, why it doesn't save the modified date value from my json?
Cheers

How to update the quantity of all items in the cart with respect to database?

I am doing a shopping cart using code-igniter. While I can do the add cart functions, I'm somewhat confused with how to update them with regards to database. All I want is when I change the item quantity in the cart and click update ,both the cart and database must be updated for that particular item.I tried to do it but couldn't get it.Can someone please enlighten me what to do?
controller code
$this->load->model('user_m');
$result['query'] = $this->user_m->get($id); // inserts coresponing item
foreach ($result['query'] as $row)
$id = $row->pid;
$qty = $a;
$quan=$row->quantity;
$price = $row->pprice;
$name = $row->pname;
$q=$quan-$a; // for remainig stock i.e total qty-user qty
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'stock' =>$q
);
$this->cart->insert($data);
$this->load->model('user_m');
$result['qry'] = $this->user_m->up_cart($id,$q);
redirect($_SERVER['HTTP_REFERER']);
}
just tell me how to update pls!
when ever you add any product in cart, it will generate an uniq row ID and that will be unique for each and every product and hence you need to update quantity by that row ID
$data = array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
);
$this->cart->update($data);
// Or a multi-dimensional array
$data = array(
array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
),
array(
'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
'qty' => 4
),
array(
'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
'qty' => 2
)
);
$this->cart->update($data);
Now you want to know about how you will get row id.
$cart=$this->cart->contents();
it will return whole array of cart and when you listing it on webpage, please associate row id with quantity text box to update that particular product.
please check following url:
https://ellislab.com/codeigniter/user-guide/libraries/cart.html

PHP. How to take data from 2 mysql tables instead of 1

Learning php and I am losing my mind trying to solve this for days now. Please help.
This is a code which goes thought a table COUPON, take data with a condition met, and download it afterwards. In this table COUPON I have USER_ID as number but I want to have a user name also, which is kept in another table USER.
How can I go to another table (USER) and take names (REALNAME) by this USER_ID which is the same in both tables?
if ( $_POST ) {
$team_id = abs(intval($_POST['team_id']));
$consume = $_POST['consume'];
if (!$team_id || !$consume) die('-ERR ERR_NO_DATA');
$condition = array(
'team_id' => $team_id,
'consume' => $consume,
);
$coupons = DB::LimitQuery('coupon', array(
'condition' => $condition,
));
if (!$coupons) die('-ERR ERR_NO_DATA');
$team = Table::Fetch('team', $team_id);
$name = 'coupon_'.date('Ymd');
$kn = array(
'id' => 'ID',
'secret' => 'Password',
'date' => 'Valid',
'consume' => 'Status',
);
$consume = array(
'Y' => 'Used',
'N' => 'Unused',
);
$ecoupons = array();
foreach( $coupons AS $one ) {
$one['id'] = "#{$one['id']}";
$one['consume'] = $consume[$one['consume']];
$one['date'] = date('Y-m-d', $one['expire_time']);
$ecoupons[] = $one;
}
down_xls($ecoupons, $kn, $name);
After this, I want to try to do the same thing using only SQL queries.
You would need to JOIN the tables in the SQL query
SELECT something FROM coupons as coupons JOIN user as user ON coupons.id=user.id
You should use join when you want to retrieve details from two tables.
Join table COUPON and table USER based on user_id . This should yield results you want.

Categories