I just want to ask how can you get the value of the texbox in this code:
Here is my view:
<div class="row">
<h3><input type='text' name="table1" value='<?php echo $table1; ?>' style='border: none; background-color: transparent;' readonly/></h3>
<div class="table_design">
<button style='position: absolute; top:290px; right: 173px;' class='btn_addinventory btn-success btn-sm' id='btn_addinventory' name='btn_addinventory'><span class='glyphicon glyphicon-plus'></span> Add Inventory</button>
<table class="boom_table">
<tr>
<?php foreach($sample1 as $field) { ?>
<th><?php echo $field->name; ?></th>
<?php } ?>
</tr>
<?php foreach($select1 as $row) { ?>
<tr>
<td><?php echo $row->ID; ?></td>
</tr>
<?php } ?>
</table>
<?php foreach($sample1 as $field) { ?>
<input type="text" name="txt_rowname" value="<?php echo $field->name; ?>"/>
<?php } ?>
</div>
</div>
Here is the controller:
public function inventory_new(){
$tablenaming1 = $_POST['ninja7'];
// var_dump($tablenaming1);
// exit;
$this->session->userdata('login_session');
$this->data['title'] = "Inventory";
$this->load->vars($this->data);
$this->load->view('homeview');
$select_inv['inventorytype'] = $this->inventory_model->select_tables();
$this->load->view('inventoryview', $select_inv);
$sample['sample'] = $this->inventory_model->select_fields_2($_POST['ninja7']);
$select_details['select'] = $this->inventory_model->select_queries2($this->input->post());
// var_dump($select_details);
// exit;
$value_array = array('sample1' => $sample['sample'],
'table1' => $tablenaming1,
'select1' => $select_details['select']);
$this->load->view('sampleviewtable', $value_array);
$this->load->view('footer_view');
}
And here is my model:
public function select_fields_2($tablename2){
$fields = $this->db->field_data($tablename2);
return $fields;
}
public function select_queries2($tablename){
$select_table = $this->db->select('*')
->from($tablename['ninja7']);
return $select_table->get()->result();
}
In my view above you will see:
<?php foreach($select1 as $row) { ?>
<tr>
<td><?php echo $row->ID; ?></td>
</tr>
<?php } ?>
And this:
<?php foreach($sample1 as $field) { ?>
<input type="text" name="txt_rowname" value="<?php echo $field->name; ?>"/>
<?php } ?>
I want to get the value of the <input type="text" name="txt_rowname" value="<?php echo $field->name; ?>"/> from my view, For example: if the value of this textbox is equal to 'Name' instead of $row->ID now it should be $row->Name. I really can't figure out this yet, I want to pass the value of the textbox instead of putting 'ID' on $row. Is there a way to do that?
Thank you in advance for your help! :)
Eljon as per your comments and what i understand your requirement is you need to populate table structure on clicking table name.
So,in codeigniter their is a simple way to achieve this like this:
$fields = $this->db->list_fields('table_name');
foreach ($fields as $field)
{
echo $field;
}
Here 'list_fields' will give you your fields name of specified table.
If you want to get field data you can try this out :
$fields = $this->db->field_data('table_name');
foreach ($fields as $field)
{
echo $field->name;
echo $field->type;
echo $field->max_length;
echo $field->primary_key;
}
Here is more information in codeigniter documentation
https://ellislab.com/codeigniter/user-guide/database/fields.html
Hope this helps you.
Regards,
Zeeshan.
----- Update ------
You can populate database table in tabular structure using inbuilt codeigniter library,you can directly echo that table in your view the code goes like this:
$this->load->library('table');
$query = $this->db->get('table_name');
$page_data['db_table'] = $this->table->generate($query);
In your view :
echo $db_table
--- Update 2 ----
Update as per your comments and requirements
you can use set_template of table class to set the id as
$tmpl = array ( 'table_open' => '<table id="YOUR_ID" class="mytable">' );
$this->table->set_template($tmpl);
More details regarding table class here https://ellislab.com/codeigniter/user-guide/libraries/table.html
Related
I need hide first one < td > <?php echo $user_feature['tab_title_single']; ?> if second < td > are abolutely empty without any checked ('tab_title_labels') values.
<?php
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
if (!empty($id)) {
$post_id = $id;
}
$user_features = array(
array(
'tab_title_single' => 'Apsauga',
'tab_title_labels' => 'Imobilaizeris,Signalizacija,Palydovinė sekimo sistema,Šarvuotas (apsaugos)'
),
array(
'tab_title_single' => 'Audio/video įranga',
'tab_title_labels' => 'CD grotuvas,MP3 grotuvas,Papildoma audio įranga,CD keitiklis,AUX jungtis,Žemų dažnių garsiakalbis,DVD grotuvas,USB jungtis,Laisvų rankų įranga,Apple CarPlay / Android Auto'
),
array(
'tab_title_single' => 'Eksterjeras',
'tab_title_labels' => 'Lengvojo lydinio ratlankiai,LED dienos žibintai,LED žibintai,Žibintai „Xenon“,Rūko žibintai,Kablys,Priekinių žibintų plovimo įtaisas,Stogo bagažinės laikikliai,Automatiškai užsilenkiantys veidrodėliai,Žieminių padangų komplektas'
),
array(
'tab_title_single' => 'Elektronika',
'tab_title_labels' => 'El. valdomi veidrodėliai,El. valdomas bagažinės dangtis,Automatiškai įsijungiantys žibintai,Borto kompiuteris,El. reguliuojama vairo padėtis,Kritulių jutiklis,Šildomi veidrodėliai,Atstumo jutiklių sistema,Beraktė sistema,Autopilotas,El. šildomas priekinis stiklas,Start-Stop funkcija,Valdymas balsu,Pavarų perjungimas prie vairo,LCD ekranas,Navigacija/GPS'
)
);
?>
<?php if (!empty($user_features)) {
if (!empty($post_id)) {
$features_car = get_post_meta($post_id, 'additional_features', true);
$features_car = explode(',', $features_car);
} else {
$features_car = array();
}
foreach ($user_features as $user_feature) { ?>
<table style="width: 100%; margin-bottom: 0px">
<tr>
<td style="width: 16%; padding-left:5px;">
<div class="heading-font" style="color:#555555;font-size: 13px;font-weight: 500;"><?php echo $user_feature['tab_title_single']; ?></div>
</td>
<td style="width: 84%; padding-left:5px;">
<?php $features = explode(',', $user_feature['tab_title_labels']); ?>
<?php if (!empty($features)): ?>
<?php foreach ($features as $feature): ?>
<?php
$checked = '';
$hide = 'style="display:none;"';
if (in_array($feature, $features_car)) {
$checked = 'checked';
$hide = '';
};
?>
<label <?php echo $hide; ?>>
<span class="featuresspan"><?php echo esc_attr($feature); ?></span>
</label>
<?php endforeach; ?>
<?php endif; ?>
</td>
</tr>
</table>
<?php }
}
?>
To compare two arrays ($features and $features_car) in php, the most direct tool for that task is array_intersect(). Although I should tell you (and researchers) that it would be far, far better to Normalize your table data so that you are not storing comma-separated values. Having a normalized/granular table of labels and their individual features spread across multiple rows will allow your application to enjoy cleaner and more efficient queries and allow you to move the filtering processes from php to mysql where it belongs.
Code: (Demo)
$features_car = ['USB jungtis', 'Rūko žibintai'];
foreach ($user_features as $user_feature) {
$features = explode(',', $user_feature['tab_title_labels']);
$matched_features = array_intersect($features, $features_car);
if ($matched_features) {
echo "<table>";
echo "<tr>";
echo "<td>";
echo "<div class=\"heading-font\">{$user_feature['tab_title_single']}</div>";
echo "</td>";
echo "<td>";
foreach ($matched_features as $show_feature) {
echo "<label><span class=\"featuresspan\">{$show_feature}</span></label>";
}
echo "</td>";
echo "</tr>";
echo "</table>";
}
}
Output:
<table>
<tr>
<td>
<div class="heading-font">Audio/video įranga</div>
</td>
<td>
<label><span class="featuresspan">USB jungtis</span></label>
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="heading-font">Eksterjeras</div>
</td>
<td>
<label><span class="featuresspan">Rūko žibintai</span></label>
</td>
</tr>
</table>
p.s. I don't know if I support the stacking of <table> elements like this, but it is up to you how you wish to style your markup. You should move ALL of your inline styles to an external stylesheet so that your markup is easier to read and manage. I removed the esc_attr() only so that my demo would work without errors -- you can safely apply it to all variables that you are concerned about.
How can I join a foreach with another foreach in the query WHERE.
I have stored in the second Foreach the contents with the ID of the first ID and wants to display this in the respective column.
So if in the second column db_buy.tb_buy_shop has the ID 2, this should show in the HTML column where the db_shop.tb_shop_id 2.
view:
<?php
foreach ($shops_where_1 as $shop_where_1):
foreach ($buy_sums as $buy_sum):
if($buy_sum['tb_buy_shop']==$shop_where_1['tb_shop_id']) {
$gesamtsumme = $buy_sum['gesamtsumme'];
}
endforeach;
?>
<tr>
<td><?php echo $shop_where_1['tb_shop_name']; ?></td>
<td></td>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($buy_sum['gesamtsumme'],2,",",".");?>" disabled></div></td>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($gesamtsumme,2,",",".");?>" disabled></div></td>
<td></td>
<td></td>
</tr>
<?php
// endforeach;
endforeach;
?>
Model:
public function shops_where_1()
{
$this->db->select('*');
$this->db->from('db_shop');
$this->db->where('db_shop.tb_shop_buy = 1');
$this->db->order_by('tb_shop_name');
$query = $this->db->get();
return $query->result_array();
}
public function buy_sums()
{
$this->db->select('(SELECT SUM(db_buy.tb_buy_gesamt) FROM db_buy) AS gesamtsumme');
$this->db->select('(SELECT SUM(db_buy.tb_buy_abbezahlt) FROM db_buy) AS abbezahlt', FALSE);
$this->db->select('tb_buy_shop');
$this->db->from('db_buy');
$query = $this->db->get();
return $query->result_array();
}
Controller:
public function buy_shop($slug)
{
// if (!$this->session->userdata('logged_in'))
// {
// redirect('users/login');
// }
$data['get_shops'] = $this->Admin_model->get_shops($slug);
$data['shops_where_1'] = $this->Admin_model->shops_where_1();
$this->load->view('templates/header_acp');
$this->load->view('admin/buy_shop', $data);
$this->load->view('templates/footer');
}
If I am correct in what you are asking it seems that you are just wanting to know how to format the code so that it will build the table. This would be how to do so.
<?php
foreach ($shops_where_1 as $shop_where_1):
?> <tr>
<td><?php echo $shop_where_1['tb_shop_name']; ?></td>
<?php
foreach ($buy_sums as $buy_sum):
if($buy_sum['tb_buy_shop']==$shop_where_1['tb_shop_id']) {
?>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($buy_sum['gesamtsumme'],2,",",".");?>" readonly></div></td>
<?php
}
endforeach; // endforeach 2
</tr>
endforeach; // endforeach 1;
?>
I can't call my controller in view page. even if i use print_r in controller but it didn't show. I have body_product.phtml view code:
<table class="table table-hover table-condensed">
<tbody>
<?php
$i = 1;
//print_r("list produk:".$this->productList);
foreach ($this->productList as $data) {
$desc=explode(",",$data['descriptions']);
?>
<tr>
<th colspan="3">
<input type="hidden" id="id_pack" name="id_pack" value="<?php echo $data['package_id']; ?>">
<input type="hidden" id="nama_pack" name="nama_pack" value="<?php echo $data['package_name']; ?>">
<h4 align="center" class="title-pack"><?php echo $data['package_name']; ?></h4>
</th>
</tr>
<tr id="dashe">
<td>
<ul class="myul">
<?php foreach($desc as $descriptions) { ?>
<li class="myli"> <?php echo $descriptions; ?></li>
<?php } ?>
</ul>
</td>
<td>
<h4 class="prize">
<?php setlocale(LC_MONETARY, 'id_ID');
echo money_format('%.2n', $data['package_price']); ?>
/ month
</h4>
</td>
<td>
<p id="btn-hrm" class="mybutton" data-toggle="modal" data-target=".mymodal">Order</p>
</td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
and in the indexController:
public function loadProductAction() {
$viewModel = new ViewModel();
$storage = Product\Storage::factory($this->getDb());
$productList = new Product($storage);
$data = $productList->loadProduct();
$arr = array();
if ($data) {
foreach ($data as $val) {
array_push($arr, $val);
}
}
print_r('teaaat'.$arr);
$viewModel->setVariables(array('productList' => $arr))
->setTerminal(true);
return $viewModel;
}
If I open print_r in the view,it show error Warning: Invalid argument supplied for foreach() in.... I think it cause of view can't call the controller.
Help me please,thanks.
Firstly when you try to use print_r it is expecting an array. So it should be something like print_r($arr). Also give this a try and see if it helps.
public function loadProductAction() {
$storage = Product\Storage::factory($this->getDb());
$productList = new Product($storage);
$data = $productList->loadProduct();
$arr = array();
if (is_array($data) && !empty($data)) {
foreach ($data as $val) {
array_push($arr, $val);
}
} else {
echo '$data is not an array or it is empty';
}
print_r($arr);
return new ViewModel(array(
'productList' => $arr
));
}
I have a page that contains an ordering form, on this form it lists the vendor information and then each of the products for the vendor underneath and in front of the product is an input field that allows the user to input the quantity of each product that they want.
Upon submitting the information goes to a confirmation page where I need to be able to show the order information. On the form on the order page, I have a hidden field that contains the vendor id. and the vendor id is put once for each vendor. What I need to be able to do is not only echo out the quantity but also echo out the vendor id specific for each order. My code is below. The first block is the order page and then the block below that will be the confirm page.
As it stands right now underneath every quantity it displays all the vendor ids as opposed to just the one I need.
<?php defined('C5_EXECUTE') or die("Access Denied.");?>
<div class="ccm-ui">
<?php
$db= Loader::db(); //This loads the database helper.
Loader::model('user'); //This loads the user Model.
$user = new User();
$userInfo = UserInfo::getByID($user->getUserID()); //This gets the user info for the current user.
$userCostCenter = $userInfo->getAttribute('cost_center'); //This sets a variable equal to the attribute Cost Center for the current user.
//The if statement below checks if the user is an admin and then displays the info accordingly.
if ($userCostCenter === "Admin") {
?>
<form name="SelectCostCenter" action="/adminorder" method="POST">
<select name="CostCenter">
<option value="unitedilluminating">United Illumination</option>
<option value="clp">CL&P</option>
</select>
<input type="submit" value="Continue">
<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
<?php
} elseif ($userCostCenter === "United Illuminating") {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php
$query = 'SELECT * FROM Vendors WHERE costCenterID = 1';
$productQuery = 'SELECT * FROM Products WHERE costCenterID = 1';
$results = $db->getAll($query);
$productResults = $db->getAll($productQuery);?>
<table class="table">
<thead>
<tr>
<th>Quantity/Product</th>
<th>Category</th>
<th>Vendor</th>
<th>Address</th>
</tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
<td></td>
<td><?php echo $vendor['Category']; ?></td>
<td><?php echo $vendor['Vendor']; ?></td>
<td><?php echo $vendor['Address']; ?></td>
</tr>
<?php foreach ($productResults as $product) { ?>
<tr class="product">
<td colspan="4"><span class="name"><input type="text" name="quantities[]" size="1" /><?php echo $product['Product'];?></span></td>
</tr>
<?php } ?>
<td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
<?php
}?>
</table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
else {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php $query = 'SELECT * FROM Vendors Where costCenterID = 2';
$productquery = 'SELECT * FROM Products WHERE costCenterID = 2';
$results = $db->getAll($query);
$productresults = $db->getAll($productquery);?>
<table class="table">
<thead>
<tr>
<th>Quantity/Product</th>
<th>Category</th>
<th>Vendor</th>
<th>Address</th>
</tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
<td></td>
<td><?php echo $vendor['Category'];?></td>
<td><?php echo $vendor['Vendor'];?> </td>
<td><?php echo $vendor['Address'];?></td>
</tr>
<?php
foreach ($productresults as $product){
?>
<tr class="product">
<td colspan="4"><span class="name"><input type="text" name="quantities[<?php echo $vendor['vendorID']; ?>]" size="1" /><?php echo $product['Product'];?></span></td>
<td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
</tr>
<?php
}
?>
<?php
}?>
</table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
?>
This is the confirm page below.
<?php defined('C5_EXECUTE') or die("Access Denied.");
$db= Loader::db();
$quantity = $_POST['quantities'];
$vendor = $_POST['vendor'];
$minimumorder = 25;
foreach($quantity as $num){
if ($num >= $minimumorder){
echo "$num";
echo "</br>";
foreach($vendor as $vendors){
echo "$vendors";
echo "</br>";
}
}
}
?>
I appreciate any help anyone can give. This has had me stumped for a few days actually.
you might want to rearrange your array, and do something like:
$i = 0;
foreach ($productresults as $product) {
echo '<input name="product['.$i.'][quantity]" />';
echo '<input name="product['.$i.'][vendor_id]" value="'.$vendor['vendorID'].'" type="hidden" />';
++$i;
}
The resulting array in $_POST would have the quantities & their vendor separated into their own arrays.
In your code $vendor['vendorID'] seems the key of your $_POST['quantities'] so in your confirm page you could use:
foreach($quantity as $vendorid=>$num){
if ($num >= $minimumorder){
echo "$num";
echo "</br>";
echo "$vendorid";
}
}
I want to have the ability to show all the entries in a database table and by each one give the user the ability to delete specific ones.
I am currently using a for each loop that loops through the database showcasing each entry.
$result = mysql_query("SELECT * FROM KeepScores");
$fields_num = mysql_num_fields($result);
echo "<table><tr>";
// printing table headers
echo "<td>Recent Posts</td>";
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
How would to add a delete button that appears by each one and removes the entry from the database table?
You can do it with forms:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr>
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
</table>
//delete.php:
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
header('Location: main.php');
}
Or you can do it with jQuery and AJAX:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr id="<?php echo $row['id']; ?>">
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<button class="del_btn" rel="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
<script>
$(document).ready(function(){
$('.del_btn').click(function(){
var del_id = $(this).attr('rel');
$.post('delete.php', {delete_id:del_id}, function(data) {
if(data == 'true') {
$('#'+del_id).remove();
} else {
alert('Could not delete!');
}
});
});
});
</script>
//delete.php
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
if($result !== false) {
echo 'true';
}
}
It's all untested and sure needs some adjustment for your specific project, but I think you get the idea and I hope it helps.
Next time, please post your schema if you ask stuff about database.
I thought I would improve on this a little bit by wrapping the delete post in a class and function. I was having the same problem. and this worked great for me. Thanks again # Quasdunk
<?php
// Class to hold the remove post function
class someClass{
//Function for removing the post
function removePost(){
if(isset($_POST['delete_id']) && (!empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM post WHERE post_id='".$delete_id."' AND post_member='" . $_SESSION['SESS_USER'] . "'");
if($result !== false) {
echo 'true';
}
}
}
}
if(isset($_SESSION['SESS_MEMBER_ID'])){
$member = $_SESSION['SESS_USER'];
$res = mysql_query("SELECT * FROM post WHERE post_member='$member' ORDER BY timestamp DESC") or die(mysql_error());
$i = new someClass;
while($row = mysql_fetch_array($res)){
echo '<div style="width:100%;margin:0 auto;border-top:thin solid #000;">';
echo '<div style="width:600px;margin:0 auto;padding:20px;">';
echo $row['post_text'] . '<br>';
$postID = $row['post_id'];
echo '<div style="border-top:thin solid #000;padding:10px;margin-top:5px;background-color:#CCC;">';
echo 'You posted this on: ' . $row['post_date'] . '#' . $row['post_time'];
echo '<div style="float:right;">
<form method="post" action="'. $i->removePost() .'">
<input type="hidden" name="delete_id" value="'.$row['post_id'].'" >
<input type="submit" value="Delete Post">
</form>
</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>
Produce a key to each table, using jquery,then link it to a php file which an accept the key and delete from the specific table (which also can be passed through jquery)