php mysql select with different values - php

I have a table with exercises and I want to generate I want that the visitor can select the number of exercises that him wants... every exercise must be appear in a different select and I need that the first value in every select will be different. I have this code:
<form class="form-horizontal" action="wod_auto_sartu.php" method="post" enctype="multipart/form-data">
<?php
$numbers = range(1, 12);
shuffle($numbers);
for ($i=1;$i<=$num_jornadas;$i++){
$numbers2=$numbers[$i];
$random_wod=$wod[$numbers2];
?>
<div class="form-group">
<label for="inpuName" class="col-md-4 control-label"><font color="#fff"><b><?php echo _("Jornada: $i"); ?></b></font></label>
<div class="col-md-6">
<select class="form-control c-square c-theme" id="wod" style="opacity: .8;max-width:80%;" name="num_jornadas" method="post">
<?php
foreach ($numbers as $row){
echo '<option value='. $random_wod[0] .'>'. $random_wod[1] .'</option>';
}
?>
</select>
</div>
</div>
<?php
}
?>
In this moment, I get info as I have mentioned, but if I unfold the select I get the same value in all options, I need as I said that the first option in every option will different but when I unfold, I need see all other exercises, not the same...
How can I solve it?
Example image

Problem solved!! I had an error of concept... I was introducing the same array all the time waiting for a different output ... as Einstein said... "Stupidity is repeating the same error expecting different results"
The correct code:
<form class="form-horizontal" action="wod_auto_sartu.php" method="post" enctype="multipart/form-data">
<?php
$lista_wods = array();
foreach($wod as $row=>$value):
$lista_wods[] = '<option value="'.$row.'">' .$value[1].'</option>';
endforeach;
$numbers = range(1, 12);
shuffle($numbers);
for ($i=1;$i<=$num_jornadas;$i++){
$numbers2=$numbers[$i];
$random_wod=$wod[$numbers2];
?>
<div class="form-group">
<label for="inpuName" class="col-md-4 control-label"><font color="#fff"><b><?php echo _("Jornada: $i"); ?></b></font></label>
<div class="col-md-6">
<select class="form-control c-square c-theme" id="wod" style="opacity: .8;max-width:80%;" name="num_jornadas" method="post">
<?php
print_r ('<option value='. $random_wod[0] .'>'. $random_wod[1] . '</option>');
print_r(array_values($lista_wods));
?>
</select>
</div>
</div>
<?php
}
?>
Thanks to those who have tried to help me!

Related

Using php to dynamically output an html table based on input from a form on the same page

I'm designing a web interface for my sql database. I ported this from an Access database that used separate forms for each of its queries. I could do that easily, but the forms are very similar and I can put them all into one just by using 3 html select boxes. I made the form, I have input for to/from date, one select to choose between totals of departments or employees, and one for selecting and individual department or employee. I would also like to do this without the use of JS if possible, I've only had to use JS as a print function so far.
First of all, I want the table to show on the same page as the form, but I don't care if I have to force it to refresh, for this I used <form method="post action="" and maybe if(isset($_POST["Submit])){. I have seen some controversy about the action="" but I thought it would work until I know what is appropriate to use in my case, which is why I'm pointing it out now.
Then I need to check the output of $_POST, this is something I don't know how to right a conditional for. So, for $_POST["select-emp/dep"] if the user selects department it would show one table, if the user selects employee it would be another. If the user leaves $_POST["select-emp/dep"] blank and chooses something from $_POST["select-employee"] or $_POST["select-department"] it will output another table. The conditions for those two should just be anything besides empty, they aren't handling any sql yet so that's fine I think. If more than one box is filled then I will have it display an error, same with if nothing is entered. I'm trying to think about it but is this just something as simple as if($_POST["select-emp/dep"]("Department"))?
Here is the code I have so far if I did not explain well enough:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Incidents Queries</title>
<meta charset="utf-8">
</head>
<body>
<div id="body">
<div id="nav">
<ul>
<li class="active"><a href='/incidents/index.php'>Index</a></li>
<li class="active"><a href='/incidents/employeestable.php'>Employees</a></li>
<li class="active"><a href='/incidents/departmentstable.php'>Departments</a></li>
<li class="active"><a href='/incidents/incidentstable.php'>Incidents</a></li>
<li class="active"><a href='/incidents/incidentsform.php'>Incidents Form</a></li></ul>
</div>
</div>
<div id="button">
<button onclick="javascript:demoFromHTML();">PDF</button>
</div>
<div id="content">
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "incidents";
$mysqli = mysqli_connect($hostname,$username,$password,$db);
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$sqlerror = $mysqli->errno . ' ' . $mysqli->error;
?>
<form method="post" action="">
<div class="row">
<div id="col-25">
<label for="date-from">Select a date range</label>
</div>
<div id="col-75">
<input type="date" id="date-from" name="date-from">
To
<input type="date" id="date-to" name="date-to">
</div>
</div>
<div class="row">
<div id="col-25">
<label for="select-emp/dep">Choose from one</label>
</div>
<div id="col-75">
<select id="select-emp/dep" name="select-emp/dep">
<option>Select one</option>
<option value="Department">Department</option>
<option value="Employee">Employee</option>
</select>
<select id="select-department" name="select-department">
<option>Select one</option>
<?php
$dsql = "SELECT `Department` FROM departments";
$dquery = mysqli_query($mysqli, $dsql);
while ($dlist = mysqli_fetch_array($dquery)) {
$d = $dlist['Department'];
echo '<option value="'. $d .'">' . $d . '</option>';
}
?>
</select>
<select id="select-employee" name="select-employee">
<option>Select one</option>
<?php
$esql = "SELECT `Employee` FROM employees";
$equery = mysqli_query($mysqli, $esql);
while ($elist = mysqli_fetch_array($equery)) {
$e = $elist['Employee'];
echo '<option value="'. $e .'">' . $e . '</option>';
}
?>
</select>
</div>
</div>
<div class="row">
<div id="submit">
<input type="submit" value="Submit" >
</div>
</div>
</form>
<br>
<?php
if(isset($_POST["Submit"])){
if($_POST["select-emp/dep"] *This needs to be conditional* ?>
*table code goes here*
<?php endif
if($_POST["select-emloyee"] ?>
<table>
<?php endif
if($_POST["select-department"] ?>
<table>
<php endif
} ?>
</body>
</html>

Dynamic HTML form and saving into multiple records in DB

I am writing a small app using codeigniter to track payments and payments dates. I have a form where we input details about the customer, and among those fields, we enter how many instalments will be used for the customer to pay his entire premium.
The next page takes this number, and generates the same number of fields. For example instalment_1, date_1; instalment_2, date_2.
<?php for($i=1; $i<=$instalments; $i++):?>
<div class="row form-group">
<div><label>Instalment <?php echo $i;?></label></div>
<div>
<input type="text" id="instalment_<?php echo $i;?>" name="instalment_<?php echo $i;?>">
</div>
</div>
<div class="row form-group">
<div><label>Premium Date <?php echo $i;?></label></div>
<div>
<input type="text" id="date_<?php echo $i;?>" name="date_<?php echo $i;?>" class="form-control">
</div>
</div>
The idea is now to loop through this form, and save each set of instalment and date into 1 record in the database.
i was able to do it if i have only one type of fields (eg instalments). but when i added the date, it is saving, but each record gets created twice.
$post = $this->input->post();
foreach($post as $key=>$value){
//check for the word instalment
if(strpos($key, "instalment") == 0){
//get the instalment number from the name of the field
$inst_number = substr($key, -1);
//Use $category_id and $value in this loop to build update statement
echo "<script type='text/javascript'>alert('$inst_number');</script>";
//$arr = "inst_".$inst_number;
//reform the name of the field to get the values from it
$instalment_var = "instalment_".$inst_number;
$date_var = "date_".$inst_number;
echo "<script type='text/javascript'>alert('$instalment_var');</script>";
$amount = $this->input->post($instalment_var);
$date = $this->input->post($date_var);
echo "<script type='text/javascript'>alert('$amount');</script>";
$arr = array(
'payment_amount' => $amount,
'payment_date' => $date,
'policy_name' => $policy_id,
);
if(!$this->Policy_model->save_payment_details($arr)){
redirect('ships/index');
}
}
}
How can i do it so that i don't have every record saved twice? and if i add another field, it will get saved 3 times.
i though about naming the div, and working on this, but i didnt get any result.
Thank you in advance.
Try this code.This will resolve your issue.
View:
<form action="action.php" method="POST">
<?php for($i=1; $i<=$instalments; $i++){?>
<div class="row form-group">
<div><label>Instalment <?php echo $i;?></label></div>
<div>
<input type="text" id="instalment[]" name="instalment[]">
</div>
</div>
<div class="row form-group">
<div><label>Premium Date <?php echo $i;?></label></div>
<div>
<input type="text" id="date_<?php echo $i;?>" name="date[]" class="form-control">
</div>
</div>
<?php } ?>
<input type="submit" name="submit" value="submit">
</form>
In your controller:
$post = $this->input->post();
$i=0;
foreach($post as $key=>$value){
$arr = array(
'payment_amount' =>$post['instalment'][$i],
'payment_date' => $post['date'][$i],
'policy_name' => 1,
);
$i++;
}exit;

PHP - Separate tags with span tags from database

I am creating a website that will have some places and for each place I want to have tags. I have a database with a table name checkbox and inside the table I have a column called checkbox_title. In my backend I have three checkboxes which have the values basketball, baseball, and volleyball. If I check all or some of the boxes I would like to add them to the database, inside checkbox_title column. I have done this using implode(). However, when I insert them in my database there are separated with a comma, which is fine but in the front end of my website I would like to show the tags as separated wrapped in a span tag, not as a group using span tag. What I would like is to know if there is a way to separated implode() values using span tags instead of comma. Hope someone can help me to solve this problem.
PS. I have added comments in my front end to understand how I want this to work.
Backend Code:
<form method="post" action="">
<div class="form-group">
<?php
if(isset($_POST['add_tags'])) {
$tags = implode(', ' , $_POST['tags']);
$query = "INSERT INTO checkbox (checkbox_title) VALUES ('$tags') ";
$result = mysqli_query($connection, $query);
if(!$result) {
die("Error ".mysqli_error($connection));
}
}
?>
<label class="checkbox-inline"><input type="checkbox" name="tags[]" value="basketball">Basketball</label>
<label class="checkbox-inline"><input type="checkbox" name="tags[]" value="baseball">Baseball</label>
<label class="checkbox-inline"><input type="checkbox" name="tags[]" value="volleyball">Volleyball</label>
</div>
<div class="form-group">
<input type="submit" name="add_tags" class="btn btn-primary" value="Submit">
</div>
</form>
Front end code:
<div class="container">
<div class="row">
<div class="col-xs-12">
<?php
$select_all_query = "SELECT * FROM checkbox";
$select_all_query_con = mysqli_query($connection, $select_all_query);
while($tags = mysqli_fetch_assoc($select_all_query_con)) {
$product_tags = $tags['checkbox_title'];
echo "<span class='c_span'>$product_tags</span>";
}
?>
<!--
<span class="c_span">Basketball</span>
<span class="c_span">Baseball</span>
<span class="c_span">Volleyball</span>
-->
</div>
</div>
</div>
You can update front code as bellow
<div class="container">
<div class="row">
<div class="col-xs-12">
<?php
$select_all_query = "SELECT * FROM checkbox";
$select_all_query_con = mysqli_query($connection, $select_all_query);
while($tags = mysqli_fetch_assoc($select_all_query_con)) {
$product_tags = $tags['checkbox_title'];
$tags = explode(',', $product_tags);
foreach ($tags as $item) {
echo "<span class='c_span'>". $item ."</span>";
}
}
?>
<!--
<span class="c_span">Basketball</span>
<span class="c_span">Baseball</span>
<span class="c_span">Volleyball</span>
-->
</div>
</div>
</div>

Even when DB has records, while trying to edit records, code does not work

I am trying to update records in 'pantry-info' table. Code goes to the if loop instead of else
The code is mentioned below, what am I doing wrong?
DB connection is:
require 'config/connectDB.php';
session_start();
$id = $_GET['id'];
$sql = 'SELECT * FROM temp WHERE'. " pan_id = '$id'";
$result = mysqli_query($conn,$sql);
Trying to read DB table values in form as follows:
<?php
if(mysqli_num_rows($result1) == 0)
{
echo '<h2>This record already exists. Do you want to delete it?</h2>';
} else { ?>
<h4>Edit the record here:</h4>
<br>
<form name="pantryinfo" id="pantryForm" method = "post" action="update.php" data-toggle="validator" role="form">
<?php while ($row = mysqli_fetch_array($result1,MYSQLI_ASSOC)) { ?>
<div class="control-group form-group">
<div class="controls">
<input type="hidden" class="form-control" id="panid" name="panid" value="<?php echo $row['pan_id'];?>">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo $row['pname'];?>" required>
<p class="help-block"></p>
</div>
</div>
</form>
<?php
}
}
?>
Similar code works fine on a different page retrieving values from another DB table. Please help. Thanks in advance.
I resolved the issue. In the SQL query I used $result as a variable and in the if-else code I was using result1.
Thank you Fred for pointing it out.

insert form to database

i have program codeigniter anda i want to insert form to database.
code view:
<form target="paypal" method="post">
<div class="field1">
<div class="field">
<label>Nama</label>
<input placeholder="Nama" name="nama" type="text">
</div>
<div class="field">
<label>No. HP</label>
<input placeholder="No. HP" name="handphone" type="text">
</div>
<div class="field">
<label>Alamat</label>
<input placeholder="alamat" name="alamat" type="text">
</div>
<div class="field">
<label>Jumlah</label>
<div class="selectbox">
<select name="jumlah" id="">
<?php for ($i=1; $i <= 20; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div>
</div>
<button type="submit" name="submit" class="ui teal button order-button">Order now</button>
</div>
</form>
code controller
function simpanOrder()
{
$this->load->model("M_order");
$data['nama'] = $_POST['nama'];
$data['handphone'] = $_POST['handphone'];
$data['alamat'] = $_POST['alamat'];
$data['jumlah'] = $_POST['jumlah'];
if($this->input->post('submit')){
$this->M_order->insert($data);
}
}
when i click submit data not insert to database. so can you help me with this code problem? thanks.
Your form doesn't have an action, and therefore may not be going to the function you want it to. (/controller/function)
<form target="paypal" method="post">
Also, instead of using a button to submit the form - try using <input type="submit"...
Using the <button>, in some browsers, you would have "submit" submitted, in others, "Order now".
If the above doesn't work - check your SQL.
As a side note, CodeIgniter has a form helper and a form_validation library which are quite useful if you're already using CodeIgniter. That won't fix your problem but it's just something I felt I would point out.
See:
http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html
http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
Call model from controller. and write below code in model.
$data = array(
'handphone' => $this->input->post('handphone'),
'alamat' => $this->input->post('alamat'),
)
In this array key is database columnname.
$this->db->insert(yourtablname, $data);
$insert_id = $this->db->insert_id();
You need to define action attribute in form tag where you will provide controller name and method name like this
<form action="<?php echo site_url('controllername/simpanOrder')?>" method="post">
After posting you can debug your code like this
$post = $this->input->post();
echo '<pre>';
print_r($post);
Then
if($this->input->post('submit')){
$this->M_order->insert($data);
}
And finally
echo $this->db->last_query();
This will display you the last query run.

Categories