I am building a lightweight school management system, that takes in grades directly from the database, how do I update all values at the same time. Please help
<?php
$mydb->setQuery("SELECT * FROM tblbible");
loadresult();
function loadresult(){
global $mydb;
$cur = $mydb->loadResultList();
foreach ($cur as $result) {
echo '<tr>';
echo '<td> <input type="hidden" name="studentID" value="'. $result->studentID.'" /></td>';
?>
<?php
$rowz = mysql_query("SELECT * FROM tblstudent where studentID = '$result->studentID' ")or die(mysql_error());
$detailz = mysql_fetch_array($rowz);
echo '<td>'.$detailz['name'].' '.$detailz['initial'].' '.$detailz['lastname'].'</td>';
?>
<?php
echo '<td> <select name="studentID[]" class="form-control" required>
<option>Select Grade</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select></td>';
echo '<td> <select name="studentID[]" class="form-control" required>
<option>Select Grade</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select></td>';
echo '<td> <select name="studentID[]" class="form-control" required>
<option>Select Grade</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select></td>';
?>
<?php
echo '</tr>';
}
?>
then the insert function below:
<?php
function doInsert(){
if (isset($_POST['save'])){
if ($_POST['value1'] == "" OR $_POST['value2'] == "" OR $_POST['value3'] == "") {
$messageStats = false;
message("All fields are required!","alert alert-success alert-dismissable");
redirect('index.php?page=5');
}else{
$studentID = $_POST['studentID'];
for($i = 0; $i<10; $i++) {
foreach( ($studentID) as $key => $value ) :
$cla = new Bible();
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
$value3 = $_POST['value3'];
$cla->sing =$value1;
$cla->verses =$value2;
$cla->prayer =$value3;
$cla->update($studentID);
redirect('index.php?page=5');
message("Report generated successfully!", "info");
endforeach;
}
}
}
}
And I would love to add all those in a one click button.
Related
Here is my code to use Condorcet:
use CondorcetPHP\Condorcet\Condorcet;
use CondorcetPHP\Condorcet\Election;
use CondorcetPHP\Condorcet\Candidate;
use CondorcetPHP\Condorcet\CondorcetUtil;
use CondorcetPHP\Condorcet\Vote;
if (isset($_POST) && $_POST != "") {
Condorcet::setDefaultMethod('Schulze');
$election = new Election ();
print("<pre>");
print_r($_POST['item']);
// To get Candidates by Category via Doctrine orm
$category = $em->getRepository('Entities\Category')->findOneBy(['id' => 1]);
$candidates = $category->getCandidates();
$total_items = 0;
foreach ($candidates as $candidate) {
$candidate = $candidate->getCandidate();
$election->addCandidate(new Candidate($candidate));
++$total_items;
}
$votes = array();
foreach ($_POST['item'] as $item => $ranking) {
$vote_factor = 1 + $total_items - $ranking;
if (isset($votes[$vote_factor])) {
$votes[$vote_factor] = (array) $votes[$vote_factor];
$votes[$vote_factor][] = $item;
} else {
$votes[$vote_factor] = $item;
}
}
$result = $election->addVote(new Vote($votes));
print("<br />");
foreach ($election->getResult('Schulze') as $rank => $candidates) :
echo 'Rank ' . $rank . ': ';
echo implode(', ', $candidates);
echo '<br />';
endforeach;
print_r($result->getSimpleRanking()); // To be saved in db.
echo 'Schulze winner is : ' . $election->getWinner('Schulze')->getName() . '<br />';
}
?>
<html>
<body>
<form method="post">
Wingspan: <select name="item[Wingspan]" id="Wingspan">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br />
Scythe: <select name="item[Scythe]" id="Scythe">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br />
Spirit Island: <select name="item[Spirit Island]" id="Spirit Island">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br />
Everdell: <select name="item[Everdell]" id="Everdell">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
I want to add a blank select option, that my users if they don't want to have some candidates in voting just use the blank select option and don't be forced to select a ranking number from the drop down menu. How to omit blank options completely from listing and voting? I think I have to unset blank options in a foreach? If yes, how?
I have an HTML form that a user can use to edit a row in a mysql database, the PHP script queries the database and then displays the current data in form. I have this working fine for text fields with something like:
Correct Answer:<input type="text" name="answer" value="<?php echo $row['CorrectAnswer']; ?>"><br>
and simple radio set up like:
<?php
if ($row['Hazardous'] == "no"){
?>
Hazardous?:<input type="radio" name="hazardous" value="yes">Yes
<input type="radio" name="hazardous" value="no" checked="checked">No<br>
<?php
}else{
?>
Hazardous?:<input type="radio" name="hazardous" value="yes" checked="checked">Yes
<input type="radio" name="hazardous" value="no" >No<br>
<?php } ?>
I also have a select option element like below:
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
Which im trying to set up, I could use:
<?php if ($row['Category'] == "hazardawareness"){ ?>
Category: <select name="category">
<option value="hazardawareness" selected="selected">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "observation"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation" selected="selected">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "insurance"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance"selected="selected">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "attitude"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude" selected="selected">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "knowledge"){ ?>
Category: <select name="category">
<option value="hazardawareness" >Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge" selected="selected">Gen. Knowledge</option>
</select><br>
<?php } ?>
But perhaps there is a better method to do this without duplicating so much code?
You can add an if clause on your option creation that will at the selected attribute if needed.
<?php $options = array(
"Hazard Awareness" => hazardawareness,
"Observation" => observation,
"Insurance" => insurance,
"Attitude" => attitude
); ?>
<select name="category">
<?php foreach($options as $display => $value) { ?>
<option value='<?= $value ?>' <?php if($row['Category'] == trim($value)) { ?>selected='selected'<?php } ?>>
<?= $display ?>
</option>
<?php } ?>
</select>
I think this will help you to do the work using less code:
<?php
$options = array(
"Hazard Awareness" => hazardawareness,
"Observation" => observation,
"Insurance" => insurance,
"Attitude" => attitude
);
echo 'Category: <select name="category">';
foreach($options as $display => $value) {
if ($row['Category'] == trim($value)) {
echo '<option value="' . $value . '" selected>' . $display .'</option>';
}
else {
echo '<option value="' . $value . '">' . $display . '</option>';
}
}
echo '</select>';
Put your option data in array().
I'm currently using this garbage code to select the correct selected="selected" value for my HTML dropdown. There has got to be a better way... maybe with a switch statement or something else?
Any tips on how to make this more efficient?
if ($quantity == 0)
{
echo '
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
}
else if ($quantity == 1)
{
echo '
<option value="0">0</option>
<option selected="selected" value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
}
else if ($quantity == 2)
{
echo '
<option value="0">0</option>
<option value="1">1</option>
<option selected="selected" value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
}
else if ($quantity == 3)
{
echo '
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option selected="selected" value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
} etc....
This should work for you:
echo "<select>";
for($count = 0; $count < 10; $count++)
echo "<option " . ($quantity == $count ? 'selected': '') . " value='$count'>$count</option>";
echo "</select>";
You could loop through them and print each one's HTML code, like this:
for($i=0;$i<10;$i++){
$value=$i;
if($value==9) $value="9+";
if($i==$quantity){
echo '<option value="' . $value . '" selected="selected">' . $value . '</option>'
}
else{
echo '<option value="' . $value . '">' . $value . '</option>'
}
}
Using a loop is all you need :
$out='<option';
for($i=0;$i<10;i++){
if($quantity==$i)
$out.='selected="selected" ';
$out.=" value='$i'>$i</option";
echo $out;
}
I usually interpolate the 'selected' variable in to the loop
for($i=0;$i<10;$i++){
$selected = $i==$quantity? 'selected="selected"':'';
echo "<option value=\"$i\" $selected>$i</option>";
}
loop over it:
for($i = 0; $i < 9; $i++){
if($i == $quantity){
$selected = 'selected="selected"';
}else{
$selected = '';
}
$i2 = ($i == 9) ? '9+' : $i;
$select_options .= '<option value="'.$i2.'" '.$selected.'>'.$i2.'</option>';
}
echo $select_options;
Edit: looks like you can't set selected in the select its self, which is pretty stupid if you think about it. I've filed a complaint with the html5 standards people. its illogical to set the selected value of a select element in the individual options.
i'm working on a php script wherein i must add certain score value at each row. I was able to display all the rows but i'm not sure on how would I able to store each of the given score in a variable and what query should I make to add all of them.
Here's my code
<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect);
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score" id="score" size="1">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select></td></tr>';
echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
if(isset($_POST['submit'])) {
//not sure if all the scores will be stored in here.
$id = $_POST['id'];
$query = mysql_query('insert query here');
}
?>
</table>
</body>
</html>
any suggestions are appreciated. Thanks in advance.:D
I think you need the id of each changed row (maybe as a hidden field for each row. Then just do a loop through all received rows and UPDATE each one.
You might also want to change all of your form field names to use the array format. This way it's easier to make your PHP loop throught them.
Sample row:
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score['.$i["id"].']" id="score" size="1">
<option value="5">5</option>
<option value="5">10</option>
<option value="5">15</option>
<option value="5">20</option>
<option value="5">25</option>
<option value="5">30</option>
<option value="5">35</option>
<option value="5">40</option>
<option value="5">45</option>
<option value="5">50</option>
</select></td></tr>';
Now just loop through the $_POST["score"] array and use the appropriate ID for your update.
foreach($_POST["score"] as $id => $value{
// ESCAPE your db values!!!!!
// query stuff with $value and $id
}
Also keep in Mind
mysql is deprecated! Use mysqli
Escape anything from outside sources like $_POST before use in SQL
You just needs to make an array of your drop down box like below,
while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score[".$i['com_id']."]" id="score" size="1">
<option valyue="5">5</option>
<option valyue="5">10</option>
<option valyue="5">15</option>
<option valyue="5">20</option>
<option valyue="5">25</option>
<option valyue="5">30</option>
<option valyue="5">35</option>
<option valyue="5">40</option>
<option valyue="5">45</option>
<option valyue="5">50</option>
</select></td></tr>';
echo'<br>';
}
and you can access it for all of your comments
<option valyue="5">50</option>
should be
<option value="5">50</option>
To send the value of a comment to database you need to add a ID of the comment
you should loop something like this.
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
echo '<input type="hidden" name="id" value="'.$i['com_id'].'">';
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score" id="score" size="1">
<option value="5">5</option>
<option value="5">10</option>
<option value="5">15</option>
<option value="5">20</option>
<option value="5">25</option>
<option value="5">30</option>
<option value="5">35</option>
<option value="5">40</option>
<option value="5">45</option>
<option value="5">50</option>
</select></td></tr>';
echo'<br>';
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
}
I guess the easiest way for you is the following (a mix of the other solutions and comments):
<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$x = 0;
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect);
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
$x++;
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score_'.$x.'" id="score" size="1">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select></td></tr>';
echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
if(isset($_POST['submit'])) {
//not sure if all the scores will be stored in here.
$id = $_POST['id'];
for($y = 0;$y <= $x; $y++)
{
//This query is no sql injection save. Please add filters for productive uses!!
$query = mysql_query('UPDATE table_name SET score = '.$_POST["score_".$y].' WHERE id='.$id);
}
?>
</table>
</body>
</html>
Code is no tested!
Hey i am having some trouble getting an options[] array to work, if anyone can help that will be great
Form
<form method="post" action="array2.php">
<select name="options[]">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<input name="submit" type="submit" value="submit">
</form>
The array2.php
<?php
session_start();
if(isset($_POST['submit'])){
if($_POST['options[]'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
?>
Any help would be great, also what happens is even if it's an empty feild, the thing progress's to the rest of the script
rest of script
<?php
for($i=0; $i < count($checked); $i++){
echo "You have selected to recive " . $checked[$i] . " tickets<br/>";
}
for($i=0; $i < count($checked2); $i++){
echo "And you have selected to recive " . $checked2[$i] . " for accommodation are you sure? <br/>";
}
?>
Sorry I am unable to reply to people for now soon as I posted it a class came to the empty room so need to wait an hour :/
You need the change your options[] to option as it mean you are submitting multiple select with the same name
<form method="post" action="array2.php">
<select name="options">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<input name="submit" type="submit" value="submit">
</form>
in your array2.php file
<?php
session_start();
if(isset($_POST['submit'])){
if($_POST['options'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
?>
if you really need to send options[]
<?php
session_start();
if(isset($_POST['submit'])){
if(is_array($_POST['options']){
if($_POST['options'][0] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'][0];
$_SESSION['checked'] = $checked;
}
}else{
if($_POST['options'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
}
?>
you can clean this if else as you like
You can use this example code
<?php
if($_POST) {
if(isset($_POST['state'])) {
if($_POST['state'] == 'NULL') {
echo '<p>Please select an option from the select box.</p>';
}
else {
echo '<p>You have selected: <strong>', $_POST['state'], '</strong>.</p>';
}
}
}
?>
and your html code
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Please select a state</legend>
<select name="state">
<option value="NULL">-- Please select a state --</option>
<option value="AK">AK - Alaska</option>
<option value="AL">AL - Alabama</option>
<option value="WY">WY - Wyoming</option>
</select>
<input type="submit" name="submit">
</fieldset>
</form>
in your case options[] can be used for sending multiple fields as array, the index starts with 0, for example:
<input name="test[]"> : index 0
<input name="test[]"> : index 1
then, you can get these values in $_POST['test'] like this:
$input_one = $_POST['test'][0];
$input_one = $_POST['test'][1];
if you look at this inside $_POST it will be like this:
$_POST = array (
...,
'test' => array(0=> ..., 1 => ...)
)
for your form, if you only had one options[], then the value is the $_POST will be
if(isset($_POST['options'][0])){
}
So let's clean it up
<select name="options">
You do not need options[].
And then the result of $_POST['options'] will be the value of option. And add there <option value="0">Select</option> and then check whether the POSTed data is higher then 0
in html
<select name="options">
<option value="0"></option>
<option value="1">1</option>
.
.
in php
if(isset($_POST['options']) && !empty($_POST['options'])) {
$checked = $_POST['options'];
}