How to add column to wp_post table in database? - php

I have a database named event-listing-db which contains a table wp_posts. Also i have a function for creating a form with method post with the following structure:
function ru_meta_callback() {
wp_nonce_field(basename(__FILE__), 'ru_jobs_nonce ');
?>
<form method="POST">
<div>
<div class="meta-row">
<div class="meta-th">
<label for="date_listed" class="ru-row-title">Event Date</label>
</div>
<div class="meta-td">
<input type="text" name="event_date" id="event_date" value=""/>
</div>
</div>
<div class="meta-row">
<div class="meta-th">
<label for="event_location" class="ru-row-title">Event Location</label>
</div>
<div class="meta-td">
<input type="text" name="event_location" id="event_location" value=""/>
</div>
</div>
<input type="submit" value="Add Event">
</div>
</form>
<?php
}
When I submit the form only the post_title column is filling. My question is: How can I create columns for event_date and event_location and fill'em when the data is submitted and save the new columns along with the original post?

You don't have to add a new column to the wp_posts table. But you can add a new meta for the post. If you want to add the field in the admin panel for the new field you want. You can use the add_meta_box to add a new meta box for post edit screen and then use the update_post_meta on the save_post hook. Hope that helps.
The meta fields can be used in the WP_Query in the meta_query index. But you can't use the custom field added to the wp_posts table to filter or search for the posts with the new meta added to the posts.

You have few options to have additional data to your post,
-> You can use hooks to add additional fields like here, WordPress - Add extra column to wp_posts, and post to it
-> You can make use of custom fields to add additional data to your post. And there is a good plugin too for this, https://wordpress.org/plugins/advanced-custom-fields/.
-> You can use the wp_postmeta table too to save your additional data for your post

Related

restrict a minimum order using php

I'm creating an eCommerce website using HTML and PHP. I have a table named "products" in the database.
the minimum order values of the products are all saved in this table.
I have an admin panel to set the minimum order for that particular product. The quantity of the minimum order of each and every product was saved in the MySQL database. The minimum order value of that particular product is set by the admin according to the amount of availability of the product. There are no constraints for the admin to set the minimum order in the admin panel.
My question is how do I get the minimum order($row['moq']) quantity from the "product" table in the database so that the orders(cart_qty) from the users will be restricted to a certain amount that has been set from the Admin Panel using minimum attributes.
These are the codes that I used to require users to add the quantity that they wanted to order:
<div class="clearfix"></div>
<form action="store_items.php" method="post">
<div class="col-12 form-group">
<div class="row">
<div class="col-3">
<input type="number" name="cart_qty" step="0.01" value="1" class="form-control text-center" </div>
<div class="col-9">
<input class="btn btn-block addtocart" type="submit" value="Add to cart" name="addtocart" <?php if($row[ 'product_status']==0 ) echo 'disabled="disabled"'; ?> >
</div>
</div>
This is where I add the minimum order from the admin panel. moq is the entity of the minimum order in the product table.
<div class="col-md-2">
<div class="form-group">
<label>Minimum Order Quantity*</label>
<input type="number" step="0.01" class="form-control" placeholder="Enter minimum order quantity" name="moq" value="<?php echo $row['moq'];?>" required>
</div>
</div>
How do I get the $row['moq'] to restrict the number of orders in cart_qty. I know the method of using the minimum attributes but how do I get the values from of the moq in the product table to restrict the input value of cart_qty in the user page.
I'm not using any eCommerce platform (eg. WooCommerce) to build my website. Just a pure HTML, PHP, and JavaScript.
You can use the onChange event to restrict the value using Javascript.
<input type="number" onchange="restrictValue(this, 10, 30);">
Then in Javascript you do:
function restrictValue(inputElement, minimum, maximum)
{
let value = inputElement.value;
if (value < minimum) value = minimum;
if (value > maximum) value = maximum;
inputElement.value = value;
}
You could use the onKeyUp to constantly correct any value entered, but that could be quite disruptive and irritating.

textarea data is not showing in form - but is saving to database, anyone know why?

my form(foundation 5 html form) is saving all the fields to the database and all the fields are repopulating back into the pop up form except for the textarea data field - this field is not repopulating
I have tried adding textarea id="task_description" to the tag and some other simple things that I did not have much faith in working..and, nope, it did not work!
<div class="row">
<div class="large-12 columns">
<label>Task Description </label>
<textarea name="task_description" placeholder="task details.." value="<?php echo $task->task_description; ?>" >
</textarea>
</div>
</div>
I am wanting the text that is typed into the task_description field of my form to repopulate back into the form when selecting the row from the page listview. I am using PDO..and have bound this field just the same as all the other fields in my form. The other fields repopulate, but the task description will not - it is empty. The field data is saving to the database however.
textarea doesn't have a value, it has content. You probably want this
<textarea name="task_description" placeholder="task details.." >
<?php echo $task->task_description; ?>
</textarea>
You are confusing <input> with <textarea>. Inputs have values, whereas textareas use the innerText as a "value". You would want to change your code to this:
<div class="row">
<div class="large-12 columns">
<label>Task Description </label>
<textarea name="task_description" placeholder="task details..">
<?php echo $task->task_description; ?>
</textarea>
</div>
</div>

Laravel 5 - Multiple form inputs with the same name but keeping the order

Making a blogging system using L5 and my current set up is all ready except for the saving of the blog posts.
I have 2 buttons. One creates a textarea input and the other creates a file upload interface.
Essentially, after creating a blog post I am left with the structure like so:
<form>
<textarea name="text-update">foo</textarea>
<textarea name="text-update">foo</textarea>
<textarea name="text-update">foo</textarea>
<textarea name="text-update">foo</textarea>
<input type="hidden" value="an image url"/>
<input type="hidden" value="an image url"/>
<textarea name="text-update">foo</textarea>
</form>
Ideally I want to be able to go:
public function store()
{
foreach (INPUTS AS INPUT) {
add new row to database with the content and also the type of input.
}
}
The aim is that instead of having a single blog I instead have blog sections which will belong to a blog post.
If this isn't possible then Ill just have to increment the names of the inputs and figure something out.
Edit: When you add an element to the DOM you can define the array key with an id to preserve the array order.
You can make the inputs an array by adding [] at the end of the name:
<form>
<textarea name="text-update[1]">foo</textarea>
<textarea name="text-update[2]">foo</textarea>
<textarea name="text-update[3]">foo</textarea>
<textarea name="text-update[4]">foo</textarea>
<input type="hidden" name="image[1]" value="an image url"/>
<input type="hidden" name="image[2]" value="an image url"/>
<textarea name="text-update[5]">foo</textarea>
</form>
This will put all the values in an array that you can iterate over
foreach (Request::get('text-update') as $update) {
//add new row to database with the content and also the type of input.
}
foreach (Request::get('image') as $update) {
//add new row to database with the content and also the type of input.
}
Set your fields up like this:
<textarea name="text-update[]">foo</textarea>
using the brackets will take all the text fields and group them into an array that you can then iterate through. You will also need to do the same with hidden fields. Make sure you use the [] in the name like so:
<input type="hidden" name="somename[]" value="an image url"/>

How to get post meta meta key and meta value of wp color picker field

I have created an input field in my custom metabox and linked it to wpColorPicker object in jquery so that it becomes a color picker field.But i dont know to get the meta key to show the value.My code is:
<label for="title-background-color">Choose background color: </label>
<input type="text" name="title-background-color" id="title-background-color"
class="title-background-color" value="#dddddd" data-default-color="#dddddd">
I want to show it here
<div class="post-title-under-header" style="background: `i want that value here` ;">
<div class="post-title-under-header">
<?php the_title('<h1>', '</h1>');?>
</div>
</div>
To set the background color of your element with class post-title-under-header you can do something like this with jQuery. The background-color will, in this example, change on an change event.
$(document).ready(function(){
$('#title-background-color').on('change',function(){
$('.post-title-under-header').css('background-color',$('#title-background-color').val());
});
});

Insert text field content based on what checkboxes are selected php mysql

Hey Fellow Programmers,
I have a slight problem and I cant find the right answer online.
Basically what I need to do is, a user inserts content into a text box and then selects a check box. Whichever check box is selected is what table the text box content is supposed to insert into. **Both check boxes can be selected so the user can upload to two diff tables, before you ask no I cannot just upload to a diff row it has to be a completely diff table.
Let me know if I am not clear, and thanks in advance
HTML CODE:
<body class="login">
<div class="wrapper">
<h1><img src="img/logo-big.png" alt="" class='retina-ready' width="59" height="49">FLAT</h1>
<div class="login-body">
<form action="db_pre_panel.php" name="login" class='form-validate' id="test" method="post">
<div class="control-group">
<div class="email controls">
<h3>TEST</h3>
<input type="text" name="content" maxlength="500" placeholder="Content" />
</div>
</div>
<div class="control-group">
<input type="checkbox" name="Ck_1" /> <label>Ck_1</label>//If selected then INSERT content into tbl_connect
<input type="checkbox" name="Ck_2" /> <label>Ck_2</label>//If selected then INSERT content into tbl_share
</div>
<div class="submit">
<input type="submit" value="Simplicity" />
</div>
PHP CODE:
<?php
//Define Content, Post & Share
$content=$_POST['content'];
$post=$_POST['ck_1'];
$share=$_POST['ck_2'];
//Insert into the db
$sql_post="INSERT INTO $tbl_connect (wall) VALUES ('$connect', '$post')";
$sql_share="INSERT INTO $tbl_share (wall) VALUES ('$connect', '$share')";
//Make sure it insert into db
$result_post = mysql_query($sql_post);
$result_share = mysql_query($sql_share);
if($result_post){
header("location:alert.php");
}else{
header("location:error.html");
}
if($result_share){
header("location:http://www.google.com");
}else{
header("location:error.html");
}
?>
Just keep it simple:
//Define Content, Post & Share
$content = $_POST['content']; // you should sanitize this to prevent SQL injection
if ( !empty($_POST['ck_1']) ) {
$sql_post = "INSERT INTO `tbl_connect` (wall) VALUES ('$connect')"; // if you have more than one value, then you need to specify more than one column...
}
if ( !empty($_POST['ck_2']) ) {
$sql_share = "INSERT INTO `tbl_share` (wall) VALUES ('$connect')";
}

Categories