I created an accounts page wherein I can view all accounts from the database. Each of them has an action button called View More. If I press View More, additional details from the database will be displayed in textboxes. I want to get the ID for each of the row and pass it to the controller in order for it to be viewed. However, I can't seem to make them show in the boxes.
Expected: Image
Current: Image
Controller:
function viewmore()
{
$userid = $this->input->get('userid', TRUE);
$data['view'] = $this->model_accounts->viewmore_user($userid);
$data['main_content'] = 'view_adminviewmore';
$this->load->view('includes/admin_viewmore_template', $data);
}
Model:
public function viewmore_user($userid)
{
$query= $this->db->select('*')->from('accounts')->where('userid', $userid)->get();
return $query->result();
}
View for the Action Button:
<?php foreach($users as $row): ?>
<tr>
<td><?php echo $row->firstname; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><?php echo $row->username; ?></td>
<td><?php echo $row->address; ?></td>
<td class="action-button mobile-important">
<button type="button" class="btn btn-custom-3">View More</button>
</td>
<?php endforeach; ?>
</tr>
View for displaying the data:
<?php foreach($view as $row): ?>
<br>
<p> First Name </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->firstname; ?>">
<br>
<p> Last Name </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->lastname; ?>">
<br>
<p> Username </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->username; ?>">
<br>
<p> Address </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->address; ?>">
<br>
<p> E-mail Address </p>
<input class="form-control" id="sel1" type="email" placeholder="" value="<?php echo $row->email; ?>">
<br>
<p> Contact Number </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->contactnum; ?>">
<br>
<?php endforeach; ?>
If you are using CodeIgniter, why not use the Form Helper?
You can use code like this:
<?php echo form_input('sel1', $row->firstname); ?>
instead of:
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->firstname; ?>">
Read some more about it here: https://www.codeigniter.com/user_guide/helpers/form_helper.html
your action button must be like this:
you can use <?="hello word!!!" ?> instead of <?php echo "hello word!!!" ?>
and base_url() return the root of your codeigniter folder that set
in application->config->config.php
<a href="<?=base_url()." YOUR_CONTROLLER_NAME/viewmore/".USER_ID ?>View More</a>
link above return <a href="localhost/YOUR_CONTROLLER_NAME/viewmore?id=USER_ID>View More</a>
and your function viewmore() must be change to function viewmore($id=0),this $id give USER_ID from your url
and you better to know url in codeigniter contain this:
Controller/Function/variable that you want pass to your function
Related
How can I make 2 forms which has the same method? One is responsible to open editaddresss.php with hidden addresse ID, and the second form (editaddress.php)should trigger controller and repository to save the data?
The problem is, as soon as I click "Edit Address" button on profile.php, triggers editAddress() method in controller. But I want to only happen when is clicked on editaddress.php
Profile.php
<form action="editaddress.php" method="POST" id="form1">
<tr>
<input type="hidden" name="addressid" value="<?php echo $perssonalAddress->getAddressid(); ?>">
<td><?php echo $perssonalAddress->getStreet(); ?></td>
<td><?php echo $perssonalAddress->getZip(); ?></td>
<td><?php echo $perssonalAddress->getCity(); ?></td>
<td><?php echo $perssonalAddress->getCountry(); ?></td>
<td><?php echo $perssonalAddress->getType(); ?></td>
<td><input type="submit" id="removeitem" value="Edit Address" form="form1"></td>
</form>
Editaddress.php
<form action="editaddress" method="POST" name="editadresse" id="form2">
<input type="hidden" name="update">
<input type="hidden" value="<?php echo $getPersonalAddress->getAddressid(); ?>" name="addressid">
<label for="street">Street</label>
<input type="text" value="<?php echo $getPersonalAddress->getStreet() ?>" id="street" name="street">
<label for="zip">ZIP</label>
<input type="text" value="<?php echo $getPersonalAddress->getZip() ?>" id="zip" name="zip">
<label for="city">City</label>
<input type="text" value="<?php echo $getPersonalAddress->getCity() ?>" id="city" name="city">
<label for="country">Country</label>
<input type="text" value="<?php echo $getPersonalAddress->getCountry() ?>" id="country" name="country">
<input type="submit" class="login" id="login" value="Update Address" name="editadresse" form="form2">
</form>
Controller
/**
* Get Customers specific Address
*/
public function editAddress(): void
{
//Error and Success Messages
$error = false;
$success = false;
//Error if there is no such an address
$message = false;
//Get the specific customer address
$getPersonalAddress = $this->profileService->getAddressById();
if ($getPersonalAddress === false) {
$message = $this->profileService->getMessages();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $this->addressForm->isValid($_POST)) {
$this->profileService->updateCustomerAddress();
$success = $this->profileService->getMessages();
}else {
$error = $this->addressForm->getFormErrorMessage();
}
//Render to View
$this->render('editaddress', ['getPersonalAddress' => $getPersonalAddress, 'message' => $message, 'error' => $error, 'success' => $success]);
}
I think you're approaching this incorrectly. You are trying to get one end-point (editaddress.php) to do two different things.
instead, you should have one end-point for generating a form with the details filled in.
and a different end-point for recording data saved from that form.
I need help for this conversion of HTML to PHP
This is my code and I want to write inside the if tag with echo to get the results of the base of conditions for more info I am getting the error around the value of the input field from the database.
<td>
<input type="text" name="sale_rate[]" onchange="getProductData(<?php echo $x; ?>)" id="sale_rate_<?php echo $x; ?>" class="form-control" autocomplete="off" value="<?php echo $val['sale_rate'] ?>">
<input type="hidden" name="sale_rate_value[]" id="sale_rate_value_<?php echo $x; ?>" class="form-control" autocomplete="off">
</td>
to like this
<?php if($user_id == 1)
echo "<td><input type="text" name="sale_rate[]" onchange="getProductData(<?php echo $x; ?>)" id="sale_rate_<?php echo $x; ?>" class="form-control" autocomplete="off" value="<?php echo $val['sale_rate'] ?>">
<input type="hidden" name="sale_rate_value[]" id="sale_rate_value_<?php echo $x; ?>" class="form-control" autocomplete="off">
</td>";
?>
Any kindly of help will be so appreciated
You can close and reopen <?php ?> tags. So you don't need echo
<?php if($user_id == 1){ ?>
<td>
<input type="text" name="sale_rate[]" onchange="getProductData(<?php echo $x; ?>)" id="sale_rate_<?php echo $x; ?>" class="form-control" autocomplete="off" value="<?php echo $val['sale_rate'] ?>">
<input type="hidden" name="sale_rate_value[]" id="sale_rate_value_<?php echo $x; ?>" class="form-control" autocomplete="off">
</td>
<?php } ?>
Alternatively, you can use if/endif syntax:
<?php if($statement): ?>
<some-html>...</some-html>
<?php endif; ?>
There are many different approaches, which amount to as much a matter of style as anything else.
Personally, I prefer to not have numerous instances of <?php [CODE HERE] ?> throughout my markup, so, instead, I would favour something more like this:
<?php
if($user_id == 1) {
echo '
<td>
<input type="text" name="sale_rate[]" onchange="getProductData('.$x.')" id="sale_rate_'.$x.'" class="form-control" autocomplete="off" value="'.$val['sale_rate'].'">
<input type="hidden" name="sale_rate_value[]" id="sale_rate_value_'.$x.'" class="form-control" autocomplete="off">
</td>
';
}
?>
I am trying to store the values obtained from the form into the database. I am stuck where I do not know how to obtain the values individually from foreach($questions as $row) and store them into the database.
My database table has the following columns:
variantid | stepid | questionid | newquestion | radiovalues | description
When I click on save, I am trying to save all the details obtained from the view into the database. every value obtained with each loop of foreach($questions as $row) needs to get stored in a new row. I think I should be using insert_batch, but I do not know how I can put all the data into the array.
How can I proceed with this? The following is the code that I have written. I have not written anything in model as I am stuck.
<form id="theform">
<label for="variants">Variants:</label><br>
<?php
foreach($variants as $row)
{?>
<div class="form-group">
<input type="radio" name="variants[]" checked="true" id="variants" value="<?php echo $row->variantid ?>"/><?php echo $row->variantname ?><br/>
<?php }
?>
<div id='steplist'>
<?php
foreach($substeps as $row){
?>
<div id='img_div'>
<input type="radio" name="stepid[]" checked="true" id="stepid<?php echo $row->stepid; ?>" value="<?php echo $row->stepid; ?>"/><?php echo $row->stepname ?><br/>
</div>
<?php
foreach($questions as $each){
?><br><br>
<input type="text" name="questions[]" id="questions<?php echo $each->questionid; ?>" value="<?php echo $each->questionname; ?>" readonly/><br/>
<input type="hidden" name="questionid[]" id="questions<?php echo $each->questionid; ?>" value="<?php echo $each->questionid; ?>"/>
<a id="addq">Add question</a>
<input type="text" class="form-control" name="newquestion[]" style="font-weight:bold;width:100%;height:5%" id="new_questions<?php echo $each->questionid; ?>" /><br>
<div class="radio" id="answer">
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="no" required>no</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="probably no" required>probably no </label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="unknown" required>unknown</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="probably yes" required>probably yes</label>
<label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="yes">yes</label>
</div>
<textarea name="question1_text[]" id="description<?php echo $each->questionid; ?>" rows="3" cols="73"></textarea><br>
<?php
}
?>
<?php
}
?>
</div>
</form>
<input type='button' value='Save' class='save' id='save' />
<script>
$(document).ready(function(){
$("#save").click(function(e){
e.preventDefault();
$.ajax({
type:"POST",
url:"<?= base_url() ?>index.php/task/perform",
data:$('#theform').serialize(),
success:function(response){
alert(response);
}
});
});
});
This is my controller:
public function perform(){
var_dump($_POST);
$variantid = $this->input->post('variants');
$stepid = $this->input->post('stepid');
$questionid = $this->input->post('questions');
$newquestion = $this->input->post('newquestion');
$radioquestions = $this->input->post('radioquestions');
$question1_text = $this->input->post('question1_text');
$this->load->model('task_model');
$result= $this->task_model->performdata();
if($result){
echo "Data saved successfully";
}
}
Update
Thank you for the quick reply. I changed the code according to the suggestion. How can I get these values into the controller and send them into the database?
The HTML inside the foreach is invalid as it is missing a closing div tag and your input does not have the correct name attribute, for multiple inputs you should use variants[] and not the same ID. Do it in this manner:
<?php foreach ($variants as $row) { ?>
<div class="form-group">
<input type="radio" name="variants[]" checked="true" class="variants" value="<?php echo $row->variantid ?>"/><?php echo $row->variantname ?><br/>
</div>
<?php } ?>
I want to keep the data in a text box after submit and inserted into db.
even if i refresh the page data should not change or empty
here is my code
<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_admin"><?php _e('Collaboration Admin', 'custom_table_example')?></label>
</th>
<td>
<input id="approved_admin" name="approved_admin" type="email" value="<?php echo esc_attr($item['approved_admin'])?>"size="50" class="code" placeholder="<?php _e('Admin Email', 'custom_table_example')?>" required>
</td>
</tr>
if i do this data will be there if i reload empty text box will be shown.I need to make this readonly and onload same text should be there.
Form
<form id="form" method="POST">
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(basename(__FILE__))?>"/>
<?php /* NOTICE: here we storing id to determine will be item added or updated */ ?>
<input type="hidden" name="sid" value="<?php echo $item['sid'] ?>"/>
<div class="metabox-holder" id="poststuff">
<div id="post-body">
<div id="post-body-content">
<?php /* And here we call our custom meta box */ ?>
<?php do_meta_boxes('Settings', 'normal', $item); ?>
<input type="submit" value="<?php _e('Save', 'custom_table_example')?>" id="submit" class="button-primary" name="submit" / >
<input type="submit" value="<?php _e('Edit', '')?>" id="edit" class="button-primary" name="edit" / >
</div>
</div>
</div>
</form>
I tried doing
<input type="email" name="approved_admin" value="<?php if (isset($_POST['approved_admin'])) { echo $_POST['approved_admin']; } ?>">
and also
<input type="text" name="country" value="Norway" <?php isset($approved_admin) ? echo 'readonly' : ''; ?> >
You can save data after page reload a lot of ways like... Session , cookie, get , post. After this value get by isset and echo .
In your case Post method used..
Try this working fine all thing
<input type="text" name="country" value=" <?php echo isset($_POST['country']) ? $_POST['country'] : ''; ?>" <?php echo isset($_POST['country']) ? 'readonly' : ''; ?> >
You just put field name in isset();
You can use this syntax. Have a try for this.
<input type="text" id="to_date" name="to_date" class="form-control" placeholder="Enter End Date" value="<?php echo isset($_POST['to_date']) ? $_POST['to_date'] : '' ?>" />
The name of the input type has to be mentioned in the isset() condition.
I want to view data from database.there are three table to join.in two table have same field name. when i try to view data as follows.but there was an error called
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$project_name1
Filename: views/boq_doc.php
Line Number: 12
Controller
class Project_list extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('project_list_model');
}
function show_project_id() {
$id = $this->uri->segment(3);
$data['projects'] = $this->project_list_model->show_projects();
$data['single_project'] = $this->project_list_model->show_project_id($id);
$this->load->view('boq_doc', $data);
}
}
Model
class Project_list_model extends CI_Model {
// Function To Fetch All Students Record
function show_projects(){
$this->db->select("project.project_name AS project_name1 , project.id AS id, client.firstname AS firstname1, client.lastname AS lastname1,staff.firstname AS firstname2, staff.lastname AS lastname2, project.location,project.category, project.start_date, project.end_date");
$this->db->from('project');
$this->db->join('client', 'project.client_id = client.client_id');
$this->db->join('staff', 'staff.id = project.staff_id');
$query = $this->db->get();
return $query->result();
}
// Function To Fetch Selected Student Record
function show_project_id($data){
$this->db->select('*');
$this->db->from('project');
$this->db->where('id', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
View
<?php foreach ($projects as $project): ?>
<li><?php echo $project->project_name1; ?></li>
<?php endforeach; ?>
</ol>
</div>
<div id="detail">
<!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
<?php foreach ($single_project as $project): ?>
<form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_project_id1"?>">
<label id="hide">Id :</label>
<?php echo $project->project_name1; ?>
<label>Name :</label>
<input type="text" name="dname" value="<?php echo $project->location; ?>">
<label>Email :</label>
<input type="text" name="demail" value="<?php echo $project->start_date; ?>">
<label>Mobile :</label>
<input type="text" name="dmobile" value="<?php echo $project->end_date; ?>">
<label>Address :</label>
<input type="text" name="dmobile" value="<?php // echo $project->firstname1; ?>">
<input type="text" name="daddress" value="<?php // echo $project->project_address; ?>">
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?php endforeach; ?>
</div>
Your foreach loop syntax seems wrong try this instead
<?
foreach ($projects as $project)
{
?>
<li><?php echo $project->project_name1; ?></li>
<?
}
?>
</ol>
</div>
<div id="detail">
<!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
<?php foreach ($single_project as $project){ ?>
<form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_project_id1"?>">
<label id="hide">Id :</label>
<?php echo $project->project_name1; ?>
<label>Name :</label>
<input type="text" name="dname" value="<?php echo $project->location; ?>">
<label>Email :</label>
<input type="text" name="demail" value="<?php echo $project->start_date; ?>">
<label>Mobile :</label>
<input type="text" name="dmobile" value="<?php echo $project->end_date; ?>">
<label>Address :</label>
<input type="text" name="dmobile" value="<?php // echo $project->firstname1; ?>">
<input type="text" name="daddress" value="<?php // echo $project->project_address; ?>">
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?
}
?>
</div>