post in one specfic array while loop - php

I want to post value in one row while doing loop in specific array by id, but when click on bottom then will post it in "response" row in all arrays of table!!
<?php
require_once('config.php');
$con->set_charset('utf8');
$query = "select * from contact order by id";
$result = mysqli_query($con,$query);
while($name = mysqli_fetch_array($result)){
echo '<table width="200" border="1" align="right">';
echo "<tr>";
echo "<td align='center'> <div align='center'> <b> $name[id] </b> </td>";
echo "<td align='center'> <div align='center'> <b> The number </b> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <div align='center'> <b> $name[ticketnumber] </b> </td>";
echo "<td align='center'> <div align='center'> <b> ticket number </b> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <b> $name[name] </b> </td>";
echo "<td align='center'> <b> name </b> </div> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <b> $name[phone] </b> </td>";
echo "<td align='center'> <b> phone </b> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <b> $name[email] </b> </td>";
echo "<td align='center'> <b> email </b> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <b> $name[subject] </b> </td>";
echo "<td align='center'> <b> subject </b> </td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'> <b> $name[response] </b> </td>";
echo "<td align='center'> <b> response </b> </td>";
echo "</tr>";
echo "<tr>";
echo ' <td align="center"> <form method="POST" action="showcontact.php">
<input type="text" name="response" height="50pt"/> <br/>
<input type="submit" value="send" name="send"> </form> </td>';
echo "<td align='center'> <b> answer </b> </td>";
echo "</tr>";
if(isset($_POST['response'])) {
$response = $_POST['response'];
$sql = ("UPDATE contact SET response = '$response' WHERE id= $name[id]");
$rst = mysqli_query($con,$sql);
if($rst){
echo "<td align='center'> <b> sent </b> </td>" ;
echo " <td align='center'> </td>";
} else {
echo "<td align='center'> <b> did not send </b> </td>";
echo " <td align='center'> </td>";
echo "</table>";
}
}
}
?>

Having everything in a loop makes it hard to read your code.
You need a contact id to attach a response to.
Here's a brief overview of how I'd do it:
loop through contacts
row begins
display contact info
display response form
row ends
end loop
response form:
give it a post method
add a hidden contact id field <--
response input (text)
submit button
Form processing logic:
if this is a post request, and we have an id and response:
do your SQL update
I'd place the form processing out the loop at the top of your script, or on another page.

Related

Notice: Undefined index:

I have a problem with my code.
if I use select * from $table it's all good but
now with select distinct it shows me
Notice: Undefined index: L_code in /home/lab_users/web_tue25/web_pages/2014-12-09/stats03-04e.php on line 70
and it show the same for all the variables A/A etc.
Can anyone help me find my mistake(s)???
Thank you
<?php
include_once "dbconnect.php";
$sql = "SELECT DISTINCT akexam = '2003-04.e' FROM $table ";
$result = $dblink->query($sql);
if ($result->num_rows !== 0)
{
echo '<table border="1" style="width:100%; border: 1px solid black; border-collapse:collapse; text-align: center;">
<tr bgcolor= #787878>
<th>A/A</th>
<th>ak.εξαμ.</th>
<th>L_code</th>
<th>τ.ε</th>
<th>ck</th>
<th>rb</th>
<th>L_descr</th>
<th>ΔΗΛ</th>
<th>ΣΥΜ</th>
<th>a</th>
<th>b</th>
<th>c</th>
<th>d</th>
<th>e</th>
<th>f</th>
<th>ΣΥΝ</th>
<th>ok</th>
<th>ok%</th>
</tr>';
while($row = $result->fetch_assoc())
{
echo '<tr bgcolor= purple>';
if(strstr($row["L_code"],"241"))
{
echo "<td style= text-align:left>".$row["A/A"]."</td>";
echo "<td style= text-align:left>".$row["akexam"]."</td>";
echo "<td style= text-align:left>".$row["L_code"]."</td>";
echo "<td>".$row["te"]."</td>";
echo "<td> <input type='checkbox' name= 'ch' value='1'></td>";
echo "<td> <input type='radio' name='labs' value='1'></td>";
echo "<td>".$row["L_descr"]."</td>";
echo "<td style= text-align:right>".$row["dhl"]."</td>";
echo "<td style= text-align:right>".$row["sym"]."</td>";
echo "<td style= text-align:right>".$row["a"]."</td>";
echo "<td style= text-align:right>".$row["b"]."</td>";
echo "<td style= color:blue>".$row["c"]."</td>";
echo "<td style= color:blue>".$row["d"]."</td>";
echo "<td style= color:blue>".$row["e"]."</td>";
echo "<td style= color:blue>".$row["f"]."</td>";
echo "<td style= text-align:right>".$row["syn"]."</td>";
echo "<td style= text-align:right>".$row["ok"]."</td>";
echo "<td style= text-align:right>".$row["ok%"].'%';"</td>";
}
else
{
echo '<tr bgcolor= baby blue>';
echo "<td style= text-align:left>".$row["A/A"]."</td>";
echo "<td style= text-align:left>".$row["akexam"]."</td>";
echo "<td style= text-align:left>".$row["L_code"]."</td>";
echo "<td>".$row["te"]."</td>";
echo "<td> <input type='checkbox' name= 'ch' value='1'></td>";
echo "<td> <input type='radio' name='theories' value='1'></td>";
echo "<td>".$row["L_descr"]."</td>";
echo "<td style= text-align:right>".$row["dhl"]."</td>";
echo "<td style= text-align:right>".$row["sym"]."</td>";
echo "<td style= text-align:right>".$row["a"]."</td>";
echo "<td style= text-align:right>".$row["b"]."</td>";
echo "<td style= color:blue>".$row["c"]."</td>";
echo "<td style= color:blue>".$row["d"]."</td>";
echo "<td style= color:blue>".$row["e"]."</td>";
echo "<td style= color:blue>".$row["f"]."</td>";
echo "<td style= text-align:right>".$row["syn"]."</td>";
echo "<td style= text-align:right>".$row["ok"]."</td>";
echo "<td style= text-align:right>".$row["ok%"].'%';"</td>";
}
echo '</tr>';
}
echo '</table>';
}
else
{
echo "0 results";
}
Yuo have to change the query.
if you select distinct akexam, you will found a list of different akexam values in the table. Your rows will be just $row["akexam"]
If you select distinct * you will select all the different rows in the table.
It will look like a normal select but it will discard the rows that are identical (if you have a primary key in the table all the rows are different, i warn you).
So please provide an explanation of why you want to select distinct and i can give you more precise solution.
You have to select each column individually.
SELECT DISTINCT `akexam`, `L_code`, .... /*Fetch other columns*/
FROM `table`
WHERE akexam = '2003-04.e'

Php empty array checking

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>";
}
}

Unique radio name in php while loop

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.

Freezing Table columns and rows using only one table

I have a dynamic table which is filled from an Application.
My problem is that I need some way to freeze the first row and the first four columns BUT using only one table, obviously because I will be posting the table back again to the application.
My Current Table Code :
....
<form name="myform" method="post" action="test123.php" onsubmit="set_value();">
//this form is used to post back the data after user updates it.
<br />
<table align="center" id="dataTable">
<thead>
<tr class="header">
...
$arratt49 = (explode(',',$response49,-1)); //sending an array from application
$dayscounter = sizeof($arratt49);
$sizemon = strlen($month1);
if($sizemon > 0)
{
echo "<th class='text ce6'>No.</th>";
echo "<th class='text ce6'>Emp code</th>";
echo "<th class='text ce6'>Name</th>";
echo "<th class='text ce6'>Department </th>";
for($kkk=0;$kkk<$dayscounter;$kkk++)
{
echo "<th class='text ce7'>";
echo $arratt49[$kkk];
echo "</br>";
echo $arratt49[$kkk+1];
echo "</th>";
$kkk = $kkk +1;
}
}
</tr>
</thead>
<tbody>
<tr>
....//Here goes the Body
for($k=0;$k<=$arrcnt;$k++)
{
echo "<td class='text ce2'>";
echo $counter;
echo "</td>";
echo "<td class='text ce2'><INPUT type='text' name='code[]' size = '8' readonly='true 'value='";
echo $arratt[$k];
echo "'";
echo "</td>";
echo "<td class='text ce2'><INPUT type='text' name='name[]' size = '40' readonly='true' value='";
echo $arratt[$k+1];
echo "'";
echo "</td>";
echo "<td class='text ce2'><INPUT type='text' name='dept[]' size = '12' readonly='true 'value='";
echo $arratt[$k+2];
echo "'>";
echo "</td>";
Already tried :
Big Four method
and also This
Any Good Ideas?
Again I CANNOT use more than one table method.
You can use datatables plugin FixedColumns, just set parameter "iLeftColumns" to the number of columns needed to be fixed from the left side of table. Header already fixed by default.

post value's text field OrderForm

I have multiple books that people can order. All those books are stored in a MySQL database.
People can enter value's (INT) into a textfield. Example: Book One value = [5].
But when I enter submit it will only show the last entered value of that textfield.
How can I arrange, that if people only enter value's in some textfields and then hit submit they see what product they ordered and the value with it. Thanks :)
My code
<table width="990">
<form name="form" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<tr>
<td width="93" class="main">Bestelnummer</td>
<td width="550" class="main">Titel</td>
<td width="100" class="main">Categorie</td>
<td width="150" class="main">Type Onderwijs</td>
<td width="80" class="main">Groep</td>
<td width="50" class="main">Prijs</td>
<td width="40" class="main">Aantal</td>
</tr>
<?php
// Laat Resultaten zien
$s = "SELECT * FROM producten ORDER BY id ASC";
$sql = (mysql_query($s))or die ("FOUT: " . mysql_error());
while($row = mysql_fetch_array($sql))
{
$id = $row['id'];
echo "<tr>";
echo "<td width='93'>" .$row['bestelnummer']. "</td>";
echo "<td width='550'><a href='".$row['link']."' target='_blank' title='".$row['titel']."' >" .$row['titel']. "</a></td>";
echo "<td width='100'>" .$row['categorie']. "</td>";
if ($row['onderwijs'] == "BO") { echo "<td width='150'>Basis Onderwijs</td>"; } elseif ($row['onderwijs'] == "VO") { echo "<td width='150'>Voortgezet Onderwijs</td>"; } else { }
echo "<td width='80'>" . $row['groep'] . "</td>";
if ($row['prijs'] == 0) { echo "<td width='50'><i>gratis</i></td>"; } else { echo "<td width='50'>€ " .$row['prijs']. "</td>"; }
echo "<td width='40'><input type='text' name='nummer".$id."' title='nummer".$id."' class='aantal' maxlength='4' /></td>";
echo "</tr>";
}
?>
<tr>
<td><input type="submit" name="submit" value="Plaats bestelling" class="verzend"/></td>
</tr>
</form>
</table>
<?php if (isset($_POST['submit']))
{
if (empty($_POST['aantal']))
{
echo "L33g";
}
else
{
$_POST['nummer".$id."'];
}
}?>
You would be better off using an array so use
<input type='text' name='nummer[".$id."]' title='nummer".$id."' class='aantal' maxlength='4' />
Then replace
$_POST['nummer".$id."'];
with
foreach($_POST['nummer'] as $value) {
echo $value;
}

Categories