Wordpress multiple input custom fields query - serialization

I have managed to save a custom meta field with multiple inputs to a post. The inputs are going like this:
<input type="text" name="menuitem[1][title]" />
<input type="text" name="menuitem[1][section]" />
<input type="text" name="menuitem[1][price]" />
<input type="text" name="menuitem[2][title]" />
<input type="text" name="menuitem[2][section]" />
<input type="text" name="menuitem[2][price]" /> etc.
And the data are stored in serialized arrays.
The reason I do it this way, is because I want to query the values of this field, related to each other. (e.g. to query posts sorted by menuitem[price] of menuitem[title]).
The problem is that WP-query can only read a single key value and cannot read serialized data.
Is there any better way to store meta keys so they are related to each other?
Any suggestions would be appriciated.
Thank you

You have to use foreach
Example:
<?php
$entries = get_post_meta($post->ID, '_your_metabox_group_id', true );
foreach ( (array) $entries as $key => $entry ) {
$title = $section = $price = '';
if ( isset( $entry['_your_title_meta_id'] ) )
$title = get_post_meta($post->ID, '_your_title_meta_id', true);
if ( isset( $entry['_your_section_meta_id'] ) )
$section = get_post_meta($post->ID, '_your_section_meta_id', true);
if ( isset( $entry['_your_price_meta_id'] ) )
$price = get_post_meta($post->ID, '_your_price_meta_id', true);
// your output e.g
// echo '<span class="some-class">'.$title.'</span>';
}
?>

Related

Insert Data Into Wordpress Database Custom Table From Custom Form

Insert data into Wordpress database table from a custom form
I have worked with your example but I have a problem. I have error on the start. I get this message:
WordPress database error: [Cannot add or update a child row: a foreign
key constraint fails (prowebex_barbara.barbara_schedule, CONSTRAINT
barbara_schedule_ibfk_1 FOREIGN KEY (day_id) REFERENCES barbara_days
(id_day))]
INSERT INTO barbara_schedule (id, challenger, challenged,
date_match, day_id, term_start, term_end) VALUES ('', 'gfdgdfgdfg', 'gdfgfdgdf', '2015-11-30', '', '23:00', '00:59');
Data Submitted
Can you tell me how can I change this. I have already, truncate tables,delete both tables. I don't know what to do.
<?php
if(isset($_POST['submit'])) {
global $wpdb;
$id = sanitize_text_field($_POST['id']);
$challenger = sanitize_text_field($_POST['challenger']);
$challenged = sanitize_text_field($_POST['challenged']);
$date_match = sanitize_text_field($_POST['date_match']);
$day = sanitize_text_field($_POST['day_id']);
$term_start = sanitize_text_field($_POST['term_start']);
$term_end = sanitize_text_field($_POST['term_end']);
$table_name = $wpdb->prowebex_barbara.barbara_schedule;
$wpdb->insert($table_name, array(
'id' => $id,
'challenger' => $challenger,
'challenged' => $challenged,
'date_match' => $date_match,
'day_id' => $day,
'term_start' => $term_start,
'term_end' => $term_end,
),
array( '%s', '%s', '%s', '%s', '%s', '%s', '%s')
);
$msg = "Data Submited";
echo $msg;
}?>
<form action="" method="post" id="subForm">
<div>
<input type="hidden" name="id" id="iiihuu-iiihuu" placeholder="" />
<br>
<input type="text" name="challenger" id="iiihuu-iiihuu" placeholder="Izazivač" />
<br>
<input type="text" name="challenged" id="iiihuu-iiihuu" placeholder="Izazvani" />
<br>
<input type="date" name="date_match" id="iiihuu-iiihuu" placeholder="Datum" />
<br>
<select>
<option>I don't know how to do this</option>
</select>
<br>
<input type="time" name="term_start" id="iiihuu-iiihuu" placeholder="Termin od" />
<br>
<input type="time" name="term_end" id="iiihuu-iiihuu" placeholder="Termin do" />
<br>
<input type="submit" name="submit" value="Spasi" class ="submit_button" id="formSubmit" />
</div>
</form>
</div>
The errors look like that the custom tables actually have relationship with each other, not sure how the database structure is, but it looks like in order to create a record in barbara_schedule you will have to also create a record in barbara_days.
So in order to just check if this is the case you should pass a fake day_id. So replace $_POST['day_id'] with an existing day_id that already exists in the barbara_days table.
But if thats not the case, then here is a quick thought, do you have a require statement on the top of this file?
Usually this is needed in order to make an insert on custom tables besides the meta table.
require_once('LINK-TO/wp-load.php');
Secondly i am not sure, what this outputs
$table_name = $wpdb->prowebex_barbara.barbara_schedule;
but just in case, try to hardcode the table name like this
$table_name = $wpdb->prefix."table-name"

Insert function using a radio button

So my problem is that I am unable to insert the data into the selected table using the radio buttons. The radio buttons are used to tell us which table we need to access and to add the data. I thought that I had it correct but it did not work. Here is what I have. Any kind of help would be greatly appreciated.
<h1 align="center">Event</h1>
<h1 align="center">Storage</h1>
<form action="set_event.php" method="post">
<p>
<p>
<input type="submit" name="submit" value="Set Items">
</p>
<p>
<br>
<label>
<input type="radio" name="storage" value="concourse_stairs_s" id="s1">
Concourse Stairs</label>
<br>
<label>
<input type="radio" name="storage" value="bat_cave_s" id="s2">
Bat Cave</label>
<br>
<label>
<input type="radio" name="storage" value="fireside_s" id="s3">
Fireside Storage</label>
<br>
</p>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require ('mysqli_connect.php'); // Connect to the db.
$errors = array(); // Initialize an error array.
}
if (isset($_REQUEST['bat_cave_s']))
{
$q = "INSERT INTO bat_cave_s (item_name, check_in) VALUES ('test', NOW())";
$r = #mysqli_query ($dbc, $q); // Run the query.
}
else if(isset($_REQUEST['concourse_stairs_s']))
{
$q = "INTO concourse_stairs_s (item_name, check_in) VALUES ('test', NOW())";
$r = #mysqli_query ($dbc, $q); // Run the query.
}
else if(isset($_REQUEST['fireside_s']))
{
$q = "INSERT INTO fireside_s (item_name, check_in) VALUES ('test', NOW())";
$r = #mysqli_query ($dbc, $q); // Run the query.
}
NOW())";
Your PHP is checking the wrong thing.
You need to check the value of $_REQUEST['storage'].
if($_REQUEST['storage'] == 'bat_cave_s') {

Inserting into the db with PDO

<form action="uploads.php" method="post" enctype="multipart/form-data" id="upload" class="upload">
<fieldset>
<legend>Upload</legend><br/>
Title: <input type="text" name="name" id="name" class="name" required> <br/><br/>
<textarea name="description" rows="6" cols="35" maxlength="120"></textarea><br/>
<input type="file" id="file" name="file[]" required multiple> <br/>
<input type="submit" id="submit" name="submit" value="Upload">
</fieldset>
<div class="bar">
<span class="bar-fill" id="pb"><span class="bar-fill-text" id="pt"></span></span>
</div>
<div id="uploads" class="uploads">
Uploaded file links will appear here.
</div>
<?php
// configuration
$dbhost = "localhost";
$dbname = "blog";
$dbuser = "root";
$dbpass = "pass";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$name = 'name';
$mime = 'mime';
$data = 'data';
$size = 'size';
$description = 'description';
$created = 'created';
$url = 'url';
// query
$sql = "INSERT INTO videos (name,mime,data,size,description,created,url) VALUES (:name,:mime,:data,:size,:description,:created,:url)";
$q = $conn->prepare($sql);
$q->execute(array(':name'=>$name,
':mime'=>$mime,
':data'=>$data,
':size'=>$size,
':description'=>$description,
':created'=>$created,
':url'=>$url));
?>
I'm not so good with PDO, I can get videos to upload to my db, but I can't take in a name or anything. It just shows: name, description, size is 0 and etc. I've watched a few tutorials, but none of them show how to add it by what the user names it or describes it as, only what they put into the values goes to the database. I've also searched around on here and many other websites, but no luck.

OpenCart text at login page doesnt get changed if I change lines in language files

I run an opencart shop 1.5.6.4 with vqmod installed.
I installed xml from opencart in order to force a login before reaching my shop. So it's a closed shop. I customized my login page inside css to hide all menu bars and I "copied" the language selector from the shop into the login page.
It's working, but since then the language texts from the connected language files don't get recognized anymore and the login page texts are like "entry_password". But in the language folders/files I connected them correctly.
Do you have any hints how to fix this?
Yes, I could write them directly into my login.tpl but then I would only have one language available.
I tried to uninstall all vqmod xmls via vqmod manager and got the same result.
It looks like the language isn't loaded correctly, but I really don't know why.
This is my login.tpl in catalog/view/theme/default/template/account:
<?php echo $header; ?>
<style>
INLINE CSS
</style>
<div id="banner">
<center><img src="BANNER URL" alt="header" /></center><br/>
</div>
<?php if ($success) { ?>
<div class="success">Login erfolgreich</div>
<?php } ?>
<?php if ($error_warning) { ?>
<div class="warning">Login fehlgeschlagen</div>
<?php } ?>
<div id="content">
<div class="login-content">
<div class="left">
<h2><?php echo $text_new_customer; ?></h2>
<div class="content">
<p><b><?php echo $text_register; ?></b></p>
<p><?php echo $text_register_account; ?></p>
<?php echo $button_continue; ?></div>
</div>
<div class="right">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data">
<div id="language_selector">Sprache | Language<br>
<img src="image/flags/de.png" alt="Deutsch" title="Deutsch" onclick="$('input[name=\'language_code\']').attr('value', 'de'); $(this).parent().parent().submit();">
<img src="image/flags/gb.png" alt="English" title="English" onclick="$('input[name=\'language_code\']').attr('value', 'en'); $(this).parent().parent().submit();">
<img src="image/flags/nl.png" alt="Nederlands" title="Nederlands" onclick="$('input[name=\'language_code\']').attr('value', 'nl'); $(this).parent().parent().submit();">
<input type="hidden" name="language_code" value="">
<input type="hidden" name="redirect" value="http://MYDOMAIN/opencart/index.php?route=account/login">
</div>
<div class="content">
<h2>Login</h2>
<b>E-Mail:</b><br />
<input type="text" name="email" value="<?php echo $email; ?>" />
<br />
<br />
<b>Password</b><br />
<input type="password" name="password" value="<?php echo $password; ?>" />
<br />
<?php echo $text_forgotten; ?><br />
<br />
<input type="submit" value="<?php echo $button_login; ?>" class="button" />
<?php if ($redirect) { ?>
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
<?php } ?>
</div>
</form>
</div>
</div>
<?php echo $content_bottom; ?></div>
<script type="text/javascript"><!--
$('#login input').keydown(function(e) {
if (e.keyCode == 13) {
$('#login').submit();
}
});
//--></script>
<?php echo $footer; ?>
I want to change the div class content in order to make the h2 and b fields in multilanguage by reading it out from the language-php files:
<div class="content">
<h2><?php echo $text_login; ?></h2>
<b><?php echo $entry_email; ?></b><br />
<input type="text" name="email" value="<?php echo $email; ?>" />
<br />
<br />
<b><?php echo $entry_password; ?></b><br />
<input type="password" name="password" value="<?php echo $password; ?>" />
<br />
<?php echo $text_forgotten; ?><br />
<br />
<input type="submit" value="<?php echo $button_login; ?>" class="button" />
<?php if ($redirect) { ?>
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
<?php } ?>
</div>
this is a language-file login.php example: (located in /catalog/language/english/account/)
<?php
// Heading
$_['heading_title'] = 'Account Login';
// Text
$_['text_account'] = 'Account';
$_['text_login'] = 'Login';
$_['text_new_customer'] = 'New Customer';
$_['text_register'] = 'Register Account';
$_['text_register_account'] = 'By creating an account you will be able to shop faster, be up to date on an order\'s status, and keep track of the orders you have previously made.';
$_['text_returning_customer'] = 'Returning Customer';
$_['text_i_am_returning_customer'] = 'I am a returning customer';
$_['text_forgotten'] = 'Forgotten Password';
// Entry
$_['entry_email'] = 'E-Mail Address:';
$_['entry_password'] = 'Password:';
// Error
$_['error_login'] = 'Warning: No match for E-Mail Address and/or Password.';
$_['error_approved'] = 'Warning: Your account requires approval before you can login.';
?>
this is my controller-file login.php example which should connect $entry_email, $entry_password and $text_login into the active language file (english language file see above).
It is located in /catalog/controller/account
<?php
class ControllerAccountLogin extends Controller {
private $error = array();
public function index() {
$this->load->model('account/customer');
// Login override for admin users
if (!empty($this->request->get['token'])) {
$this->customer->logout();
$this->cart->clear();
unset($this->session->data['wishlist']);
unset($this->session->data['shipping_address_id']);
unset($this->session->data['shipping_country_id']);
unset($this->session->data['shipping_zone_id']);
unset($this->session->data['shipping_postcode']);
unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_address_id']);
unset($this->session->data['payment_country_id']);
unset($this->session->data['payment_zone_id']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
unset($this->session->data['comment']);
unset($this->session->data['order_id']);
unset($this->session->data['coupon']);
unset($this->session->data['reward']);
unset($this->session->data['voucher']);
unset($this->session->data['vouchers']);
$customer_info = $this->model_account_customer->getCustomerByToken($this->request->get['token']);
if ($customer_info && $this->customer->login($customer_info['email'], '', true)) {
// Default Addresses
$this->load->model('account/address');
$address_info = $this->model_account_address->getAddress($this->customer->getAddressId());
if ($address_info) {
if ($this->config->get('config_tax_customer') == 'shipping') {
$this->session->data['shipping_country_id'] = $address_info['country_id'];
$this->session->data['shipping_zone_id'] = $address_info['zone_id'];
$this->session->data['shipping_postcode'] = $address_info['postcode'];
}
if ($this->config->get('config_tax_customer') == 'payment') {
$this->session->data['payment_country_id'] = $address_info['country_id'];
$this->session->data['payment_zone_id'] = $address_info['zone_id'];
}
} else {
unset($this->session->data['shipping_country_id']);
unset($this->session->data['shipping_zone_id']);
unset($this->session->data['shipping_postcode']);
unset($this->session->data['payment_country_id']);
unset($this->session->data['payment_zone_id']);
}
$this->redirect($this->url->link('common/home'));
}
}
if ($this->customer->isLogged()) {
$this->redirect($this->url->link('common/home'));
}
$this->language->load('account/account');
$this->document->setTitle($this->language->get('heading_title'));
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
unset($this->session->data['guest']);
// Default Shipping Address
$this->load->model('account/address');
$address_info = $this->model_account_address->getAddress($this->customer->getAddressId());
if ($address_info) {
if ($this->config->get('config_tax_customer') == 'shipping') {
$this->session->data['shipping_country_id'] = $address_info['country_id'];
$this->session->data['shipping_zone_id'] = $address_info['zone_id'];
$this->session->data['shipping_postcode'] = $address_info['postcode'];
}
if ($this->config->get('config_tax_customer') == 'payment') {
$this->session->data['payment_country_id'] = $address_info['country_id'];
$this->session->data['payment_zone_id'] = $address_info['zone_id'];
}
} else {
unset($this->session->data['shipping_country_id']);
unset($this->session->data['shipping_zone_id']);
unset($this->session->data['shipping_postcode']);
unset($this->session->data['payment_country_id']);
unset($this->session->data['payment_zone_id']);
}
// Added strpos check to pass McAfee PCI compliance test (http://forum.opencart.com/viewtopic.php?f=10&t=12043&p=151494#p151295)
if (isset($this->request->post['redirect']) && (strpos($this->request->post['redirect'], $this->config->get('config_url')) !== false || strpos($this->request->post['redirect'], $this->config->get('config_ssl')) !== false)) {
$this->redirect(str_replace('&', '&', $this->request->post['redirect']));
} else {
$this->redirect($this->url->link('common/home'));
}
}
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home'),
'separator' => false
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_account'),
'href' => $this->url->link('account/account', '', 'SSL'),
'separator' => $this->language->get('text_separator')
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_login'),
'href' => $this->url->link('account/login', '', 'SSL'),
'separator' => $this->language->get('text_separator')
);
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['text_new_customer'] = $this->language->get('text_new_customer');
$this->data['text_register'] = $this->language->get('text_register');
$this->data['text_register_account'] = $this->language->get('text_register_account');
$this->data['text_returning_customer'] = $this->language->get('text_returning_customer');
$this->data['text_i_am_returning_customer'] = $this->language->get('text_i_am_returning_customer');
$this->data['text_forgotten'] = $this->language->get('text_forgotten');
$this->data['entry_email'] = $this->language->get('entry_email');
$this->data['entry_password'] = $this->language->get('entry_password');
$this->data['button_continue'] = $this->language->get('button_continue');
$this->data['button_login'] = $this->language->get('button_login');
if (isset($this->error['warning'])) {
$this->data['error_warning'] = $this->error['warning'];
} else {
$this->data['error_warning'] = '';
}
$this->data['action'] = $this->url->link('account/login', '', 'SSL');
$this->data['register'] = $this->url->link('account/register', '', 'SSL');
$this->data['forgotten'] = $this->url->link('account/forgotten', '', 'SSL');
// Added strpos check to pass McAfee PCI compliance test (http://forum.opencart.com/viewtopic.php?f=10&t=12043&p=151494#p151295)
if (isset($this->request->post['redirect']) && (strpos($this->request->post['redirect'], $this->config->get('config_url')) !== false || strpos($this->request->post['redirect'], $this->config->get('config_ssl')) !== false)) {
$this->data['redirect'] = $this->request->post['redirect'];
} elseif (isset($this->session->data['redirect'])) {
$this->data['redirect'] = $this->session->data['redirect'];
unset($this->session->data['redirect']);
} else {
$this->data['redirect'] = '';
}
if (isset($this->session->data['success'])) {
$this->data['success'] = $this->session->data['success'];
unset($this->session->data['success']);
} else {
$this->data['success'] = '';
}
if (isset($this->request->post['email'])) {
$this->data['email'] = $this->request->post['email'];
} else {
$this->data['email'] = '';
}
if (isset($this->request->post['password'])) {
$this->data['password'] = $this->request->post['password'];
} else {
$this->data['password'] = '';
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/login.tpl')) {
$this->template = $this->config->get('config_template') . '/template/account/login.tpl';
} else {
$this->template = 'default/template/account/login.tpl';
}
$this->children = array(
'common/column_left',
'common/column_right',
'common/content_top',
'common/content_bottom',
'common/footer',
'common/header'
);
$this->response->setOutput($this->render());
}
protected function validate() {
if (!$this->customer->login($this->request->post['email'], $this->request->post['password'])) {
$this->error['warning'] = $this->language->get('error_login');
}
$customer_info = $this->model_account_customer->getCustomerByEmail($this->request->post['email']);
if ($customer_info && !$customer_info['approved']) {
$this->error['warning'] = $this->language->get('error_approved');
}
if (!$this->error) {
return true;
} else {
return false;
}
}
}
?>
What I already tried:
- Replace login.tpl with original default login.tpl (in catalog/view/theme/default/template/account)
- Remove all vqmod xml's from vqmod manager.
Thanks in advance for your help.
language-file login.php
$_['text_email1'] = 'Email Address';
$_['text_pass1'] = 'Password';
controller-file login.php
$this->data['email1'] = $this->language->get('text_email1');
$this->data['pass1'] = $this->language->get('text_pass1');
Login.tpl
<div class="content">
<h2><?php echo $text_login ?></h2>
<b><?php echo $email1 ?></b><br />
<input type="text" name="email" value="<?php echo $email; ?>" />
<br />
<br />
<b> <?php echo $pass1 ?> </b><br />
<input type="password" name="password" value="<?php echo $password; ?>" />
<br />
<?php echo $text_forgotten; ?><br />
<br />
<input type="submit" value="<?php echo $button_login; ?>" class="button" />
<?php if ($redirect) { ?>
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
<?php } ?>
</div>
Add the code in language and controller file,and change div class content with this.

Zend_Form array notation and empty elements names

I'want to render:
<input type="text" value="" name="foo[]" />
<input type="text" value="" name="bar[]" />
but Zend_Form_Element require a (string) name, so I need to do:
$this->addElement('text', '1', array(
'belongsTo' => 'foo'
));
$this->addElement('text', '2', array(
'belongsTo' => 'bar'
));
but the output is:
<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-2" type="text" value="" name="bar[2]" />
I can also accept an output like:
<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-1" type="text" value="" name="bar[1]" />
but Zend_Form_Element rewrite elements with the same name
is there a way to do what I need?
For multiple values:
$foo = new Zend_Form_Element_Text('foo');
// Other parameters
$foo->setIsArray(TRUE);
$this->addElement($foo);
Generates: name="foo[]"
--
If you're looking for given keys such as name="foo[bar]", use:
$bar= new Zend_Form_Element_Text('bar');
// Other parameters
$bar->setBelongsTo('foo');
$this->addElement($bar);
--
Tested on ZF 1.11.5
class MyFooForm extends Zend_Form {
public function init() {
$fullNameOpts = array(
'required'=>false,
'label'=>'fullName',
'isArray'=>true,
'validators' => array( array('stringLength', false, array(1, 250) ) )
);
$this->addElement('text' ,'fullName',$fullNameOpts);
// rest of the elements , forms and stuff goes here
}
}
And that does creates
<dd id="fullName-element"><input type="text" class="inputAccesible" value="" id="fullName"name="fullName[]"></dd>
It's on Element.php , in Form , line 512 "isArray" check.
I'm using a regular zend_form, crossValidation with custom validators and i'm pushing subforms to replicate the main form, 'cause the user can add multiple times the same form.
Additionally , I'm too lazy to research custom decorators, i have created one, but it kills subForms and array notation, so i just stick with the regular ones, and that solves it.
I'm at Zf 1.10.