http://i.stack.imgur.com/TyC6U.png
I have something like this and when a person checked a checkbox and then click the button Save, it would save the checked box and the next time the person see this table it would appear the table with all the checkboxs that the person checked or unchecked last time. How can I do that?
The months are in one array, check my code:
<?php
$results = mysql_query("SELECT * FROM utilizadores");
$month = array("January","February","March","April","May","June","July","August","September","October","November","December");
echo '<form id="form" method="post" action="">';
echo '<table border="1" align="center">';
echo '<tr class="d0">';
echo '<td align="center" height="30" width="400"><strong>Nome</strong></td>';
for($i=0;$i<=11;$i++)
{
echo '<td align="center" width="80"><strong>'.#$month[$i].'</strong></td>';
}
echo '</tr>';
$rowColors = Array('#ffffff','#ffffcc'); $c= 0;
while($row = mysql_fetch_array($results)) {
echo '<tr style="background-color:'.$rowColors[$c++ % count($rowColors)].';">';
echo '<td align="center" height="30" width="400">' . $row['nome'] . "</td>";
for($i=0;$i<=11;$i++) {
echo '<td align="center" width="80"><input type="checkbox" name="asd" /></td>';
}
}
echo "</tr>";
echo "</table>";
echo "<br>";
echo '<input name="submit" type="submit" value="Save" />';
echo '</form>';
?>
<br />
</body>
</html>
Related
I am making a table that shows some data and I have it in a while where the data from the database is taken.
the problem is that I need a form tag for each of the tr to be able to take the data later, this works in all but not in the first field.
Code:
while ($fila=mysqli_fetch_row($resultado)){
$user_name = $fila[0];
//echo '<script>alert("'.$user_name.'");</script>';
$last_name = $fila[1];
$email1 = $fila[2];
$id = $fila[3];
echo '<form id="update_user'.$id.'" name="update_user'.$id.'" action="" method="post">';
echo '<tr>';
echo '<td class="quit_click" id="nombre'.$id.'"><input id="innombre'.$id.'" name="innombre" type="text" value="'.$last_name.'"/></td>';
echo '<td class="quit_click" id="user'.$id.'"><input id="inuser'.$id.'" name="inuser" type="text" value="'.$user_name.'"/></td>';
echo '<td class="quit_click" id="email'.$id.'"><input id="inemail'.$id.'" name="inemail" type="text" value="'.$email1.'"/></td>';
echo '<td class="quit_click" id="pass'.$id.'"><input id="inpass'.$id.'" name="inpass" type="text" value="********"/></td>';
echo '</tr>';
echo '</form>';
}
In all fields it shows the form and the tr minus in the first one that only shows the content of the tr, does anyone know why?
If more code is needed, tell me
Thank you.
Please dont break the html structure, table tag should be followed with thead, tr and td so form shouldnot come in between. I cannot identify the issue without seeing the full code But Can you check by adding the table fully inside the form as given below
while ($fila=mysqli_fetch_row($resultado)){
$user_name = $fila[0];
$last_name = $fila[1];
$email1 = $fila[2];
$id = $fila[3];
echo '<form id="update_user'.$id.'" name="update_user'.$id.'" action="" method="post">';
echo '<table>';
echo '<tr>';
echo '<td class="quit_click" id="nombre'.$id.'"><input id="innombre'.$id.'" name="innombre" type="text" value="'.$last_name.'"/></td>';
echo '<td class="quit_click" id="user'.$id.'"><input id="inuser'.$id.'" name="inuser" type="text" value="'.$user_name.'"/></td>';
echo '<td class="quit_click" id="email'.$id.'"><input id="inemail'.$id.'" name="inemail" type="text" value="'.$email1.'"/></td>';
echo '<td class="quit_click" id="pass'.$id.'"><input id="inpass'.$id.'" name="inpass" type="text" value="********"/></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
}
Hi I am looking to add a Show More / Load More button to this For Each loop.
I am getting data from an API through JSON and displaying it in 3 columns.
I would like to limit it to say 3 rows of three, then perhaps use another row with only 1 td with a button centred with a GET parameter adding more to a Limit variable?
Here is my code how would be the best way to do this? Many thanks
$output = json_decode($output);
curl_close($ch);
$i = 0;
echo '<table border="0" align="center" padding-top="10px" padding-right="10px" padding-left="10px" padding-bottom="10px" style="width: 100% ">';
echo '<tr >';
foreach($output as $template) {
echo '<td align="center"><br /><br /><b>' . '<font color="#0083C1" size="5" face="sans-serif">'.$template->template_name .'</font></b><br />';
echo '<ul class="img-list"><li><img src="'. $template->thumbnail_url.'" height="250" width="370">'.'<span class="text-content"><b>'.'Preview Website'.'</b></span>'.'</li></ul>';
echo '<form method="GET" value="/websites/choose-your-website.php">';
echo '<input type="hidden" name="template_id" value="' . $template->template_id . '">';
echo '<input type="email" name="email" placeholder="Enter Your Email and Click Below" required" align="center" style="width: 250px">'.'<br />';
echo '<button type="submit" align="center" class="button">Choose Site</button><br />';
echo '</form></td>';
$i++;
if($i == 3) {
echo '</tr>'.'<tr>';
$i = 0;
}
}
echo '</tr>';
echo '</table>';
The file is in PHP with JSON data pulling information from an API displaying in a PHP table
I have a table that displays every unapproved leave, it looks like this:
For each unapproved there will be a new row, so another checkbox, but the name of the checkbox will be the same or slightly different(name="approve1"). Personally I don't think that's good practice...
Also the evaluation isn't really good, because it has to go into the database.
<form role="form" method="post" action="" id="form">
<h2><?php echo $title ?></h2>
<?php
$page = date('F');
$page = lcfirst($page);
echo "<p><a href='$page.php'>Back</a></p>";
?>
<hr>
<?php
if($i == 0){
echo "<h3>No unapproved leaves.</h3>";
}else{
echo '<table class="table table-striped table-hover">';
echo '<thead>';
echo '<th>';
echo 'Start';
echo '</th>';
echo '<th>';
echo 'End';
echo '</th>';
echo '<th>';
echo 'Employee';
echo '</th>';
echo '<th>';
echo 'Approve';
echo '</th>';
echo '</thead>';
echo '<tbody>';
for($j =0; $j < $i; $j++){
echo '<tr>';
echo '<td>';
echo $unapproved[$j]['start'];
echo '</td>';
echo '<td>';
echo $unapproved[$j]['end'];
echo '</td>';
echo '<td>';
$id = $unapproved[$j]['employee_FK'];
$result = mysql_query("select name, surname from employee where employee_ID = $id");
while ($row = mysql_fetch_assoc($result)) {
$employee[] = $row;
}
echo $employee[0]['name'], " ", $employee[0]['surname'];
echo '</td>';
echo '<td>';
echo '<input type="checkbox" name="approve" value="1" id="approve">';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
}
?>
<input type="submit" name="submit" value="Submit" class="btn" >
</div>
</div>
</form>
Can anyone think of a better, easier solution?
The practice for tabular data in most frameworks is to name them Model[id][field], so in your example it would look like "<input type='checkbox' name='leave[$j][approved]' value='1' id='leave_$j_approved'>".
Then you can easily access your data by iterating the post data like so:
foreach($_POST['leave'] as $id => $values) {
}
use checkbox name like this
<input type="checkbox" name="approve[]" value="<?php $your_row_id; ?>" id="approve">
here $your_row_id means unique id of a row in database.
And in post you will get an array named approve with all the selected values.
Parse that array using foreach.
You can generate checkboxes using a array notation in the "name" attribute.
<input type="checkbox" name="approve[]" value="1" id="approve">
After on send, you can recover values using array notation $_GET['approve'][0], ...
You can transfer arrays via HTTP :
echo '<input type="checkbox" name="approve[]" value="', $id, ']" id="approve">';
Now $_GET['approve'] will be an array which only contains values of checked boxes.
You can also specify the key associated with the value:
echo '<input type="checkbox" name="approve[', $id, ']" value="', $id, ']" id="approve">';
so i have a form with like 6 fields(FirstName,LastName,etc),when the users submit the form,the values are saved in a database.In another page i have a table which shows those values from database,with same 6 cols + another one which contains 2 buttons(delete and edit),the delete one works perfect,but the edit buttons has some issues
The buttons code is this:
echo '<td>';
echo '<button>Modifica!</button>';
echo '<button>Sterge!</button>';
echo '</td>';
As you can see i have assigned rand id(this is the primary key for databse rows) to the var idang.
$idang=$_GET['idang'];
if(isset($idang)) {
$CerereSQL="SELECT * FROM `angajati` WHERE id='$idang'";
$rezultat=mysqli_query($con,$CerereSQL);
$rand=mysqli_fetch_assoc($rezultat);
I've made another variable with the global var $_GET['idang'],and then i have SELECTED the table angajati from database where the primary key from my db its equal with the primary key from the table row,which i've pushed the edit button.This method works for the delete button,and probably for the edit one too,but the real problem is that when i want to edit that row,it will link me to another page containing another form,with original values in the fields,and when i modify those fields with other values,it must change only that row,not the entire table,or the column.But i have no idea how to do that,except that it can be done with the UPDATE statement
The rest of the code,containing the form is:
echo '<table border=0 align=center cellspacing=10>';
echo '<form action="" method=post>';
echo '<tr>';
echo '<th>Nume:</th><td><input type="text" name=nume value="';
echo $num;
echo '"></td>';
echo '<td style=color:red>';
echo $err_nume;
echo $err_nume1;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Prenume:</th><td><input type="text" name=prenume value="';
for($value=1;$value<count($nume);$value++) {
$c=$c."".$nume[$value];
}
echo $c;
echo '"></td>';
echo '<td style=color:red>';
echo $err_prenume;
echo $err_prenume1;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>CNP:</th><td><input type="text" name=cnp value="';
echo $cnp;
echo '"></td>';
echo '<td style=color:red>';
echo $err_cnp;
echo $err_cnp1;
echo $err_cnp2;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Data angajare:</th><td><input type="text" name=data value="';
echo $rand['data'];
echo '"></td>';
echo '<td style=color:red>';
echo $err_data;
echo '</td>';
echo '</tr>';
$companii=array("S.C. 2K Telecom S.R.L.", "S.C. Accessnet International S.R.L.", "S.C. Ad Net Market Media S.A.", "S.C. Idilis S.R.L.", "S.C. Infratel Net S.R.L.");
echo '<tr>';
echo '<th>Compania:</th>';
echo '<td>';
echo '<select name=companie>';
//echo '<option value="alege">Alege compania</option>';
//if(!isset($_POST['companie'])){
echo '<option value='.$companie.'>'.$companie.'</option>';
$key=array_search($companie, $companii);
unset($companii[$key]);
//}
foreach($companii as $firme) {
echo '<option value="'.$rand['companie'].'"';
if(isset($_POST['companie']) && $_POST['companie'] == $firme) {echo " selected";}
echo ">$firme</option>";
}
echo '</select>';
echo '</td>';
echo '<td style=color:red>';
echo $err_companie;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Functia:</th><td><input type="text" name=functie value="';
echo $rand['functie'];
echo '"></td>';
echo '<td style=color:red>';
echo $err_functie;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align=center><a href=tabel.php><input type=button name=button value=Inapoi!></a></td>';
echo '<td align=center><input type=submit name=submit value=Introducere! ></td>';
echo '<td align=center><input type=reset name=reset value=Resetare! ></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2" align=center>'.$up.'</td>';
echo '</tr>';
echo '</form>';
echo '</table>';
So this form contains the old values from the table row i've pressed the button,and i want to cahnge with new values.Any idea?
Here is basic example to update name column of your table. Get other fields and add in the query.
if(isset($_POST['submit'])) {
//echo "<pre>";print_r($_POST);exit; // to check your post data
$name = $_POST['nume'];
$query = "UPDATE table_name set name='$name' where id=$idang";
mysqli_query($query); //run the query
}
echo '<form action="abc.php" method=post>'; // add action
abc.php
If($_POST)
{
// fetch values from post
// update query
}
in else you can do if $_GET then insert.
I think from this you will get an idea to solve.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
This code works and is fine, but I was wondering if there was an easier way to code something like this?
<?php
while($rows=mysql_fetch_array($result)){
$pid=$rows['id'];
$d = $rows['date'];
echo '<style>tr, td {border:0;} #dat{text-align:right;} #co {background-color:#33B533;} </style>';
echo '<div id="postd">';
echo '<table id="mdata">';
echo '<tr>';
echo '<td>';
echo '<a href="profile.php?name=';
echo $rows['name'];
echo '">' . $rows["name"] . '</a>';
echo '</td>';
echo '<td id="dat">';
echo $result7 = nicetime($d);
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo $rows['post'];
echo '</td>';
echo '<td id="dat">';
echo '<form method="post" action="delete.php">';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input id="sub" type="submit" name="delete" value="Delete" />';
echo '</form>';
echo '<form method="post" action="edit.php">';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input id="sub" type="submit" name="edit" value="Edit" />';
echo '</form>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
$sql9 = "SELECT * FROM `like` WHERE postid='$pid' AND userid='$userid'";
$result9=mysql_query($sql9);
if($result9){
echo '<form method="post" action="like.php">';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input type="hidden" value="';
echo $userid . '" name="nid" />';
echo '<input id="sub" type="submit" name="like" value="Like" />';
echo '</form>';
}
echo '</td>';
echo '<td id="dat">';
if ($rows['like'] == 1) {
echo $rows['like'] . " Like";
} else if ($rows['like'] == 0) {
echo "";
} else {
echo $rows['like'] . " Likes";
}
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</div>';
$sql2="SELECT * FROM comments WHERE postid='$pid' ORDER BY date ASC";
$result2=mysql_query($sql2);
while($rows2=mysql_fetch_array($result2)){
$c = $rows2['date'];
echo '<table id="co" border="1px solid black" width="250px" align="center">';
echo '<tr>';
echo '<td>';
echo $rows2['name'];
echo '</td>';
echo '<td id="dat">';
echo $result4 = nicetime($c);
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo $rows2['comment'];
echo '</td>';
echo '</tr>';
echo '</table>';
}
echo '<form action="addcomment.php" method="post">';
echo '<table id="co" border="1px solid black" width="250px" align="center">';
echo '<tr>';
echo '<td>';
echo '<textarea id="target" type="text" name="com" placeholder="Comment"></textarea>';
echo '</td>';
echo '<td>';
echo '<input type="submit" name="submit" value="Post">';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input type="hidden" name="name" value="Jon" />';
echo '</form>';
echo '<br/>';
}
?>
Basically I've created a Facebook type system. This spools the posts down in the order they are posted in the database. Check it out at http://bockhorst.comeze.com/Wall/wall.php Its still a work in progress. Also does anyone know a php compressor?
The more efficient way to do this is to use a proper development framework that provides some structure and cohesion to your code. What you have here is a stew of SQL, HTML, and code. It's extremely hard to maintain an application written like this.
Additionally you're using the woefully out of date mysql_query interface. Please, don't. It's terrible. You really should be using an ORM to handle your routine database interfacing, something like Doctrine or Propel. These make it easier to compose the queries correctly and deal with the data instead of being stuck trying to render all your application logic directly in SQL.
If you're dead-set on doing SQL by hand, PDO is the way to go.