Echo a mysql field into a checkbox using an if Statement - php

Hello I think I am really close but I just cant seem to get my statement working correctly
I have a overtime field from Mysql and I can display it but I am trying to make it either checked or not checked depending on if it is a 1 for checked or 0 for not checked. I can show the 1 or the 0 onto a Text field but the checkbox just won't work for me.
echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]" <? if($list_hours["overtime"]==1){ echo "checked=checked"} ?> >';
My complete code:
<?
// This next section is to load the text fields from the mysql
// this is to collect all of the logged hours on the job sheet and then display
// the hours already entered.
$hours_query = "SELECT * FROM hours where job_number = '$job_number'";
$hours_result= ($mysqli-> query($hours_query));
// this next section collects the data from the parts table and then adds it to an array
// this then goes into a while statement until it has displayed all of the results it has
// depending on how many parts were used
while ($list_hours = mysqli_fetch_array($hours_result))
{
echo " <table border ='0' table width = 70%>";
echo '<td> <input type="hidden" name="chg_hrs_id[]" value="'.$list_hours['unique_id'].'">';
echo '<td> Start Time <input type="time" name="chg_hrs_str[]" value="'.$list_hours['start_time'].'" >';
echo '<td> Finish Time <input type="time" name="chg_hrs_fin[]" value="'.$list_hours['finish_time'].'">';
echo '<td> Date Of Work <input type="date" name="chg_hrs_date[]" value="'.$list_hours['date'].'">';
echo '<td> Ovt Boolean <input type="ovt_Booloean" name="chg_hrs_ovtb[]" value="'.$list_hours['overtime'].'">';
echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]" <? if($list_hours["overtime"]==1){ echo "checked=checked"} ?> >';
echo "</tr>";
}
echo "</table>";
?>

I think you have some Syntax issues. Try the following:
echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]"' . ($list_hours["overtime"]==1 ? 'checked="checked"' : '') . '>';

Thanks to Marc I had got the syntax incorrect I have used Marc's syntax and it has worked straight away. I now have to see how the statement is put together.
It seems like the dots is a link to connect statements together and it doesn't seem to actually need a if statement to be setup.

Related

Update selected rows with value in table by using a checkbox for each row

I have tried searched, but I am missing somthing or not phrasing myself correctly I guess.
php, sql and Codeigniter.
What I have is a list of reports in a database.
Theese have a column that states if they are "active" 1 or 0.
What I want to do is add a checkbox on the list that prints the rows in my view as that I can select and then when I have selected the rows I want to be able to press a button and by sending form data change the selected rows in the database to a 1 or 0.
example of how I am thinking of presenting it:
echo "<form action='' method='POST'><table>";
foreach($reports->result() as $row)
{
echo "<tr>";
echo "<td><input type="checkbox" name=""></td>;
echo "<td>".$row->name."</td>";
echo "<td>".$row->time."</td>";
echo "<td>".$row->spot."</td>";
echo "<td>".$row->priority."</td>";
echo "</tr>";
}
echo "<tr><td colspan='5'><input type='submit'></td></tr>";
echo "</table></form>";
What would be the best is if I could get the checkboxes I select into a array for example that would be lovely.
I have tried with array_push, but I could not find a way to select only the ones I selected to send. Is there some way to do this that I dont know of?
I have found a way to make the selected checkboxes into an array by adding id[] to all checkboxes name like this this:
form_checkbox('id[]',$row->id, FALSE);
I loop out alot of these and might get this for example:
<input type="checkbox" name="id[]" value="12340">
<input type="checkbox" name="id[]" value="12353">
<input type="checkbox" name="id[]" value="12367">
<input type="checkbox" name="id[]" value="12389">
<input type="checkbox" name="id[]" value="12391">
Then in my controller I added this where my form takes action to and loops through the ones that I have checked and take action on them.
$arr = array();
if(!empty($_POST['id'])) {
foreach($_POST['id'] as $selected) {
$this->db->where('id', $selected);
$this->db->update('rapporter' , $data);
}
}else{}
This is working great for me. Now I only update the rows I have checked!
If someone have a more efficient way of doing this I am all ears!

Radio Buttons / Checkboxes

I wasn't sure how to word the title, because just thinking about this concept kind of confuses me and I'm not sure why. I'm sure there is some simple solution to this.
Here is a picture of my codes (Attached below) output.
The user should be able to click 1 radiobox inside each box, but I can't seem to make it do that. The way it is right now, only 1 vacation button can be selected and only 1 absent button can be selected.
Here is my code:
if($loop < 4) {
echo '<td>';
echo ' NAME<BR><input type="radio" name="vacationTeammates[]" value="'.$value[0].'">Vacation<br>
<input type="radio" name="absentTeammates[]" value="'.$value[0].'">Absent';
echo '</td>';
$loop++;
} else {
$loop = 1;
echo "</tr><tr align='center'>";
echo '<td>';
echo ' NAME<BR><input type="radio" name="vacationTeammates[]" value="'.$value[0].'">Vacation<br>
<input type="radio" name="absentTeammates[]" value="'.$value[0].'">Absent';
echo '</td>';
}
(The loop makes it so that the table row ends and starts a new one, so it is somewhat irrelevant but the radio buttons are what matters)
Each radio button pair must have the same "name" attribute value for browsers to know they're their own set.
Change your code to something like this, where "$SomeUniqueRowVar" is a variable that changes for each row:
echo 'NAME<BR>';
echo '<input type="radio" name="status'.$SomeUniqueRowVar.'" value="vacation">';
echo 'Vacation<br>';
echo '<input type="radio" name="status'.$SomeUniqueRowVar.'" value="absent">';
echo 'Absent';
I would add a counter and assign each name to a specific array element, rather than just []. Since you only want one of the radios to be selectable for each teammate, the inputs will have to be the same name but have different values. Put the teammates name in an array (a variable separate from the radio inputs) that uses the counter as it's index. This way you know which response goes to which teammate.
if($loop < 4) {
$name[$counter] = $value[0];
echo '<td>';
echo ' NAME<BR><input type="radio" name="teammates[$counter]" value=1>Vacation<br>
<input type="radio" name="teammates[$counter]" value=0>Absent';
echo '</td>';
$counter++;
$loop++;
} else {
$name[$counter] = $value[0];
$loop = 1;
echo "</tr><tr align='center'>";
echo '<td>';
echo ' NAME<BR><input type="radio" name="teammates[$counter]" value=1>Vacation<br>
<input type="radio" name="teammates[$counter]" value=0>Absent';
echo '</td>';
$counter++;
}
Now when you process the array, you will know if teammates[$counter] is 1 it means vacation and 0 it means absent.
You could also opt to use the name AS the index if you can be sure it is free of special characters/safely parse special chars out.
Thank you #user3163495 & #FirstOne for a few tips I used in my answer here.
I made both of the radio buttons in each cell have the same name, and all the others have different names.
if($loop < 4) {
echo '<td>';
echo ' <input type="radio" name="goneTeammates['.$uniqueLoop.']" value="'.$value[0].'|V">Vacation<br>
<input type="radio" name="goneTeammates['.$uniqueLoop.']" value="'.$value[0].'|A">Absent<br>NAME';
echo '</td>';
$loop++;
} else {
$loop = 1;
echo "</tr><tr align='center'>";
echo '<td>';
echo ' <input type="radio" name="goneTeammates['.$uniqueLoop.']" value="'.$value[0].'|V">Vacation<br>
<input type="radio" name="goneTeammates['.$uniqueLoop.']" value="'.$value[0].'|A">Absent<br>NAME';
echo '</td>';
}
For the value, I used the teammates name (Actually their uniqueid) and an A for absent or V for vacation. The output would basically look like:
35|A
23|V
64|A
etc
On the processing side I explode()'d the input at that | sign, to basically get the uniqueID and the letter in seperate variables, and processed like normal from there.

Passing a value retrieved from a database to another page using php and mysql

I am trying to pass a value that I have received from my database to another php page to use within another SQL statement.
I have tried using sessions and also passing using the $_POST method on the other page but have had no luck.
Here is a snippet of my code looping through to display all records:
while($row = mysqli_fetch_array($sql)){
echo '<td>
<img src='.'"'.$row['image'].'"'.'><br/>
<form method="post" action="edit-record.php">
<input type="text" name="imgID" value='.'"'.$row['id'].'"'.'>
<input type="submit" value="Edit" id="edit_btn" class="admin_btn"></form>
</td>';
}
The value that I need is the ID for each specific image - $row['id'].
When the user clicks the EDIT button, they should be redirected to another page which displays only the specific record. This is why I need the ID received passed to the next page to insert into a query statement.
I hope this made sense and any help will be greatly appreciated.
UPDATE: Thanks for all of your help. I solved the problem by playing around with a few of your suggestions to pass the id via GET in the action of the form.
<form method="post" action="edit-record.php?id='. $row['id'].'">
No idea why that hadn't occurred to me! Thanks again.
while($row = mysqli_fetch_array($sql)){
echo '<td>
<img src="'.$row['image'].'"><br/>
<form method="post" action="edit-record.php">
<input type="text" name="imgID" value="'.$row['id'].'">
<input type="submit" value="Edit" id="edit_btn" class="admin_btn">
</form>
</td>';
}
in edit-record.php...
<?php
echo $_POST['imgID'];
?>
There is no reason your code technically wouldn't work but instead you could just eliminate the form and use a simple link...
while($row = mysqli_fetch_array($sql)){
echo '<td>
<img src="'.$row['image'].'"><br/>
edit
</td>';
}
and in edit-record.php...
<?php
echo $_GET['id'];
?>
4 Ways to do this...
1) Use a cookie
2) Use a session (which by default uses a cookie but in a different way)
3) Use cURL
4) add it to the GET parameters... ie. somepage.com/page.php?id=1
Strange concatenation
<input type="text" name="imgID" value="'.$row['id'].'">
Sure you select id on the mysql query???
If you make
echo $_POST['imgID'];
what is the result???
You can pass the id via get in the action form:
<form method="post" action="edit-record.php?id='. $row['id'].' ">
On the other page you recive the form in $_POST and the id in $_GET['id']
~Aha, I think the problem is your quotes. Single quotes don't allow variables to be interpreted.~
Nevermind, thats not your problem, but I already wrote it out, so I'll leave it. Look how much cleaner those quotes are :)
Switch up your quotes like so:
echo "<td>
<img src='{$row['image']}'><br/>
<form method='post' action='edit-record.php'>
<input type='text' name='imgID' value='{$row['id']'}'>
<input type='submit' value='Edit' id='edit_btn' class='admin_btn'></form>
</td>";
Need curly braces around array element (e.g {$row['id']})

Quiz FORM POST with radio checkboxes issue

I have a quiz test page where I want to test the user for knowledge.
I did it so it works, but it has a flaw.
This is my code for populating the quiz with questions and (possible) answers from a mysql database:
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="post" id="quiz">';
$sql=mysql_query("SELECT idtest,no_q,title_q,q,idteste_dtl,corect_a from teste_dtl where idtest=".$idtest." order by no_q");
$num = mysql_num_rows($sql);
$i=0;
while ($row=mysql_fetch_row($sql)) {
echo '<h3 style="color:#e74c3c">'.$row[2].'</h3>';
echo '<p data-toggle="tooltip"><strong>'.$row[3].'</strong></p>';
$ssql=mysql_query("select letter,answer from tests_answers where idtest=".$idtest." and idq=".$row[4]." order by letter");
while ($rrow=mysql_fetch_row($ssql)) {
$Label = 'question-'.$row[1].'-answers-'.$rrow[0];
echo '<div>';
echo '<label class="radio">';
echo '<input type="radio" data-toggle="radio" name="answers['.$row[1].']" id="'.$Label.'" value="'.$rrow[0].'" unchecked>';
echo $rrow[0].') '.$rrow[1];
echo '</label>';
echo '</div>';
}
echo ' <br>';
echo '<input type="hidden" name="question['.$row[1].']" value="'.$row[2].'">';
echo '<input type="hidden" name="corect_a['.$row[1].']" value="'.$row[5].'">';
echo '<input type="hidden" name="idemp" value="'.$idemp.'">';
echo '<input type="hidden" name="idtest" value="'.$idtest.'">';
echo '<input type="hidden" name="idfirm" value="'.$idfirm.'">';
echo '<input type="hidden" name="namefirm" value="'.$namefirm.'">';
echo '<input type="hidden" name="totalq" value="'.$num.'">';
}
echo' <input type="submit" class="btn btn-hg btn-primary" value="Check answers" />';
echo '</form>';
So basically I generate a pair of text (question) + radioboxes (possible answers) from 2 tables. Table 1 is called "teste_dtl" and Table 2 is called "tests_answers".
All works perfectly except there is a bug. If the user does not check a radio box in a question... my code seems to think that the first radio box was checked.
So I have something like:
Question ONE
Body of question one... some text... and so on
A) first answer
B) second answer
C) third answer
D) fourth answer
Question TWO
Body of question two... some text... and so on
A) first answer
B) second answer
C) third answer
D) fourth answer
...
So the A,B,C,D... are checkboxes (input type radio). They are all unchecked from the start.
When I POST the results... apparently the questions that were not answered (no radio was checked) are considered as "answered with answer A)"
How can I avoid this?
Thank you
Radio buttons are supposed to always have a value.
The HTML specification says: "If no radio button in a set sharing the same control name is initially 'on', user agent behavior for choosing which control is initially 'on' is undefined." This means, practically, that depending on the browser and/or the Javascript library used to submit the form (if you're using AJAX), you can get unpredictable results.
You should force one of the buttons to be selected either by inserting checked="checked" like this:
$first_answer = true; // Set a flag for the first answer
while ($rrow=mysql_fetch_row($ssql)) {
$Label = 'question-'.$row[1].'-answers-'.$rrow[0];
echo '<div>';
echo '<label class="radio">';
// Output the beginning of the INPUT tag
echo '<input type="radio" data-toggle="radio" name="answers['.$row[1].']" id="'.$Label.'" value="'.$rrow[0].'"';
if ( $first_answer ) { // When the flag is true...
echo ' checked="checked"'; // ...add the 'checked' parameter...
$first_answer = false; // ...and set the flag to false for the next round
}
echo '/>'; // Close the INPUT tag
echo $rrow[0].') '.$rrow[1];
echo '</label>';
echo '</div>';
}
Another option is to add some Javascript to check that the user has selected an answer.

PHP List of records from filemaker select button

First, thank you for reading this.
I am just starting php and I am tying to make a site using FileMaker to display and enter information.
I have the php connecting to my database, then a search page using a form, then it displays a list of records. I would like to make a "button" that will select one record then display related records.
This is where my trouble is. I do not know how to make a form that will save either the record_Id or key field to then display the next page.
I am using a foreach loop to display the list in a table:
$records = $result->getRecords();
echo '<table border="1">';
echo '<tr>';
echo '<th>Company</th>';
echo '<th>Id Num</th>';
echo '<th>Choose</th>';
echo '</tr>';
foreach ($records as $record) {
echo '<tr>';
echo '<td>'.$record->getField('Company').'</td>';
echo '<td>'.$record->getField('K_Medical').'</td>';
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';
<input type="submit" />
</form>';
echo '</form></td>';
echo '</tr>';
}
echo '</table>';
As you can see I have tried to use a hidden form field to get the key field of the record, but the page dose not work. I get an error 500 when I try to view it in a browser.
Any help would be greatly appreciated! If I have not provided enough information please let me know.
Replace :
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';
<input type="submit" />
</form>';
By :
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='.$record->getField('K_Medical').'/>
<input type="submit" />
</form>';
You have a quotes and concatenation errors.

Categories