want to display all activites under all services using php - php

<?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.

Related

Populating checkboxes from database using PHP - only last option is getting checked

I am trying to populate checkboxes with the data from my mysql database but for some reason only the last checkbox is being checked (for example if automotive, carpentry and hand tools should be checked, only hand tools is being checked) and I can't figure out why. The mysql statement is running correctly and giving me the correct information. Here is the relevant code.
<?php
require_once('../../private/initialize.php');
require_login();
if(!isset($_GET['id'])) {
redirect_to(url_for('/members/show_member_tools.php'));
}
$id = $_GET['id'];
if(is_post_request()) {
// Handle form values sent by new.php
$tool = [];
$tool['tool_ID'] = $id;
$tool['serial_number'] = $_POST['serial_number'] ?? '';
$tool['tool_name'] = $_POST['tool_name'] ?? '';
$tool['tool_description'] = $_POST['tool_description'] ?? '';
$tool['tool_picture'] = $_POST['tool_picture'] ?? '';
$category =[];
$category = $_POST['category_ID'];
$result = update_tool($tool, $category);
//get info for checkboxes
global $db;
if($result === true) {
$_SESSION['message'] = "The tool has been updated sucessfully";
redirect_to(url_for('/members/show_tool.php?id=' . $id));
} else {
$errors = $result;
}
} else {
$tool = find_tool_by_id($id);
if(isset($_GET['id'])){
$id=$_GET['id'];
$sql = "select category_name from category INNER JOIN tool_category ON category.category_ID = tool_category.category_ID where tool_category.tool_id=$id";
$query = mysqli_query($db, $sql);
while($row=mysqli_fetch_array($query)) {
// $str = "";
$str = $row['category_name'];
echo $str;
if (strpos($str , "automotive")!== false){
$checked1 ="checked";
echo "made it to automotive";
} else {
$checked1 ="";
}
if (strpos($str , "carpentry")!== false){
$checked2 ="checked";
echo "made it to carpentry";
} else {
$checked2 ="";
}
if (strpos($str , "home maintenance")!== false){
$checked3 ="checked";
echo "made it to home maintenance";
} else {
$checked3 ="";
}
if (strpos($str , "plumbing")!== false){
$checked4 ="checked";
} else {
$checked4 ="";
}
if (strpos($str , "yard and garden")!== false){
$checked5 ="checked";
} else {
$checked5 ="";
}
if (strpos($str , "hand tools")!== false){
$checked6 ="checked";
} else {
$checked6 ="";
}
}//end while loop
} //end if
} //end else
$tool_set = find_all_tools();
$tool_count = mysqli_num_rows($tool_set);
mysqli_free_result($tool_set);
?>
<?php $page_title = 'Edit Tool'; ?>
<?php include(SHARED_PATH . '/header.php'); ?>
<div id="content">
<div class="center">
« Back to My Tools
<h2>Edit Tool</h2>
</div>
<?php echo display_errors($errors); ?>
<form action="<?php echo url_for('/members/edit_tool.php?id=' . h(u($id))); ?>" method="post">
<fieldset class="form">
<img src ="<?php echo h($tool['tool_picture']); ?>" alt="<?php echo h($tool['tool_picture']); ?>"width="150"><br>
<label for="serial_number">Serial Number</label><br>
<input type="text" name="serial_number" value="<?php echo h($tool['serial_number']); ?>" ><br>
<label for="tool_name">Tool Name</label><br>
<input type="text" name="tool_name" value="<?php echo h($tool['tool_name']); ?>" ><br>
<label for="tool_description">Tool Description</label><br>
<input type="text" name="tool_description" value="<?php echo h($tool['tool_description']); ?>" ><br>
<label for="category_ID">Tool Category: </label><br>
<input type="checkbox" name="category_ID[]" value="1" <?php echo $checked1; ?>> <label for="1">Automotive</label> <br>
<input type="checkbox" name="category_ID[]" value="2" <?php echo $checked2; ?>> <label for="2">Carpentry</label> <br>
<input type="checkbox" name="category_ID[]" value="3" <?php echo $checked3; ?>> <label for="3">Home Maintenance</label> <br>
<input type="checkbox" name="category_ID[]" value="4" <?php echo $checked4; ?>> <label for="4">Plumbing </label><br>
<input type="checkbox" name="category_ID[]" value="5" <?php echo $checked5; ?>> <label for="5">Yard and Garden</label> <br>
<input type="checkbox" name="category_ID[]" value="6" <?php echo $checked6; ?>> <label for="6">Hand Tools</label> <br>
<input type="submit" value="Edit Tool" >
<a class="block" href="<?php echo url_for('/members/delete_tool.php?id=' . $id); ?>">Delete Tool</a>
</fieldset>
</form>
<div class="push"></div>
</div>
<?php include(SHARED_PATH . '/footer.php'); ?>
You're looping over your results. This means with every loop you're setting one variable to "checked" and the rest to an empty string. So only the last one will be checked. The band-aid fix is to set unchecked as the default outside of the loop, and then change to checked only when it's needed.
But the real fix is to be pulling this info from the database and working with it instead of manually mapping database IDs to labels. By moving your condition into the join, you pull all the categories. The rows that have a tool ID are checked, and the others are not. You're also pulling the category names and IDs so you can programmatically build your checkboxes.
See here for DB sample: http://sqlfiddle.com/#!9/20b223/14/0
$tool = find_tool_by_id($id);
$tool["categories"] = [];
$sql = "SELECT c.category_name, c.category_ID, tc.tool_id
FROM category c
LEFT JOIN tool_category tc ON c.category_ID = tc.category_id
AND tc.tool_id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("i", $_GET["id"]);
$result = $stmt->execute();
while($row = $stmt->fetch_assoc()) {
$id = $row["category_ID"];
$name = $row["category_name"];
$checked = $row["tool_id"] ? "checked" : "";
$tool["categories"][$id] = ["name" => $name, "checked" => $checked];
}
Now later on you can do this to automatically build all your checkbox inputs:
<?php foreach ($tool["categories"] as $id=>$category): ?>
<input type="checkbox" name="category_ID[]" id="category_<?=$id?>" value="<?=$id?>" <?=$category["checked"]?>>
<label for="category_<?=$id?>">
<?=htmlspecialchars($category["name"])?>
</label><br/>
<?php endforeach ?>

how to display related activities under services using database and php

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

php - combobox not indexed

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

Display saved checkbox value

I have some checkbox options that I save in the DB. I was able to view and also select multiple options and save them in the DB. The issue is that I want to display the saved information but I don't know how to do that.
<form action="save_comp.php" method="post">
<?php
//Display
include ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM competency ");
//$row = mysql_fetch_array($sql);
while($row = mysql_fetch_array($sql))
{
echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br />";
}
?>
<input name="submit" type="submit" value="submit" />
</form>
Save into DB
<?php
session_start();
$id = $_SESSION['user_id'];
//$id = 3;
include ('mysql_connect.php');
$insStr = '';
foreach($_POST['comp'] as $val){ $insStr .=$val.","; }
mysql_query("INSERT INTO competency_result (user_id,result) VALUES ( '$id', '$insStr' )") or die(mysql_error());
echo'<script>alert("Inserted Successfully")</script>';
?>
All I want to do now is to display the saved information in a table format. I tried doing this but it only showed me the saved ID
<?php
$res= mysql_query("SELECT * FROM competency_result WHERE user_id = '$user'")or die(mysql_error());
while($row = mysql_fetch_array($res))
{
echo"<tr>";
echo"<td> $row[result]</td>";
?>
<?php
echo"</tr>";
}
?>
<form action="save_comp.php" method="post">
<?php
//Display
include ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM competency ");
//$row = mysql_fetch_array($sql);
while($row = mysql_fetch_array($sql))
{
echo"<input type='checkbox' name='comp[". $row['id']. "]' value='". $row['competency'] ."' /> ".$row['competency']." <br />";
}
?>
<input name="submit" type="submit" value="submit" />
</form>
If you want to checkboxes check then you can try with below code:
<?php
$sql = mysql_query("SELECT name FROM competency ");
//$row = mysql_fetch_array($sql);
while($row = mysql_fetch_array($sql))
{
$focus=explode(",",$row['name']);
?>
<input type="checkbox" name="focus[]" value="Art" <?php if(in_array("Comp",$focus)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="focus[]" value="Mathematics" <?php if(in_array("Mathematics",$focus)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="focus[]" value="Dance" <?php if(in_array("Dance",$focus)) { ?> checked="checked" <?php } ?> >
<?php
}
?>

How to remain stick to a php page?

I am passing the level of the question through query string to the page. Next, I am prompting user to give the answer from the option. Now, if the answer is correct, score is incremented. Now the issue is that if the answer is wrong, I am not getting any thing in browser.
<?php
session_start();
if ( isset($_POST['submit']))
{
$qid = $_POST['qid'];
$answer = $_POST['answer'];
// $range= $_POST['range'] ;
$dbc = mysqli_connect('localhost','root','1234','islamic')
or die('unable to connect');
$query = "select * from question where qid = '$qid' ";
$result = mysqli_query($dbc,$query);
$row = mysqli_fetch_array($result);
if ( $answer == $row['answer'])
{
// echo 'Congrats, Your answer is correct.'.$_COOKIE['username'];
#$score = ++$_COOKIE['score'];
setcookie('score',$score);
}
#$page = ++$_COOKIE['page'];
if ( #$page == 4)
{
echo 'score is '.$_COOKIE['score'];
setcookie('score',0);
setcookie('page',0);
echo 'Go to Home ';
exit();
}
setcookie('page',$page);
}
if ( isset($_GET['level']))
{
$_SESSION['level'] = $_GET['level'];
}
$level = $_SESSION['level'];
$dbc = mysqli_connect('localhost','root','1234','islamic')
or die('unable to connect');
// $query = "Select * from question";
// $result = mysqli_query($dbc,$query);
// $num_rows = mysqli_num_rows($result);
$range = rand(0,6);
$query = "select * from question where level = '$level' limit $range,1";
$result = mysqli_query($dbc,$query);
while ( ($row = mysqli_fetch_array($result)) )
{
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h3> <?php echo $row['sawal']; ?></h3>
<form method = "POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="radio" name=" answer" value="A" ><?php echo $row['A']; ?><br>
<input type="radio" name=" answer" value="B" ><?php echo $row['B']; ?><br>
<input type="radio" name=" answer" value="C" ><?php echo $row['C']; ?><br>
<input type="radio" name=" answer" value="D" ><?php echo $row['D']; ?><br>
<input type="hidden" name = "qid" value="<?php echo $row['qid'] ?>">
<!-- <input type="hidden" name = "range" value="<?php $range ?>"> -->
<input type="submit" name="submit" value="ANSWER"/>
</form>
</body>
</html>
<?php
}
mysqli_close($dbc);
?>
You script doesn't display anything if the database query returns no results.
Get rid of the while and simply use $row = mysqli_fetch_array($result);

Categories