So, I am trying to sort a database by clickable links. I have a function that is supposed to order them, but for some reason it isn't working. Also, once I add something to the database and submit, if I click on the links to order, it sends me to my actual function holding file in the url, which is weird.
Forgive table formatting issues. I'm still debugging that.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Project 1</title>
</head>
<body>
<h1 style='text-align:center;'>Movie Collection Database</h1>
<form method='post' action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table align='center' border='1' cellpadding='5'>
<tr>
<td>
<input type="text" size='40' name="movieTitle" value="Movie Title">
</td>
<td>
<input type="text" name="studioName" value="Studio Name">
</td>
<td>
<select name="movieRating">
<option value="G">G</option>
<option value="PG">PG</option>
<option value="PG-13">PG-13</option>
<option value="R">R</option>
<option value="NC-17">NC-17</option>
<option value="Not Rated">Not Rated</option>
</select>
</td>
<td>
<input type="text" name="publicationYear" value="2015">
</td>
<td>
<input type="number" name="imdbRating" value="10.0">
</td>
<td>
<input type="number" name="runTime" value="0">
</td>
<td>
<input type="checkbox" name="add">Add
</td>
</tr>
</table>
<?php
$db = new PDO("mysql:host=localhost;dbname=berkleyr", "berkleyr", "12345");
?>
<?php include_once("Project1DB.php"); ?>
<?php if ($_SERVER['REQUEST_METHOD'] === "POST"):
delete($db);
if(isset($_POST['add']))
{
try
{
Insert($db, $_POST['movieTitle'], $_POST['studioName'], $_POST['movieRating'], $_POST['publicationYear'], $_POST['imdbRating'], $_POST['runTime']);
}
catch (PDOException $error)
{
$db->rollback();
echo "Bad things Happened";
$db = null;
die("Connection failed: " . $error->getMessage());
}
}
?>
<table align='center' border='1'>
<tr>
<th>Title</th>
<th>Studio Name</th>
<th>Rating</th>
<th>Pub Year</th>
<th>IMDB Rating</th>
<th>Run Time</th>
<th>Delete</th>
</tr>
<?php foreach (Select($db) as $row): ?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['studio']; ?></td>
<td><?php echo $row['rating']; ?></td>
<td><?php echo $row['pub_year']; ?></td>
<td><?php echo $row['imdb_rating']; ?></td>
<td><?php echo $row['run_time']; ?></td>
<?php echo "<td><input type='checkbox' name='". $row['id'] . "' value='" . $row['id'] . "'></td>";?>
</tr>
<?php endforeach ?>
<?php
if (isset($db))
{
$db = null; // make sure an exception didn't already close the connection. If not, close it now.
}
?>
<?php else: ?>
<table align='center' border="1">
<tr>
<th>Title</th>
<th>Studio Name</th>
<th>Rating</th>
<th>Pub Year</th>
<th>IMDB Rating</th>
<th>Run Time</th>
<th>Delete</th>
</tr>
<?php foreach (Select($db) as $row): ?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['studio']; ?></td>
<td><?php echo $row['rating']; ?></td>
<td><?php echo $row['pub_year']; ?></td>
<td><?php echo $row['imdb_rating']; ?></td>
<td><?php echo $row['run_time']; ?></td>
<?php echo "<td><input type='checkbox' name='". $row['id'] ."' value='" . $row['id'] . "'></td>";?>
</tr>
<?php endforeach ?>
<?php endif ?>
<td colspan="7" align="center">
<input type='submit' name='submit' value='Update Database' />
</td>
</table>
</form>
</body>
</html>
Here is my function for sorting:
function orderBy($order)
{
switch($order)
{
case "title":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.title ASC");
break;
case "studio":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.studio ASC");
break;
case "rating":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.rating ASC");
break;
case "pub_year":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.pub_year ASC");
break;
case "imdb_rating":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.imdb_rating ASC");
break;
case "run_time":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.run_time ASC");
break;
}
$sorted->execute();
}
Related
Table name :- add_user
data is inserting in it, there is a column due_date storing date in (dd-mm-yyyy) format
now i want to create a filter for due_date. But my code is not working. When i'm running the code it is showing all data, it is not filtering it.
here is my code
<form class="form-inline" method="post" action="">
<label> <b>From Date : </b></label>
<input type="date" name="s_date" class="form-control">
<input type="date" name="e_date" class="form-control">
<button type="submit" name="search" class="btn btn-success">Search</button>
</form>
<?php
error_reporting(0);
include_once "config2.php";
if(count($_POST)>0) {
$s_date=date("dd-mm-yyyy", strtotime($_POST['s_date']));
$e_date=date("dd-mm-yyyy", strtotime($_POST['e_date']));
$result = mysqli_query($conn,"SELECT * FROM add_user WHERE due_date BETWEEN '$s_date' AND '$e_date'");
}
?>
<center>
<table class='table table-bordered'>
<thead class="thead-dark">
<tr>
<th>Action</th>
<th>Pet Photo</th>
<th>Owner</th>
<th>Mobile Number</th>
<th>Category</th>
<th>Pet Id</th>
<th>Pet Name</th>
<th>Address</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><button class="btn btn-info">View</button></td>
<td><img src="/pet_img/<?php echo $row["signature"]; ?>"width="60px"height="80px"> </td>
<td><img src="/pet_img/<?php echo $row["photo"]; ?>"width="60px"height="80px"> <br><b> <?php echo $row["name"]; ?> </td>
<td><?php echo $row["mobile"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><b><font color="green"><?php echo $row["pet_id"]; ?></font></b></td>
<td><?php echo $row["pet_name"]; ?></td>
<td><?php echo $row["add1"]; ?> <?php echo $row["add2"]; ?> <?php echo $row["add3"]; ?> <?php echo $row["pin"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>
Your column data type needs to be either timestamp or date/datetime. You can't compare date strings in this format.
I have 17 records inserted in my table "book".
<?php
include 'config.php';
session_start();
$sql = "SELECT * from book";
$result = mysqli_query($db, $sql);
$count = mysqli_num_rows($result);
if($count == 0){
$error = "There is no books in the list";
}
?>
<html>
<body>
<table border="2" style="width:80%;" align="center">
<tr>
<th>ISBN</th>
<th>Title of the book</th>
<th>Cost</th>
<th>Copies</th>
<th>Edition</th>
<th>Publisher</th>
<th>Copy Year</th>
<th>Shelf Number</th>
<th>Subject</th>
</tr>
<?php while($row = mysqli_fetch_array($result)){
$ISBN = $row['ISBN'];
$Title = $row['Title'];
$Cost = $row['Cost'];
$Copies = $row['Copies'];
$Edition = $row['Edition'];
$Publisher = $row['Publisher'];
$CopyYr = $row['CopyYr'];
$Shelf = $row['ShelfNo'];
$Subject = $row['SubName'];
?>
<tr>
<td><?php echo $ISBN; ?></td>
<td><?php echo $Title; ?></td>
<td><?php echo $Cost; ?></td>
<td><?php echo $Copies; ?></td>
<td><?php echo $Edition; ?></td>
<td><?php echo $Publisher; ?></td>
<td><?php echo $CopyYr; ?></td>
<td><?php echo $Shelf; ?></td>
<td><?php echo $Subject; ?></td>
</tr>
<?php
}
?>
</table>
<br>
<table align="center" style="width: 30%">
<tr>
<th><form action="AddBook.php" method="post">
<input type="submit" value="Add Book"/>
</form></th>
<th><form action="editbook.php" method="post">
<input type="submit" value="Edit List"/>
</form></th>
<th><form action="deletebook.php" method="post">
<input type="submit" value="Delete Book/s"/>
</form></th>
<th><form action="AdminSummary.php" method="post">
<input type="submit" value="Back"/>
</form></th>
</tr>
</table>
</center>
</body>
</html>
This will display 17 records in the table. But when I add another record to the table, It will display an incomplete record, a "500 server error", or an empty table.
I have been in the same problem for days, searching for a solution, and I can't find any.
I don't know what the problem is, so any help would be appreciated. Thank You :)
I'm running two while loops to display a table in html.
The first while loop is to display data from a selected databasetable as long as there is content in the database.
My second while loop displays a dropdown, where the content of another table should be displayed.
My goal is to show this dropdown each row. My problem is that the dropdown filled with data is just shown in the first tablerow. All the other rows just show an empty selectfield. Can someone help me what I did wrong?
My Code so far:
$link=pg_connect($conn_str);
if (!$link) {die('Verbindung schlug fehl.');}
$arbeitspaket = pg_query($link, "SELECT * FROM arbeitspaket WHERE id='$_SESSION[user_id]'");
$sql = "SELECT * FROM anwender ORDER BY nachname ASC";
$mitarbeiter = pg_query($link, $sql); ?>
<form action=mitarbeiterauswahl.php method=post>
<table border=1>
<tr>
<th>Arbeitspaket-ID</th>
<th>Arbeitspaketbezeichnung</th>
<th>Mitarbeiterbedarf</th>
<th>Mitarbeiterzuordnung</th>
</tr>
<?php while($results=pg_fetch_array($arbeitspaket)){?>
<tr>
<td><?php echo $results['apid']; ?></td>
<td><?php echo $results['arbeitspaketbezeichnung']; ?></td>
<td><?php echo $results['mitarbeiterbedarf']; ?></td>
<td>
<select name="mitarbeiter">
<?php while($row = pg_fetch_array($mitarbeiter)){
echo '<option value="'. $row['id'] .'">('. $row['id'] .') '. $row['vorname'] .' '. $row['nachname'] .'</option>'."\n"; }?>
</select>
</td>
</tr>
<?php } ?>
</table>
</form>
Why don't you try a foreach? Looks much better in this situation. Something like this:
<table border=1>
<tr>
<th>Arbeitspaket-ID</th>
<th>Arbeitspaketbezeichnung</th>
<th>Mitarbeiterbedarf</th>
<th>Mitarbeiterzuordnung</th>
</tr>
<?php $results= pg_fetch_array($arbeitspaket);
$arbeiters = pg_fetch_array($mitarbeiter);
foreach($results as $result){?>
<tr>
<td><?php echo $result['apid']; ?></td>
<td><?php echo $result['arbeitspaketbezeichnung']; ?></td>
<td><?php echo $result['mitarbeiterbedarf']; ?></td>
<td>
<select name="mitarbeiter">
<?php foreach($arbeiters as $arbeiter) {
echo '<option value="'. $arbeiter) ['id'] .'">('. $arbeiter) ['id'] .') '. $arbeiter) ['vorname'] .' '. $arbeiter) ['nachname'] .'</option>'."\n"; }?>
</select>
</td>
</tr>
<?php } ?>
</table>
The problem in your code is that you consume all the data in the first loop, after that it is gone. Store the data in an array and iterate over the array.
<form action=mitarbeiterauswahl.php method=post>
<table border=1>
<tr>
<th>Arbeitspaket-ID</th>
<th>Arbeitspaketbezeichnung</th>
<th>Mitarbeiterbedarf</th>
<th>Mitarbeiterzuordnung</th>
</tr>
<?php
$mitarbeiter_array = array();
while($row = pg_fetch_array($mitarbeiter)) {
$mitarbeiter_array[] = $row;
}
unset($row);
?>
<?php while($results=pg_fetch_array($arbeitspaket)){?>
<tr>
<td><?php echo $results['apid']; ?></td>
<td><?php echo $results['arbeitspaketbezeichnung']; ?></td>
<td><?php echo $results['mitarbeiterbedarf']; ?></td>
<td>
<select name="mitarbeiter">
<?php foreach($mitarbeiter_array as $row){
echo '<option value="'. $row['id'] .'">('. $row['id'] .') '. $row['vorname'] .' '. $row['nachname'] .'</option>'."\n"; }?>
</select>
</td>
</tr>
<?php } ?>
</table>
</form>
i think it will be better way and will be fast
<form action=mitarbeiterauswahl.php method=post>
<?php
$select_html = '<select name="mitarbeiter">';
$select_list_data = pg_fetch_array($mitarbeiter);
?>
<?php foreach($select_list_data as $row){ ?>
<?php
$select_html .= '<option value="'. $row['id'] .'">('. $row['id'] .') '. $row['vorname'] .' '. $row['nachname'] .'</option>';
?>
<?php } ?>
<?php $select_html .= '</select>'; ?>
<table border=1>
<tr>
<th>Arbeitspaket-ID</th>
<th>Arbeitspaketbezeichnung</th>
<th>Mitarbeiterbedarf</th>
<th>Mitarbeiterzuordnung</th>
</tr>
<?php
$fetch_results = pg_fetch_array($arbeitspaket);
foreach($fetch_results as $results){ ?>
<tr>
<td><?php echo $results['apid']; ?></td>
<td><?php echo $results['arbeitspaketbezeichnung']; ?></td>
<td><?php echo $results['mitarbeiterbedarf']; ?></td>
<td>
<?php echo $select_html; ?>
</td>
</tr>
<?php } ?>
</table>
I have a group of radio buttons placed in a form with method post. But anytime the form is post the value from the radio button is zero. Below is the code
<form method="POST" action='insert_per.php'>
<table >
<caption>
Grant Permissions
</caption>
<thead>
<tr>
<th class="center"><strong>Name</strong></th>
<th class="center"><strong>Email</strong></th>
<?PHP if($_SESSION['user_group'] == 63){
echo '<th class="center"><strong>System Admin</strong></th>';
echo '<th class="center"><strong>Admin</strong></th>';
echo '<th class="center"><strong>User</strong></th>';
} ?>
<th class="center"><strong></strong></th>
</tr>
</thead>
<?php
if($stmt->execute()){
// output data of each row
while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>
<td border="0" type="hidden" style="display:none;"><input type="hidden" name="hidden" value=<?php echo $rows['member_id']; ?></td>
<td class="center"><?php echo $rows['username']; ?></td>
<td class="center"><?php echo $rows['email']; ?></td>
<?PHP if($_SESSION['user_group'] == 63){ ?>
<td class="center"><input type='radio' id=<?php echo $rows['username']; ?> name="gp" value="system admin"
<?php echo ($rows['permission']== 31 )?'checked':'' ?>></input> </td>
<td class="center"><input type='radio' id=<?php echo $rows['username']; ?> name="gp" value="admin"
<?php echo ($rows['permission']== 15 )?'checked':'' ?> ></input> </td>
<td class="center"><input type='radio' id=<?php echo $rows['username']; ?> name="gp" value="user"
<?php echo ($rows['permission']== 1 )?'checked':'' ?> ></input> </td> <?PHP } ?>
<td class="center"><button name="SUBMIT" type="SUBMIT">submit</button></td>
</tr>
<?php
}
}
?>
</table>
</form>
Below is the code for the insert_per.php
echo "this is the gp "+ $_POST['gp'];
echo "this is the gp "+ $_POST['hidden'];
try{
$stmt= $dbh->prepare("UPDATE `members_groups` SET `group_id`=(SELECT group_id FROM groups WHERE
group_name = ? ) WHERE member_id = ?");
$stmt->bindParam(1, $_POST['gp'] , PDO::PARAM_STR);
$stmt->bindParam(2 , $_POST['hidden'] , PDO::PARAM_INT);
if($stmt->execute()) {
echo " the privilege was granted successfully ".'<BR>';
echo "<a href='user_acc.php'>View result</a>";
}
} catch(PDOException $e)
{
echo "An error occured ".$e->getMessage();
}
The radio buttons are expected to produce one of the following values
"system admin"
"admin"
"user"
but it always produces zero "0" value.
I had Created a Web App in PHP Code igniter which generates table containing calculation of employees earning, deduction.
Since I am newbie to CI. I am trying to save the dynamic generated table data to database, but failed.
I am not getting that how to POST table data to Controller.
Any help is welcome.
This is My Controller To generate dynamic table
function index()
{
if($this->input->post("submit"))
{
$vals=$this->cpanel_model->esic();
if($vals){
$data['w_share']=$vals->w_share;
$data['e_share']=$vals->e_share;
}
$data["wshare"]="";
//echo $data['w_share'];
$city=$_POST["city"];
$v1= $data['w_share'];
$v2= '100';
$v3= $data['e_share'];
$arr=$_POST["wid"];
$arr1=$_POST["days"];
$arr2=$_POST["payment"];
$arr3=$_POST["wdays"];
for ($i=0; $i <count($arr1) ; $i++)
{
$net[$i]=($arr2[$i]/$arr3[$i])*$arr1[$i];
$wshare[$i]=($net[$i]/$v2)*$v1;
$eshare[$i]=($net[$i]/$v2)*$v3;
$total[$i]=$eshare[$i]+$wshare[$i];
$wdays[$i]=$arr3;
$days[$i]=$arr1;
}
$data["net"]=$net;
$data["wshare"]=$wshare;
$data["total"]=$total;
$data["eshare"]=$eshare;
$data["wdays"]=$arr3;
$data["days"]=$arr1;
$data["list"]=$this->payment_model->search($city);
$this->load->view("secure/php-version/esic",$data);
}
This Is My View to echo table values
<?php echo form_open('esic/insert',array("role"=>"form")); ?>
<div class="table-responsive">
<table border="1" id="t01" class="table table-striped table-bordered table-hover table-condensed">
<tr>
<th>Worker_id</th>
<th>Worker Name</th>
<th>Worker City</th>
<th>Designation</th>
<th>Working Days</th>
<th>Basic Payment</th>
<th>Net Payment</th>
<th>Present Days</th>
<th>Worker Share (1.75%)</th>
<th>Employee Share (4.75%)</th>
<th>Total</th>
</tr>
<?php $i=0; $sum2=0; ?>
<?php if(count($list)>0) { foreach ($list as $std) {?>
<tr>
<?php echo form_hidden("wid[]",$std["wid"]); ?>
<?php echo form_hidden("wdays[]",$wdays); ?>
<?php echo form_hidden("payment[]",$std["payment"]); ?>
<td><?php echo $std["wid"]; ?></td>
<td><?php echo $std["wname"]; ?></td>
<td><?php echo $std["city"]; ?></td>
<td><?php echo $std["designation"]; ?></td>
<td><?php echo $wdays[$i]; ?></td>
<td><?php echo $std["payment"]; ?></td>
<td><?php echo $net[$i]; ?></td>
<td><?php echo $days[$i]; ?></td>
<td><?php echo $wshare[$i]; ?></td>
<td><?php echo $eshare[$i]; ?></td>
<td><?php echo $total[$i]; ?></td>
<?php $sum2 += $std["payment"]; ?>
</tr>
<?php $i++;}}?>
<tr>
<?php
$sum=0;
$sum1=0;
$sum3=0;
$sum4=0;
$sum5=0;
$i=0;
foreach ($total as $value) {
$sum = $sum + $value;
$i++;
}
foreach ($days as $value) {
$sum1 = $sum1 + $value;
$i++;
}
foreach ($eshare as $value) {
$sum3 = $sum3 + $value;
$i++;
}
foreach ($wshare as $value) {
$sum4 = $sum4 + $value;
$i++;
}
foreach ($net as $value) {
$sum5 = $sum5 + $value;
$i++;
}
?>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum2; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum5; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum1; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum4; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum3; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum; ?>
</td>
</tr>
</table>
</div>
<?php echo form_submit(array("name"=>"submit","type"=>"submit","value"=>"Create ESIC")); ?>
<?php echo form_close(); ?>
Its not too clear on how you are currently generating your table, but regardless, when you generate your table you have to place it in a form, and assign keys to inputs. Try this approach:
HTML (these are the results you should get when you generate your table)
<form action="<?= base_url() ?>table_data" method="post">
<table>
<thead>
<tr>
<th>Name</th>
<th>Earnings</th>
<th>Deduction</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="hidden" name="table[0][name]" value="name1">name1</td>
<td><input type="hidden" name="table[0][earning]" value="earning1">earning1</td>
<td><input type="hidden" name="table[0][deduc]" value="deduc1">deduc1</td>
</tr>
<tr>
<td><input type="hidden" name="table[1][name]" value="name1">name1</td>
<td><input type="hidden" name="table[1][earning]" value="earning1">earning1</td>
<td><input type="hidden" name="table[1][deduc]" value="deduc1">deduc1</td>
</tr>
</tbody>
</table>
<input type="submit" value="Submit this table">
</form>
Controller:
function table_data() {
if ($this->input->post()) {
$table_data = $this->input->post('table');
$this->load->model('model_name');
$this->model_name->insert_table_data($table_data);
}
}
Model:
function insert_table_data($table_data){
$this->db->insert_batch('mytable', $table_data);
}