Codeigniter Insert Dynamic multiple input - codeigniter-3

can someone help me please. im stock. Can you help me how to save multiple value from multiple dynamic textbox?
My View:
<?php
echo form_open(admin/add_transaction);
for ($i=0; $i < $qty ; $i++) {
$count = $i+1;
$newinput=array(
'class'=>"form-control",
'name'=>"$property_no-$count",
'placeholder'=>"Serial No. - $property_no-$count",
); ?>
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-barcode"></i></span>
<?php echo form_input($newinput); ?></div>
<br>
<?php }
echo form_close();
?>
My Controller:
function add_transaction(){
$this->masterrecords->save_serial();
redirect('admin/transaction');
}
My Model: How to insert multiple value of input text?
function save_serial(){
$s=$this->input->post('new');
$data = array();
for ($i = 0; $i < count($this->input->post('new')); $i++)
{
$data = array(
'serial_no' => $s[$i],
);
}
$this->db->insert('tblserial_no' ,$data);
return true;
}

you have $qty and $property_no variable with you..what you can do is create two hidden fields for both the variables:
<input type='hidden' name='qty' value='<?= $qty ?>'/>
<input type='hidden' name='property_no' value='<?= $property_no ?>'/>
//then in your model:
$data = array();
for ($i=0; $i < $this->input->post('qty') ; $i++) {
$count = $i+1;
//check fields are set and not empty...
if($this->input->post($this->input->post('property_no').'-'.$count) &&
$this->input->post($this->input->post('property_no').'-'.$count) != ""){
//prepare your $data array..
$data[$count] = $this->input->post($this->input->post('property_no').'-'.$count);
}
}
hope this might give you some idea or might solve your problem.. too..
if my answer helps you can mark it as answer, thanks.

Related

How to display related post by categories with next post of current post (don't latest post or random post)

How to display related post by categories with next post of current post (don't latest post or random post). I'm use code related post for twentytwelve theme. but now, author in wentytwelve_entry_meta() is repeat.
pls help me :
<div id="related_posts">
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach((get_the_category()) as $category) {
$id = $category->cat_ID;
}
global $wpdb;
$query = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $category_ids[0]
AND $wpdb->posts.id < $post->ID
ORDER BY id DESC limit 3
";
$my_query = $wpdb->get_results($query);
if( $my_query) {
echo '<h3>Related Posts</h3><ul>';
foreach($my_query as $key => $post) {
?>
<li>
<div class="entry-header">
<div class="header-l">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<p class="datetime">
<?php twentytwelve_entry_meta(); ?>
</p>
</div>
<?php the_post_thumbnail(); ?>
</div>
<div class="entry-summary">
<?php
$str_content = wp_trim_words($post->post_content);
$str_content = str_replace('[', '<', $str_content);
$str_content = str_replace(']', '>', $str_content);
echo $str_content;
?>
</div>
</li>
<?php
}
echo '</ul>';
}
}?>
WordPress customary is to use WP_Query class to fetch posts from DB, if you can modify your code to use WP_Query it would be easier.
WP_Query Reference
As you are using Custom Query to load your posts from DB, template tags like the_permalink(), the_title_attribute(), the_title() etc will not work properly that is the reason while theme function twentytwelve_entry_meta() fails.
As per codex reference Displaying Posts Using a Custom Select Query
You should try something like this:
global $wpdb;
$query = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $category_ids[0]
AND $wpdb->posts.id < $post->ID
ORDER BY id DESC limit 3";
$my_query = $wpdb->get_results($query);
if($my_query) {
global $post;
echo '<h3>Related Posts</h3><ul>';
foreach($my_query as $key => $post) {
//use setup postdata, it works only for variable named $post.
setup_postdata($post);
//you can safely use template tags now.
?>
<li>
<div class="entry-header">
<div class="header-l">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<p class="datetime">
<?php twentytwelve_entry_meta(); ?>
</p>
</div>
<?php the_post_thumbnail(); ?>
</div>
<div class="entry-summary">
<?php
$str_content = wp_trim_words($post->post_content);
$str_content = str_replace('[', '<', $str_content);
$str_content = str_replace(']', '>', $str_content);
echo $str_content;
?>
</div>
</li>
<?php
}
}
Other interesting post:
What does setup_postdata ($post ) do?

validate capcha zend not correct

I have code:
$messages = array();
$isValid = TRUE;
$captcha = $this->_getParam('captcha_code', '');
if ( ! Captcha::validate($captcha)){
$isValid = FALSE;
$messages = array_merge($messages, array('Incorrect capcha.'));
}
}
if ($isValid AND count($messages) == 0) {
.............
}
else {
Captcha::destroy();
}
$this->view->register_messages = $messages;
and view:
<input name="captcha_code" type="text" class="input-text"/>
<?php if (isset($this->register_messages) && count($this->register_messages) > 0): ?>
<div class="alert-3">
<?php foreach ($this->register_messages as $message): ?>
<div><?= $message ?></div>
<?php endforeach; ?>
But when i enter correct capcha, it still showing "Incorrect capcha". I use zend 1
Please help me find this bug. Thanks a lot!

Create Button in last page pagination codeigniter

Hello there I have problem
I wanna create button in my pagination codeIgniter just in last page for submit form
here my code:
Controller:
function index() {
/* pagination config */
$this->load->library('pagination');
$config['base_url'] = site_url('dcm/index');
$config['per_page'] = 6;
$config['num_link'] = 5;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev' ;
$config['total_rows'] = $this->db->get('soal')->num_rows();
/* this my form still have problem but I have different post for this one */
/* if you have any Idea for this please check my Question */
$data['form_action'] = site_url('dcm/index');
$jawab = $this->input->post('jawab');
$id_soal = $this->input->post('id_soal');
$this->dcm_model->inputJawab();
/* */
/* pagination setting */
$this->pagination->initialize($config);
$data['query'] = $this->db->get('soal', $config['per_page'], $this->uri->segment(3));
$data['link'] = $this->pagination->create_links();
$this->load->view('dcm/soal', $data);
}
in view:
<form action="<?php echo $form_action; ?>">
<?php foreach($query->result() as $row) { ?>
<?php echo $row->id_soal . '. ' . $row->soal; ?>
<input type="checkbox" checked="checked" value="<?=$row->id_soal;?>" name="jawab[]" />
<?php } ?>
<?php echo $link; ?>
</form>
<input align="center" type="submit" value="selesai" />
You can check if you are on the last page with this code:
if($this->pagination->cur_page >=
ceil($this->pagination->total_rows / $this->pagination->per_page))
{
$isLastPage = true;
}
else
{
$isLastPage = false;
}
You can pass the $isLastPage variable to your view.

dynamically load form in codeigniter

I am new to codeigniter trying out some simple pages. I have successfully connected to the database for inserting and updating. But now I want to populate the fields from the database for updating. That is when the user selects the name from the dropdown the corresponding values should appear in the other fields so that the user can update easily.
I have worked on this to do without ajax or jquery. It worked for me but with some warning message - Use of undefined constant.
I am giving by mvc. Help me clear this.
Controller:
public function update()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Update Type';
$data['product_type'] = $this->editType_model->get_product_type();
$data['row'] = $this->editType_model->get_row();
$this->form_validation->set_rules('productype', 'productype', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('cm/update', $data);
$this->load->view('templates/footer');
}
else
{
$data['title']= 'Updation';
$data['message']= 'Updation succeeded';
$this->editType_model->update_news();
$this->load->view('cm/success', $data);
}
}
Model:
public function get_product_type() {
$data = array();
$this->db->order_by("product_type_name", "desc");
$query = $this->db->get('product_type');
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
public function get_row() {
$data = array();
$id= $_POST[product_type_id];
$query = $this->db->get_where('product_type', array('product_type_id' => $id));
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
public function update_news()
{
$this->load->helper('date');
$Product = $this->input->post('product_type_id');
$data = array(
'product_type_name'=>($_POST['productype']),
'rank'=>($_POST['rank'])
);
$this->db->where('product_type_id',$Product);
return $this->db->update('product_type', $data);
}
View:
<?php $productType = 0;
if(isset($_POST['product_type_id'])){
$productType = $_POST['product_type_id'];
}?>
<div id="Content"><div>
<form action="" method="post" name="f1">
<table ><tr><td>Select Product Type Name:</td>
<td><Select id="product_type_id" name="product_type_id" onChange="document.f1.submit()">
<option value="0">--SELECT</option>
<?php if (count($product_type)) {
foreach ($product_type as $list) { ?>
<option value="<?php echo $list['product_type_id']; ?>"
<?php if($productType==$list['product_type_id']) echo "Selected" ?> >
<?php echo $list['product_type_name']; ?></option>
<?php }} ?></Select></td></tr></table></form>
<?php if($productType){
if (count($row)) {
foreach ($row as $faProductType) { ?>
<div > <form action="" method="post" class="form-Fields" >
<input type="hidden" name="product_type_id" id="product_type_id" value="<?php echo $faProductType['product_type_id']; ?>" >
<table border="0" cellpadding="8" cellspacing="0" >
<tr><td>Product Type <font color="red">*</font></td><td style="width: 10px;"> : </td>
<td><input name="productype" type="text" value="<?php echo $faProductType['product_type_name']; ?>" style=" width:300px;" /></td>
<td><span class="err" id="productTypeDesc1Err"></span></td></tr>
<tr><td >Rank</td><td style="width:10px;"> : </td>
<td ><input type="text" name="rank" id="rank" value="<?php echo $faProductType['rank']; ?>" size="39" /> <span class="err" id="productTypeNameErr"></span></td>
<td ></td></tr><tr><td></td><Td colspan="2" align="center">
<input type="submit" value="Update" class="buttonimg" name='cm/editType/update' style="width:100px"
onClick="return validEditProductType();">
</Td><td></td></tr></table></form></div></div></div>
<?php }}} ?>
you can use Ajax for this purpose
onchange of your select box fill a div with form.
Controller Method
function getUserdata(){
$where['username'] = $this->input->post('username');
$this->load->model('users_model');
$data['user'] = $this->users_model->getUser($where);
$this->load->view('userform',$data);
}
And you model method
function getUser($where){
return $this->db->where($where)->get('usertable')->row()
}
Ajax
$(function(){
$('#selecttion').change(function(){
var username = $(this).val();
$.ajax({
url : '<?php 'echo url to controller method'?>',
type : 'POST' ,
data : 'username='+username,
success : function(data){
$('#dividtofill').html(data);
}
});
});
})
EDIT :
calling ajax is easy
for this include jquery library in your header.
Here is your dropdown
Note : put the above ajax code in your head section in script
<select name="user" id="selecttion">
<option value="John">John</option>
<option value="Sam">Sam</option>
<option value="David">David</option>
</select>
Now when this dropdown value is changed the ajax function defined above will be called.
I have finally cleared the error. I have done this by a simple if isset loop.
Now my get_row() in my model looks like this.
public function get_row() {
$data = array();
$id= 0;
if(isset($_POST['product_type_id'])){
$id = $_POST['product_type_id'];
}
$query = $this->db->get_where('product_type', array('product_type_id' => $id));
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}

Zend_Form_Element_Checkbox multiple values

I have below codes
foreach ($this->getChitsInstallment() as $key => $installment) {
$checkElement = new Zend_Form_Element_Checkbox("installment[]");
$checkElement->setAttrib('id', 'installment_'.$key)->setDecorators($decorators->elementDecorators);
$this->addElement($checkElement);
$checkElements[] = $checkElement->getName();
$textElement = new Zend_Form_Element_Text("installmentvalue[$key]");
$textElement->setAttrib('readonly', 'true')->setAttrib('class', 'inp-form');
$textElement->setAttrib('id', 'text_'.$key)->setDecorators($decorators->elementDecorators);
$textElement->setValue($installment);
$textElement->setLabel("Installment $key: ");
$this->addElement($textElement);
$textElements[] = $textElement->getName();
}
I want an output like
<input type="checkbox" name="installment[]" id="installment_1" />
<input type="checkbox" name="installment[]" id="installment_2" />
<input type="checkbox" name="installment[]" id="installment_3" />
In view i just call <?php echo $this->form ?> Only
Pls give me a solution for this
Regards
Nisanth
You'll want to use Zend_Form_Element_MultiCheckbox instead:
$element = new Zend_Form_Element_MultiCheckbox('installment');
foreach ($this->getChitsInstallment() as $key => $installment) {
$element->addMultiOption($installment, "Installment $key: ");
}
$this->addElement($element);