I'm making this website, and it's only gonna be opened on 2 screens at the same time. I'm going to generate a table with php with output from a database.
My goal is that if the table doesn't fit on the main screen it goes to the second screen.
Is this even possible?
I've tried to come up with something but couldn't figure anything out. I have tried for loops but it has to do with the style and html or maybe javascript or jquery?
If this isn't possible should I generate a second window with jquery?
Thanks in advance!
if (!empty($_POST['check_list']))
{
$idArr = $_POST['check_list'];
$id = $idArr[0];
$parameters = array(':medId'=>$id);
$sth = $pdo->prepare("SELECT naam FROM medewerkers WHERE medewerkerid = :medId");
$sth->execute($parameters);
while ($row = $sth->fetch())
{
echo "<h2>" . $row['naam'] . "</h2>";
}
?>
<table class="table">
<tr>
<th>Donderdag <br> 31-05</th>
<?php
$datum = "2017-05-31";
$parameters = array(':date'=>$datum,
':aid'=>$id);
$sth = $pdo->prepare("SELECT DISTINCT opdrachten.KRITISCHE_DATUM, opdrachten.PLANNINGS_DATUM, opdrachten.OPDRACHTID, onderzoekactiviteiten.A_UITVOERDER
FROM opdrachten
INNER JOIN onderzoekactiviteiten
ON opdrachten.OPDRACHTID=onderzoekactiviteiten.OPDRACHTID
WHERE PLANNINGS_DATUM = :date
AND A_UITVOERDER = :aid");
$sth->execute($parameters);
while ($row = $sth->fetch())
{
$cutKritDate = substr($row['KRITISCHE_DATUM'], 0, 10);
$cutPlanDate = substr($row['PLANNINGS_DATUM'], 0, 10);
echo "<td>Krit= " . $cutKritDate . "<br>";
echo "Plan= " . $cutPlanDate . "<br>";
echo "Opdracht= " . $row['OPDRACHTID'] . "</td>";
}
?>
</tr>
<tr>
<th>Vrijdag <br> 02-06</th>
</tr>
<tr>
<th>Maandag <br> 03-06</th>
</tr>
<tr>
<th>Dinsdag <br> 04-06</th>
</tr>
<tr>
<th>Woensdag <br> 05-06</th>
</tr>
</table>
<?php
}
else
{
$check_list = NULL;
$id = NULL;
echo "U bent vergeten een medewerker aan te vinken.";
}
I have a form with names, if one is selected it generates a table. this is how I generate the table. The goal is that the tables are below each other. But when it doesn't fit anymore (when you can scroll) the table needs to go to the second screen.
Related
I am creating a page in PHP, HTML and using MySQL. Currently When I load the page, it selects all the data from the staff table and displays it.
I have a search function so the user can filter by first name, last name or full name.
When the user clicks the search button, is it possible to have the data that's showing every row and just to replace it with my searched criteria.
Currently my code is searching correct but its just adding it as a row to all the data rows. I thought i could use a regex to replace but the variable is not accessible globally since it is inside an if statement.
<?php
$output = NULL;
if(isset($_POST['submit'])) {
$regex = '/<table[^>]*>.*?<\/table>/s'; //test doesnt work
$replace = ''; //test doesnt work
$result = preg_replace($regex, $replace, $html); //test doesnt work "no html variable"
echo($result); //test doesnt work
$search = $con->real_escape_string($_POST['search']);
$res = $_POST['searchGroup'];
if($res == "first") {
$resultSet = $con->query("SELECT * FROM staff WHERE firstname LIKE '%$search%'");
if($resultSet->num_rows > 0) {
while($rows = $resultSet -> fetch_assoc()) {
$field1name = $rows["firstname"];
$field2name = $rows["lastname"];
$field3name = $rows["dob"];
$field4name = $rows["created"];
$field5name = $rows["last_updated"];
$field6name = $rows["is_user"];
$output .= '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
<td>'.$field5name.'</td>
<td>'.$field6name.'</td>
</tr>';
}
}
else {
$output = "No Results";
}
}
And then my code which is currently displaying all the data
<?php
$query = "SELECT * FROM staff";
echo '<div class="tableFixHead">
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td> <font face="Arial"><b>First Name</b></font> </td>
<td> <font face="Arial"><b>Last Name</b></font> </td>
<td> <font face="Arial"><b>Date of Birth</b></font> </td>
<td> <font face="Arial"><b>Creation Date</b></font> </td>
<td> <font face="Arial"><b>Last Updated</b></font> </td>
<td> <font face="Arial"><b>Is User</b></font> </td>
</tr>
</div>';
echo $output;
if ($result = $con->query($query)) {
while ($row = $result->fetch_assoc()) {
$field1name = $row["firstname"];
$field2name = $row["lastname"];
$field3name = $row["dob"];
$field4name = $row["created"];
$field5name = $row["last_updated"];
$field6name = $row["is_user"];
$html= '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
<td>'.$field5name.'</td>
<td>'.$field6name.'</td>
</tr>';
echo $html;
}
$result->free();
}
?>
My approach is probably wrong but I was wondering if it is possible to somehow remove that data that is in the current $html echo tag when my search button is pressed.
Besides the code style, vulnerabilities i would like to offer you an example of some sorts. as i presume that you are still a student.
just a simple example what you could do;
$query = "SELECT * FROM staff";
if(isset($_POST['submit'])) {
$search = $con->real_escape_string($_POST['search']);
$query .= " WHERE firstname LIKE '%$search%'"
}
This should be sufficient to help you forward.
This question already has answers here:
How to delete data from sql with pdo?
(2 answers)
Closed 2 years ago.
I am currently working on a delete function for my mysql database. I am not good at coding and find it very dificult. I need to learn how to make the delete function but I have been stuck for a while. You wil probably laugh at my code but as I said, its very hard for me. I hope someone can give me a tip or help me with my code.
When I run my code like this, nothing wil happen at delete.php
<html>
<head>
</head>
</body>
<table>
<tr>
<th> Naam </th> <th> Soort </th> <th> Prijs </th> <th> Bijzonderheden </th>
</tr>
<?php
$sth = $pdo->prepare('select * from gerechten');
$sth->execute();
while($row = $sth->fetch())
{
echo "<tr> ";
echo "<td>" . $row['Naam'] . "</td>";
echo "<td>" . $row['Soort'] . "</td>";
echo "<td> " . $row['Prijs'] . "</td>";
echo "<td> " . $row['Bijzonderheden'] . "</td>";
echo "<td> <a href='Delete.php?gerechtID=".$row['ID']."'><button>Delete</button></a> <br> </td>";
echo "</tr> ";
}
// de update knoppen doorverwijzen met een href --> en dan een form maken voor 1 rij <-----------
?>
</table>
</html>
Delete.php
<?php
$id = $_GET['gerechtID'];
$servername = "localhost";
$username = "root";
$password = "";
$naam = $soort = $prijs = $bijzonderheden = "";
$foutmelding1 = $foutmelding2 = $foutmelding3 = $foutmelding4 = "";
try {
$pdo = new PDO("mysql:host=$servername;dbname=restaurant", $username, $password);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
// connectie is mislukt
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$sth = $pdo->prepare('DELETE FROM gerechten WHERE ID = $id');
?>
You are using prepared statements wrong by only preparing the DELETE statement but not binding any parameters and executing it.
https://www.php.net/manual/en/pdo.prepared-statements.php
$sth = $pdo->prepare('DELETE FROM gerechten WHERE ID = ?');
$sth->bindValue(1, $id);
$result = $sth->execute();
With this PHP code I generate a table. with content from the database. Now I want to collapse it if it didn't generate any content.
It's only a part of the table otherwise there would be to much code. The rest of the table is the same.
<table class="table">
<tr class="day">
<th>
<?php
$j = 0;
thisDayIs($weekDays[$j]);
echo "<br>";
echo $weekDays[$j];
$j++;
?>
</th>
<?php
$datum = $weekDays[$j];
$parameters = array(':date'=>$datum,
':aid'=>$id);
$sth = $pdo->prepare("SELECT DISTINCT opdrachten.KRITISCHE_DATUM, opdrachten.PLANNINGS_DATUM, opdrachten.OPDRACHTID, onderzoekactiviteiten.A_UITVOERDER
FROM opdrachten
INNER JOIN onderzoekactiviteiten
ON opdrachten.OPDRACHTID=onderzoekactiviteiten.OPDRACHTID
WHERE PLANNINGS_DATUM = :date
AND A_UITVOERDER = :aid");
$sth->execute($parameters);
while ($row = $sth->fetch())
{
$cutKritDate = substr($row['KRITISCHE_DATUM'], 0, 10);
$cutPlanDate = substr($row['PLANNINGS_DATUM'], 0, 10);
echo "<td>Krit= " . $cutKritDate . "<br>";
echo "Plan= " . $cutPlanDate . "<br>";
echo "Opdracht= " . $row['OPDRACHTID'] . "</td>";
}
?>
</tr>
<tr class="day">
<th>
<?php
thisDayIs($weekDays[$j]);
echo "<br>";
echo $weekDays[$j];
$j++;
?>
</th>
<?php
$datum = $weekDays[$j];
$parameters = array(':date'=>$datum,
':aid'=>$id);
$sth = $pdo->prepare("SELECT DISTINCT opdrachten.KRITISCHE_DATUM, opdrachten.PLANNINGS_DATUM, opdrachten.OPDRACHTID, onderzoekactiviteiten.A_UITVOERDER
FROM opdrachten
INNER JOIN onderzoekactiviteiten
ON opdrachten.OPDRACHTID=onderzoekactiviteiten.OPDRACHTID
WHERE PLANNINGS_DATUM = :date
AND A_UITVOERDER = :aid");
$sth->execute($parameters);
while ($row = $sth->fetch())
{
$cutKritDate = substr($row['KRITISCHE_DATUM'], 0, 10);
$cutPlanDate = substr($row['PLANNINGS_DATUM'], 0, 10);
echo "<td>Krit= " . $cutKritDate . "<br>";
echo "Plan= " . $cutPlanDate . "<br>";
echo "Opdracht= " . $row['OPDRACHTID'] . "</td>";
}
?>
</tr>
</table>
I've tried something with jquery but it didn't work at all.
jquery:
$('.day').click(function()
{
$(this).nextUntil('tr.day').slideToggle(1000);
});
I have 5 rows and no idea what to do i have been struggeling a lot with this and can't find a solution.
Thanks in advance!!
At the moment I have the below script which auto generates the table names and row data automatically by looking at a sqlite table. So regardless of if you have 2 or 10 columns this script works.
At the moment the script outputs the results like this:
Output currently appears as a Row
I have tried altering the script so that it outputs the results like below. Can someone assist or guide me in the right direction to achieve this?
Is it possible to output the results of the query in the below format: going down in a column rather than across as a row ?
Output should appear as a Column
<?
$ED = $_GET['ED'];
$ID = $_GET['ID'];
$table_name = $_GET['table'];
?>
<table border="1">
<tr>
<td>
<table>
<?php // Display all sqlite column names for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
if ($table['name'] == "ID") {
echo "<tr><td>" . $table['name'] . "</td></tr>";
} else {
$table_name_header = ucwords(strtolower(str_replace('_', ' ', $table['name'])));
echo "<tr><td>" . $table_name_header . "</td></tr>";
}
}
?>
</table>
</td>
<td>
<table>
<?
// Display all sqlite data for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
$columns = array();
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
$columns[] = $table['name'];
}
// Display * from USERS
// $results = $db->query('SELECT * FROM ADMIN_LOGIN WHERE ID = "57"');
$results = $db->query('SELECT * FROM ' . $table_name . ' WHERE ID = "' . $ID . '"');
while ($row = $results->fetchArray()) {
// echo "<tr>";
$test = $row[0];
foreach ($columns as $col)
echo "<tr><td>" . $row[$col] . "</td></tr>";
}
// echo "</tr>";
?>
</table>
</td>
</tr>
</table>
Modifying the code to the below by putting the data into an combined array and then pulling it back via a loop it will display as required:
<?
// Display all sqlite data for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
$columns = array();
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
$columns[] = $table['name'];
}
// Display * from USERS
// $results = $db->query('SELECT * FROM ADMIN_LOGIN WHERE ID = "57"');
$results = $db->query('SELECT * FROM ' . $table_name . ' WHERE ID = "' . $ID . '"');
while ($row = $results->fetchArray()) {
// echo "<tr>";
$test = $row[0];
foreach ($columns as $col)
$column_data[] = $row[$col];
// echo "<tr><td>" . $row[$col] . "</td></tr>";
}
// echo "</tr>";
?>
<?
$array = $columns;
$array2 = $column_data;
$result = array_combine($array, $array2);
//print_r($result);
?><br><br>
<center>
<table border="0" cellpadding="2" cellspacing="2" color="#4B708D">
<thead>
<?
foreach($result as $key => $value) {
echo "<tr><td bgcolor='#c6d5e1'>$key</td><td bgcolor='#FFFFFF'>$value</td></tr>";
}
?>
</thead>
</table>
EDIT: This is what I am trying to achieve: http://i.imgur.com/KE9xx.png
I am trying to display the results from my database in two columns. I'm a bit new to PHP so I haven't the slightest clue on how to do this. Can anybody help me with this? Thanks in advance.
Here is my current code:
include('connect.db.php');
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM todo ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table width='415' cellpadding='0' cellspacing='0'>";
// set table headers
echo "<tr><td><img src='media/title_projectname.png' alt='Project Name' /></td>
<td><img src='media/title_status.png' alt='Status'/></td>
</tr>";
echo "<tr>
<td><div class='tpush'></div></td>
<td> </td>
</tr>"
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td><a href='records.php?id=" . $row->id . "'>" . $row->item . "</a></td>";
echo "<td>" . $row->priority . "</td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:
$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);
$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;
// Put results in an array
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
$i++;
}
//display results in a table of 2 columns
echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
<table>
<tr>
<td>ProjectName</td>
<td>Status</td>
<td>ProjectName</td>
<td>Status</td>
</tr>
<?php
while($row = $result->fetch_object()) {
echo "<tr>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "</tr>";
}
?>
</table>
This is the thing on picture. With a bit CSS you can manipulate the tds.
Your function should look similar to this:
$query = "SELECT *
FROM todo
ORDER BY id";
$result = $mysqli->query($query);
while($row = $result -> fetch_array()) {
$feedback .= "<tr>\n<td>" . $row['item'] . "</td><td>" . $row['priority'] . "</td>\n</tr>";
}
return $feedback;
Then, in your HTML have the <table> already setup and where you would normally insert your <td> and <tr> put <?php echo $feedback?> (where $feedback is the assumed variable on the HTML page that retrieves the $feedback from the function). This isn't a complete fix, your code is hard to read, but by starting here, you should be able to continue on the path filling in all the extra information you need for the table, including your CSS.