PHP code
<?php
include("connect.php")
$activityid = "select activity.activity_name,activity.activity_id
from activity
join serviceactivitymap on activity.activity_id = serviceactivitymap.activity_id
where serviceactivitymap.service_id = 1";
$activityvalue = $conn->query($activityid) or die ($conn>error.__LINE__);
$activities = [];
while ($row = $activityvalue->fetch_assoc()) {
$activities[] = $row;
}
$serviceid = "select * from service";
$servicevalue = $conn->query($serviceid) or die ($conn->error.__LINE__);
$services = [];
while ($row = $servicevalue->fetch_assoc()) {
$services[] = $row;
}
foreach ($services as $service):
?>
<form action = "index.php method = "post">
<input name='serviceone[]' data-toggle="modal" data-target="#myModal" type='checkbox' data-id='Incometax' value="<?php echo $service['service_id']?>"/><?php echo $service['service_name']?>
<br>
<?php foreach ($activities as $activity):?>
<input name='activityone[]' data-toggle="modal" data-target="#myModal" type='checkbox' data-id='Incometax' value="<?php echo $activity['activity_id']?>"/><?php echo $activity['activity_name']?>
<br>
<?php endforeach ?>
<?php endforeach ?>
</form>
Output of the code is :
Incometax
Revised filling
Return filling
Gst
Revised filling
Return filling
Tds
Revised filling
Return filling
I have 3 tables services, activity, serviceactivitymap
where I store service_id, service_name in services and activity_id, activity_name in activity and service_id and related activity_id inserviceactivitymapbut i dont know how to display related activities under services.
serviceactivitymap` table structure is
Incometax
Revised filling
Return filling
Gst
Tax Payment
Statutory Audit
Tds
Internal Audit
Stock Audit
Can I display it like this?
<?php include("connect.php")?>
<?php
$activityid = "select activity.activity_name,activity.activity_id
from activity
join serviceactivitymap on activity.activity_id = serviceactivitymap.activity_id
where serviceactivitymap.service_id = 1";
$activityvalue = $conn->query($activityid) or die ($conn>error.__LINE__);
$activities = [];
while ($row = $activityvalue->fetch_assoc()) {
$activities[] = $row;
}
$serviceid = "select * from service";
$servicevalue = $conn->query($serviceid) or die ($conn->error.__LINE__);
$services = [];
while ($row = $servicevalue->fetch_assoc()) {
$services[] = $row;
}
foreach ($services as $service):
?>
<form action = "index.php" method = "post">
<input name='serviceone[]' data-toggle="modal" data-target="#myModal" type='checkbox' data-id='Incometax' value="<?php echo $service['service_id']?>"/><?php echo $service['service_name']?>
<br>
<?php foreach ($activities as $activity):?>
<input name='activityone[]' data-toggle="modal" data-target="#myModal" type='checkbox' data-id='Incometax' value="<?php echo $activity['activity_id']?>"/><?php echo $activity['activity_name']?>
<br>
<?php endforeach ?>
<?php endforeach ?>
</form>
There you go. You are missing a closing quotation mark at form action
Related
<?php
include("connection.php");
// Collect services.
$serviceid = "select * from service";
$servicevalue = $conn->query($serviceid) or die ($conn->error.__LINE__);
$services = [];
while ($row = $servicevalue->fetch_assoc()) {
$services[] = $row;
}
// Collect activities.
$activityid = "select * from activity";
$activityvalue = $conn->query($activityid) or die ($conn->error.__LINE__);
$activities = [];
while ($row = $activityvalue->fetch_assoc()) {
$activities[] = $row;
}
// Collect something else?
$perid = "select * from periodicity";
$pervalue = $conn->query($perid) or die ($conn->error.__LINE__);
$pers = [];
while ($row = $pervalue->fetch_assoc()) {
$pers[] = $row;
}
foreach ($services as $service):
?>
<form method="post" action="doalert.php" name="test">
<ul>
<li>
<input name='arr[1][service]' type='checkbox' data-id='Incometax'value="<?php echo $service['service_id']?>"/><?php echo $service['servicename']?>
<ol>
<li>
<?php foreach ($activities as $activity) : ?>
<input type='checkbox' name='arr[1][activity][]' value="<?php echo $activity['activity_id']?>" /><?php echo $activity['nameofactivity'];?>
</li>
<br>
<?php endforeach;?>
</ol>
</li>
</ul>
<br>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit"/>
</form>
The value of services and activity come from the database, I want to store all the services and related activity in DB serviceactivitymap table, here is my php code but when I store the data then last checked service value only stored with all the activity checked
incometax
filling
return
gst
filling
return
If incometax,gst,filling,return is checkboxes then when we check :
incometax->filling,return and gst->filling,return
Then the code only store
gst->filling,return and gst->filling,return
Instead of :
incometax->filling,return and gst->filling,return
You need to pass the service_id to the activity name like :
<input type='checkbox' name='activity[<?php echo $service['service_id']?>][]' value="<?php echo $activity['activity_id']?>" /><?php echo $activity['nameofactivity'];?>
Full form will be like :
<form method="post" action="doalert.php" name="test">
<?php foreach ($services as $service): ?>
<ul>
<li>
<input name='service[]' type='checkbox' data-id='Incometax' value="<?php echo $service['service_id']?>"/><?php echo $service['servicename']?>
<ol>
<?php foreach ($activities as $activity): ?>
<li>
<input type='checkbox' name='activity[<?php echo $service['service_id']?>][]' value="<?php echo $activity['activity_id']?>" /><?php echo $activity['nameofactivity'];?>
</li>
<?php endforeach;?>
</ol>
</li>
</ul>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit"/>
</form>
In your code, the form start tag is created every time a loop runs as it is inside the service foreach. Try taking it outside the loop
Also, the name you used for service input tag as arr[1]["service"] will be overwritten everytime the loop runs. Instead I would suggest if you can use a variable increment in each loop like
$i=0;
foreach($services as $service)
{
.....
<input name='arr[$i][service]' ......
$i++;
}
This will differentiate the input tag name and saving may happen properly.
<?php
include("connection.php")
$serviceid = "select * from services";
$servicevalue = $conn->query($serviceid) or die ($conn->error.__LINE__);
$activityid = "select * from activity";
$activityvalue = $conn->query($activityid) or die ($conn->error.__LINE__);
$perid = "select * from perodicity";
$pervalue = $conn->query($perid) or die ($conn->error.__LINE__);
while($row=$servicevalue->fetch_assoc()) :
?>
<input name="activity[]" type="checkbox" value="<?php echo $row['id']; ?>" id="" />
<?php
echo $row['servicename'];
echo "<br>";
?>
<br><br>
<?php while($row=$activityvalue->fetch_assoc()) :?>
<input name="activity[]" type="checkbox" value="<?php echo $row['id']; ?>" />
<?php
$activity=$row['nameofactivity'];
echo "$activity";
echo "<br>";
endwhile;
?>
<br>
<?php endwhile; ?>
my output :
incometax
form
incometax
return
2.GST
form
2.GST
return
I Want
1.Incometax
form
Return
2.GST
form
return
It's because in both while() loops you assign a value to the same variable - $row. In your second while() loop, rename your variable to something like $row2 and see if it fixes your problem.
Edit
Thinking too quickly, what about this:
<?php
include("connection.php");
// Collect services.
$serviceid = "select * from services";
$servicevalue = $conn->query($serviceid) or die ($conn->error.__LINE__);
$services = [];
while ($row = $servicevalue->fetch_assoc()) {
$services[] = $row;
}
// Collect activities.
$activityid = "select * from activity";
$activityvalue = $conn->query($activityid) or die ($conn->error.__LINE__);
$activities = [];
while ($row = $activityvalue->fetch_assoc()) {
$activities[] = $row;
}
// Collect something else?
$perid = "select * from perodicity";
$pervalue = $conn->query($perid) or die ($conn->error.__LINE__);
$pers = [];
while ($row = $pervalue->fetch_assoc()) {
$pers[] = $row;
}
foreach ($services as $service):
?>
<input name="activity[]" type="checkbox" value="<?php echo $service['id']; ?>" id="" />
<?php
echo $service['servicename'];
echo "<br>";
?>
<br><br>
<?php foreach ($activities as $activity) : ?>
<input name="activity[]" type="checkbox" value="<?php echo $activity['id']; ?>" />
<?php
echo $activity['nameofactivity'];
echo "<br>";
endforeach;
?>
<br>
<?php
endforeach;
Changes: we first collect all services and activities. Then we loop through all services and for each service through all activities, and we output them.
In my database I have 2 tables:
To insert data, I have a form that populates dropdown options from the table formulation. This is what the insert form for formulation dropdown looks like:
<?php
$formulation = '';
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
$formulation .= '<option value="' . $row["formulationID"] . '">' . $row["formulation_name"] . '</option>';
}
?>
<select>
<option value="">Select formulation</option>
<?php echo $formulation; ?>
</select>
Now I am working on the ‘Update’ form. But my question is how can I populate the ‘Formulation’ field dropdown with the data from the formulation table (like as the insert form) but pre-selected with the existing formulation value for the name from the items table? Like this image below:
I am having problem with how I should build the form. How should I proceed with this form?
<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);
while ($row = $query->fetch_assoc()) {
$output['data'][] = array(
$row['name'],
);
}
echo json_encode($output);
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text"><br>
<label>Formulation</label>
<select >
<!--What should be the codes here? -->
</select>
</div>
<button type = "submit">Save changes</button>
</form>
Thanks in advance for your suggestion.
Note: I'm not a user of mysqli so maybe there will be some error, but you will get the idea. This will not tackle the update part, just the populate part
Since you are editing a certain item, I will assume that you have something to get the item's itemID.
<?php
$sql = "SELECT * FROM items WHERE itemID = ?";
$query = $connect->prepare($sql);
$query->bind_param("s", $yourItemID);
$query->execute();
$result = $query->fetch_assoc();
$itemName = $result['name'];
$itemFormulation = $result['formulation_fk'];
//now you have the name and the formulation of that certain item
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text" value="<?php echo $itemName; ?>"><br>
<label>Formulation</label>
<select >
<?php
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['formulationID']; ?>" <?php echo ($row['formulationID'] == $itemFormulation) ? 'selected' : ''; ?>>
<?php echo $row['formulation_name']; ?>
</option>
<?php
}
?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I changed the code to better suit the problem, there may be typos, just comment for clarification
If I have understand Your question... You have to put Your result into a string. For example:
<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);
$option = '';
while ($row = $query->fetch_assoc()) {
$name=$row['name'],
$option.='<option value="$name">$name</option>'
}
echo json_encode($output);
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text"><br>
<label>Formulation</label>
<select >
<?=$option?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I hope to be of help
This should do the trick:
<?php
$itemsSql = "SELECT * FROM items WHERE itemId = 5";
$itemQuery = $connect->query($sql);
$item = $itemQuery->fetch_assoc();
$formulationsSql = "SELECT * FROM formulation";
$formulationsQuery = $connect->query($sql);
$formulations = $itemQuery->fetch_assoc();
?>
<form action="updateItem" method="POST">
<div>
<label>Item Name</label>
<input type="text" value="<?= $item[0]['name']; ?>"><br>
<label>Formulation</label>
<select>
<?php foreach($formulations as $formulation){
echo '<option value="'. $formulation['formulationId'].'">' .
$formulation['formulation_name'] . '</option>';
} ?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
i want to ask why my combobox not indexed?
here is a code
<form method="post" action="../php/proses-list-maintenance.php">
<p style="font-size:15px; font-family:Coda; color:black;">Pilih Aplikasi :</p>
<select name="comboapp" class="form-control">
<?php foreach ($list_app as $app) : ?>
<option value="<?php echo $app['application_id'] ?>"><?php echo $app['application_name'] ?></option>
<?php endforeach ?>
</select>
<button type="submit" class="btn btn-success">Pilih</button>
</form>
and here is action code
include '../koneksi.php';
$id = $_POST['comboapp'];
$query = "select * from application where application_id = '$id'";
$hasil = mysqli_query($db, $query);
$data_app = mysqli_fetch_assoc($hasil);
$query2 = "SELECT * FROM maintenance where maintenance_id = '$id'";
$hasil2 = mysqli_query($db, $query2);
if ($hasil == true && $hasil2 == true) {
echo "<script>location.href='../admin/list-maintenance.php?aaa='+$id</script>";
$data_app = array();
while ($row = mysqli_fetch_assoc($hasil)) {
$data_app[] = $row;
}
$data_man = array();
while ($row = mysqli_fetch_assoc($hasil2)) {
$data_man[] = $row;
}
}
and when I run, I got this notice
undefined index: comboapp
when the combo box is selected and the button is clicked. I want the data id's in the combo box enter the data into $id
I am in the process of creating a document association tool whereby users can associate documents to a ticket from a pre-existing list of documents.
Thanks to the help of Alberto Ponte I was able to construct the initial radio box system (originally checkbox, changed for obvious reasons) from this question: Separate out array based on array data
Now that I have the radio buttons presenting correctly I am trying to get the data inserted into a MySQL with a row for each document. Following on from this I want to have it look up the table for each object and see if there is an existing entry and update that rather than inset a duplicate entry.
Apologies in advance for the soon to be depreciated code and I will be correcting the code going forward, however that is a much bigger task than I currently have time for.
Also apologies if the code is considered messy. I'm not fantastic at PHP and not too hot on best practices. Any quick pointers are always welcome.
Display code:
<?php
include "../includes/auth.php";
include "../includes/header.php";
## Security ########################################################
if ($company_id != 1 OR $gid < 7) {
echo' <div class="pageheader">
<div class="holder">
<br /><h1>Access Denied <small> Your attempt has been logged</small></h1>
</div>
</div>
';
exit();
}
// GET Values ###################################################
$jat_id = intval($_GET['id']);
$div_id = intval($_GET['div_id']);
## Page Start ########################################################
echo '
<div class="pageheader">
<div class="holder">
<br /><h1>Clovemead Job Management Platform<small> Welcome...</small></h1>
</div>
</div>
<div class="well5" style="width:1000px !important;">
<h3>Create Document Association</h3>
<table border=1>
<tr>
<td align=center><p>Here you can associate documents to Jobs and Tasks to.</p> </td>
</tr>
</table>';
$order2 = "SELECT * FROM documents";
$result2 = mysql_query($order2);
while ($row2 = mysql_fetch_array($result2)) {
$nice_name = $row2['nice_name'];
$doc_id = $row2['id'];
}
echo '
<h3>Documents</h3>
<table border=1>
<tr><th>Document Name</th><th>Document Type</th><th>Required</th><th>Optional</th><th>Not Required</th></tr>
';
$order2 = "SELECT * FROM documents ORDER BY type_id";
$result2 = mysql_query($order2);
while ($row2 = mysql_fetch_array($result2)) {
$nice_name = $row2['nice_name'];
$doc_id = $row2['id'];
$doc_type_id = $row2['type_id'];
echo '
<tr>
<td>'.$nice_name.'</td>';
$order3 = "SELECT * FROM document_types WHERE id = '$doc_type_id'";
$result3 = mysql_query($order3);
while ($row3 = mysql_fetch_array($result3)) {
$type_name = $row3['type_name'];
echo '
<td>'.$type_name.'</td>
<form method="post" action="create-doc-assoc-exec.php">
<input type="hidden" value="'.$doc_id.'" name="doc_id">
<input type="hidden" value="'.$jat_id.'" name="jat_id">
<input type="hidden" value="'.$div_id.'" name="div_id">';
$order4 = "SELECT * FROM document_assoc WHERE doc_id = '$doc_id'";
$result400 = mysql_query($order4);
$_results = mysql_fetch_array($result400);
if (!$_results) {
echo '
<td><input type="radio" name="req0" value="2"></td>
<td><input type="radio" name="req0" value="1"></td>
<td><input type="radio" name="req0" value="0" checked ></td>';
} else {
foreach ($_results as $result400) {
$requirement = $_results['requirement'];
}
$name = "req".$doc_id;
echo '
<input type="hidden" value ="'.$name.'" name="name">
<td><input type="radio" name="'.$name.'" value="2" '; if ($requirement == 2) { echo ' checked '; } echo '></td>
<td><input type="radio" name="'.$name.'" value="1" '; if ($requirement == 1) { echo ' checked '; } echo '></td>
<td><input type="radio" name="'.$name.'" value="0" '; if ($requirement < 1) { echo ' checked '; } echo '></td>';
}
}
echo '
</tr>';
}
echo '
</table>
<input type="submit" name="submit value" value="Create Document Association" class="btn success Large"></form>';
$order2 = "SELECT * FROM divisions WHERE id = '$div_id'";
$result2 = mysql_query($order2);
while ($row2 = mysql_fetch_array($result2)) {
$dbname = $row2['division_dbname'];
$order3 = "SELECT * FROM $dbname WHERE id = '$jat_id'";
$result3 = mysql_query($order3);
while ($row3 = mysql_fetch_array($result3)) {
$type = $row3['type'];
$type2 = strtolower($type);
echo '
<input type="button" value="Back" onclick="window.location='./view-'.$type2.'.php?id='.$jat_id.''" class="btn primary Large">
';
}}
echo '
</div>';
include "../includes/footer.php";
?>
Execute Code:
<?php
include "../includes/dbconnect.php";
$name = $_POST['name'];
$name = stripslashes($name);
$name = mysql_real_escape_string($name);
$doc_id = intval($_POST['doc_id']);
$jat_id = intval($_POST['jat_id']);
$div_id = intval($_POST['div_id']);
$req = intval($_POST[$name]);
$req_blank = intval($_POST['req0']);
if ($req_blank == 0) {
$requirement = 0;
} elseif ($req_blank == 1) {
$requirement = 1;
} elseif ($req_blank == 2) {
$requirement = 2;
} elseif ($req == 0) {
$requirement = 0;
} elseif ($req == 1) {
$requirement = 1;
} elseif ($req == 2) {
$requirement = 2;
}
foreach ($doc_id as $doc_id2) {
$order = "INSERT INTO document_assoc (jat_id, dept_id, doc_id, requirement) VALUES ('$jat_id', '$div_id', '$doc_id2', '$requirement')";
$result = mysql_query($order);
$order1 = "SELECT * FROM divisions WHERE id = '$div_id'";
$result1 = mysql_query($order1);
while ($row1 = mysql_fetch_array($result1)) {
$dbname = $row1['division_dbname'];
$order2 = "SELECT * FROM $dbname WHERE id = '$jat_id'";
$result2 = mysql_query($order2);
while ($row2 = mysql_fetch_array($result2)) {
$type = $row2['type'];
$type2 = strtolower($type);
if($result){
header("location:view-$type2.php?id=$jat_id&creation=success");
} else {
header("location:view-$type2.php?id=$jat_id&creation=failed");
}
}}
}
?>
A few reference points:
$doc_id is the ID of each document which is to be passed over as an array for use in a (i believe) foreach insert
$jat_id is the ticket id documents are being associated to
$div_id is the department the ticket is associated
And the radio boxes are to decider the requirement of each document. To be passed over inline with $doc_id
What I have tried:
After a fair bit of searching - finding nothing - rechecking my terminology and searching again I managed to find the suggestion of using base64_encode(serialize()) on the arrays but I could not get this to work.