I've a following html table which name contain array. How can i check this array if it's value is empty ?
echo "<input type='radio' name='ch[$roll][$sname][$class]' value='1' /> ";
echo "<input type='radio' name='ch[$id][$sname][$class]' value='0' />";
Currently i'm checking it with following code, it's not working but i know the name is array and it's must be compare with any array function. Can you guys give me a idea ?
if(isset($_POST['ch']))
{
$ch = $_POST['ch'];
if(empty($ch))
echo "<div class='error>Select attendence field. </div>";
}
Regards.
Update: (Full Code)
$action = htmlspecialchars($_SERVER['PHP_SELF'])."?class=$class_from";
echo "<form method='post' action='$action' name='attendence'/>";
echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='200'><b>Student Name</b></td>";
echo "<td class='tdhead' valign='top' width='250'><b>Roll No</b>
</td>";
echo "<td class='tdhead' valign='top' width='250'><b>Class Name</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'><b>Present / Not present</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'>
Present All <input type= 'checkbox'
onclick='checkAll(this)'</td>";
echo "</tr>";
//start the counter variable
$counter = 1;
while($res2 = mysql_fetch_array($sql2))
{
$id = (int) $res2['id'];
$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);
echo "<tr>";
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";
echo "<td class='tdhead2' valign='top'>";
//echo each radio with the counter for this row
echo "<input type='radio' name='ch[$roll][$sname][$class]' value='1' /> ";
echo "<input type='radio' name='ch[$id][$sname][$class]' value='0' />";
echo "</td>";
echo "<td class='tdhead2' valign='top'> </td>";
echo "</tr>";
//add one to the counter
$counter++;
}
echo "<tr>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'><input type='submit' value='Record' name='Submit'
class='submit' /></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
if(isset($_POST['Submit']) && $_POST['Submit'] == "Record")
{
if(isset($_POST['ch']))
{
$ch = array_filter($_POST['ch']);
if (empty($ch))
{
echo "<div class='error>Select attendence field. </div>";
}
}
if(count($ch) == 0)
{
echo "<div class='error>Select attendence field. </div>";
}
else
{
foreach ($_POST['ch'] as $roll => $arr1)
{
$roll;
foreach ($arr1 as $name => $arr2)
{
$name;
foreach ($arr2 as $class => $value)
{
$class;
$value;
$sql = mysql_query("INSERT INTO e_attendence VALUES('', '$name', '$roll', '$class',
'$value', '$current_date')");
}
}
}
}
if($sql)
echo "<div class='success'>Succesfully recorded.
</div>";
}
empty() method work for finding whether the array is empty or not.
But for Your solution use below:-
if(isset($_POST['ch']) && $_POST['ch']=='1')
{
$ch = $_POST['ch'];
if(empty($ch))
echo "<div class='error>Select attendence field. </div>";
}
if You want to show a error message when no radio is selected then use below code:-
if(!isset($_POST['ch']) && $_POST['ch']=='')
{
echo "You have not selected any radio button";
}
use empty() instead of isset()
check the comparison table available at the below link
http://php.net/manual/en/types.comparisons.php
try this
print_r($_POST['ch']);
Try this
if(isset($_POST['ch'])){
$ch = array_filter($_POST['ch']);
if (empty($ch)) {
echo "<div class='error>Select attendence field. </div>";
}
}
Related
Is there any way to stop a button from submit button to stop looping in while loop?
it just looks terrible to have like 15 submit buttons insted of 1. whitout moving it from the from
$sql = "SELECT * FROM `bestalning` WHERE lev=0
ORDER BY tid";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
echo "<div class='continer bg-vit table-rsponsive-sm '>";
echo "<table class='table'>";
echo "<thead class='thead-dark'>";
echo "<tr><th>Lev</th>";
echo "<th> </th>";
echo "<th>Artikelnr</th>";
echo "<th>Antal</th>";
echo "<th>Singnatur</th>";
echo "<th>Önskad ankomst</th>";
echo "<th>Skickad</th>";
echo "</tr></thead><tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form method='POST' action='ID_change.php'>";
echo "<td><input style='margin-right:5px;' name='levJA' type='checkbox' value='1'></td>";
echo "<td><input style='margin-right:5px;' name='RowID' type='hidden' value='".$row['ID']."'></td>";
echo "<td>".$row['artikelnr']."</td>";
echo "<td>".$row['antal']."</td>";
echo "<td>".$row['ovrigt']."</td>";
echo "<td>".$row['Tid']."</td>";
echo "<td>".$row['date']."</td>";
echo "<input class='btn btn-dark m-1' type='submit' value='submit'>;
echo "</form>";
echo "</tr>";
}
echo "</tbody></table>";
echo "</div>";
mysqli_close($conn);
I use two PHP files, one a_ndryshoje_produktin.php for posting data to be updated and one another produkti_ndryshuar.php that executes the posted data for update.
The problem for my question is that the file produkti_ndryshuar.php does execute without errors but there isn't any update of only the produkti_id with the produkti_i_ri_id on MySQL produktet table.
Below is my code for both files.
Thank you
echo "<form action='produkti_ndryshuar.php' method='post'>";
echo "<table class='tbl_manevro'>";
echo "<tr><td class='titujt'>";
echo "ID";
echo "</td></tr>";
echo "<tr><td>";
echo "<input type='text' name='produkti_i_ri_id' maxlength='60' value='".$row['produkti_id']."' />";
echo "</td></tr>";
echo "<tr><td class='titujt'>";
echo "Kategoria";
echo "</td></tr>";
echo "<tr><td>";
echo "<input type='text' name='kategori_id' maxlength='60' value='".$row['kategori_id']."' />";
echo "</td></tr>";
echo "<tr><td class='titujt'>";
echo "Emri";
echo "</td></tr>";
echo "<tr><td>";
echo "<input type='text' name='produkti_emri' maxlength='60' value='".$row['produkti_emri']."' />";
echo "</td></tr>";
echo "<tr><td class='titujt'>";
echo "Çmimi";
echo "</td></tr>";
echo "<tr><td>";
echo "<input type='text' name='produkti_cmimi' maxlength='60' value='".$row['produkti_cmimi']."' />";
echo "</td></tr>";
echo "<tr><td class='titujt'>";
echo "Fotografia";
echo "</td></tr>";
echo "<tr><td>";
echo "<input type='text' name='produkti_fotografia' maxlength='60' value='".$row['produkti_fotografia']."' />";
echo "</td></tr>";
echo "<input type='hidden' name='produkti_id' value=".$row['produkti_id']." />";
echo "<tr><td>";
echo "<input class='shtoje_btn' type='submit' value='Ndryshoje produktin' />";
echo "</td></tr>";
echo "</table>";
echo "</form>";
and the other file produkti_ndryshuar.php :
$produkti_i_ri_id=strip_tags($_POST['produkti_i_ri_id']);
$kategori_id=strip_tags($_POST['kategori_id']);
$produkti_emri=strip_tags($_POST['produkti_emri']);
$produkti_cmimi=strip_tags($_POST['produkti_cmimi']);
$produkti_fotografia=strip_tags($_POST['produkti_fotografia']);
$produkti_id=strip_tags($_POST['produkti_id']);
if($produkti_i_ri_id && $kategori_id && $produkti_emri && $produkti_cmimi && $produkti_fotografia ){
$db=include('dbconnect.php');
$query_2=$db->prepare("update produktet set
produkti_id=:produkti_i_ri_id,
kategori_id=:kategori_id,
produkti_emri=:produkti_emri,
produkti_cmimi=:produkti_cmimi,
produkti_fotografia=:produkti_fotografia
where produkti_id=:produkti_id");
$query_2->execute(array(':produkti_i_ri_id'=>$produkti_i_ri_id,
':kategori_id'=>$kategori_id,
':produkti_emri'=>$produkti_emri,
':produkti_cmimi'=>$produkti_cmimi,
':produkti_fotografia'=>$produkti_fotografia,
':produkti_id'=>$produkti_id));
echo "<span class='lajmi_sakte'>Produkti i caktuar u ndryshua me sukses.</span>";
}
You are giving values to your if statement, not booleans.
Try isset() :
$produkti_i_ri_id=strip_tags($_POST['produkti_i_ri_id']);
$kategori_id=strip_tags($_POST['kategori_id']);
$produkti_emri=strip_tags($_POST['produkti_emri']);
$produkti_cmimi=strip_tags($_POST['produkti_cmimi']);
$produkti_fotografia=strip_tags($_POST['produkti_fotografia']);
$produkti_id=strip_tags($_POST['produkti_id']);
if(isset($produkti_i_ri_id) && isset($kategori_id) && isset($produkti_emri) && isset($produkti_cmimi) && isset($produkti_fotografia) ){
$db=include('dbconnect.php');
$query_2=$db->prepare("update produktet set
produkti_id=:produkti_i_ri_id,
kategori_id=:kategori_id,
produkti_emri=:produkti_emri,
produkti_cmimi=:produkti_cmimi,
produkti_fotografia=:produkti_fotografia
where produkti_id=:produkti_id");
$query_2->execute(array(':produkti_i_ri_id'=>$produkti_i_ri_id,
':kategori_id'=>$kategori_id,
':produkti_emri'=>$produkti_emri,
':produkti_cmimi'=>$produkti_cmimi,
':produkti_fotografia'=>$produkti_fotografia,
':produkti_id'=>$produkti_id));
echo "<span class='lajmi_sakte'>Produkti i caktuar u ndryshua me sukses.</span>";
}
else{
echo "<span class='lajmi_mink'>Ju lutemi plotësoni të gjitha fushat!!!</span>";
}
And why are you using strip_tags() function?
I am creating a random generated quiz with 10 questions. In generating the random question is fine, but I want to display also the choices in random, same with the questions.
This is my code that I am currently working with:
<?php
generate();
function generate(){
include('connection.php');
mysql_select_db('exam');
$result=mysql_query("SELECT * FROM questionaires
INNER JOIN choices ON questionaires.q_id=choices.q_id
WHERE RAND()<(SELECT ((10/COUNT(*))*10) FROM questionaires)
ORDER BY RAND() LIMIT 10");
$c=0;
echo "<table border='3' align='center' bordercolor='#CCCCCC'>
<tr>
<th>Number:</th>
<th>Question</th>
</tr>
";
while($row = mysql_fetch_array($result)){
$c++;
echo "<tr>";
echo "<td>" . $c . "</td>";
echo "<td>";
echo $row['question'] . "<br>";
echo "<input type='radio' name='ans'>".$row['choice_a']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_b']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_c']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_d']."</input><br>";
echo "</td>";
echo "</tr>";
//}
//}
}
echo "</table>";
}
?>
Little help will highly appreciated.
You could change script to this:
echo "<td>";
echo $row['question'] . "<br>";
$ans=array($row['choice_a'],$row['choice_b'],$row['choice_c'],$row['choice_d']);
shuffle($ans);
foreach ($ans as $choice) {
echo "<input type='radio' name='ans'>".$choice."</input><br>";
} unset($choice);
echo "</td>";
I'm getting all rows from mysql database. Now I want to highlight only first row in php while loop with class name keywordHighlight.
How do I highlight only first row in php while loop result ?
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($resGetSearch = mysql_fetch_array($getSearch)){
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
echo "<tr onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Do it like this
$i = 1;
while($resGetSearch = mysql_fetch_array($getSearch)){
$highlight = $i == 1 ? 'keywordHighlight' : '';
echo "<tr class='{$highlight}' onclick='getDetails($SearchCdid);' >";
---------------
-------------
-------------
$i++;
}
Only with CSS
or you can highlight it only with css
#highlight tbody tr:nth-child(1){
background: #ff6600;
}
There is more and elegant way to highlight only first row with only css not need to code, consider the example http://jsbin.com/soravuzakahu/1/
You could just add a boolean outside the loop. Like so:
$first = true;
while($resGetSearch = mysql_fetch_array($getSearch)){
if(first == true){
// Add code here that only applies to the first iteration.
}
$first = false;
}
<?php
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$i = 0;
while($resGetSearch = mysql_fetch_array($getSearch)){
++$i;
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
if($i == 1)
echo "<tr onclick='getDetails($SearchCdid);' >";
else
echo "<tr class='keywordHighlight' onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Use a flag and set its value to true and while in loop check its value , if its true then print class name and set its value to false. Like $flag=true; then inside while loop check
if($flag==true) {
<td class='yourclassname';
$flag= false;
}
put some flag $f=0;
if f==0 then do something like this:
$highlight="<div class='keywordHighlight'>valur of first row</div>";// instead of dive you can use table also it depends on how you want to display.
$f=$f+1;
and rest in else part.
$first = true;
while ( $resGetSearch = mysql_fetch_array($getSearch) )
{
if ( $first == true )
{
// Add code here that only applies to the first iteration.
$first = false;
}
else
{
// Add code here that only applies to the 2, 3, and so on.
}
}
Well, I've following Php code which generate a table with radio button so that user can select only one radio button in EVERY row. But to select only one radio button in Every row it's must be a unique name in EVERY row, Right ? But I can't get any idea how do i set a unique name in while loop. Can you guys give me any idea or solutions ?
Php code:
$action = htmlspecialchars($_SERVER['PHP_SELF'])."?class=$class_from";
echo "<form method='post' action='$action' name='attendence'/>";
echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='200'><b>Student Name</b></td>";
echo "<td class='tdhead' valign='top' width='250'><b>Roll No</b>
</td>";
echo "<td class='tdhead' valign='top' width='250'><b>Class Name</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'><b>Present / Not present</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'>Check All <input type= 'checkbox'
onclick='checkAll(this)' /></td>";
echo "</tr>";
while($res2 = mysql_fetch_array($sql2))
{
$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);
echo "<tr>";
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";
echo "<td class='tdhead2' valign='top'>
<input type='radio' name='ch[]' value='1' /> <input type='radio' name='ch[]'
value='1' />
</td>";
echo "<td class='tdhead2' valign='top'> </td>";
echo "</tr>";
}
echo "<tr>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'><input type='submit' value='Record' name='Submit'
class='submit' /></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
Thanks for your help.
Update:
echo "<td class='tdhead2' valign='top'>";
echo '<input type="radio" name="ch'.$counter++.'[]" value="1">';
echo " ";
echo '<input type="radio" name="ch'.$counter.'[]" value="0">';
echo "</td>";
you can set a variable counter:
$counter = 1;
while($res2 = mysql_fetch_array($sql2)){
echo '<input type="radio" name="ch'.$counter++.'[]" value="'.$counter++.'">';
}
Instead of making the name just ch[] you can make the name ch[SOME_UNIQUE_VALUE] for each row. Usually something like an id number is used. Perhaps $roll if that is unique to each student. That will also allow you to know which id the radio button pertains to. On form submit, you can foreach($_POST['ch'] as $id=>$value) and get the id out.
Edit:
Example code:
//start the counter variable
$counter = 1;
while($res2 = mysql_fetch_array($sql2))
{
$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);
echo "<tr>";
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";
echo "<td class='tdhead2' valign='top'>";
//echo each radio with the counter for this row
echo "<input type='radio' name='ch_{$counter}[]' value='0' /> ";
echo "<input type='radio' name='ch_{$counter}[]' value='1' />";
echo "</td>";
echo "<td class='tdhead2' valign='top'> </td>";
echo "</tr>";
//add one to the counter
$counter++;
}
Please note, unless you do like I suggested above, you will not be able to know which student each radio group goes to. You can assume that the radio buttons on group row 1 belongs to the student in row 1, but you might not know which student is in row 1. Which is why I suggest you put a unique id that goes with each student with each radio button instead of a counter.
You can store a counter $count and increase it after each iteration. Or if you have some unique id from the database per row (for example the primary_key), you can use that instead.$id = $res['id']
Then concat that value to the name=ch".$id."...
you should use student name as an unique array and put that value in the name=""
and it will auto increment according to the name and you can only choose single option at single row.
try this :
<input type='radio' name='<?php echo $res2['sname'];?>' value='0' /> ";
<input type='radio' name='<?php echo $res2['sname'];?>' value='1' />";
this solutio will work.because it work's for me in an attendance system.