On one page that contains links to the database on my website, the page loads very slowly, is there any way to speed up the loading of that page?
I tried it on the server and when there were no visitors just import database and it immediately slowed down loading the page.
You can inspect the page via my website: https://translatesubtitles.com/browse_subtitle.php or check the page code:
I think it may be up to the database table to slow it down, you can look at the table code below:
<div class="row">
<table id="example" data-order='[[ 0, "desc" ]]' class="table table-striped " style="width:100%">
<thead>
<tr>
<th style="display: none">ID</th>
<th>Name</th>
<th>Language</th>
<th>Author</th>
</tr>
</thead>
<tbody>
<?php
include("my.php");
$query = "SELECT * FROM me order by id DESC";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td style="display: none"><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['language']; ?></td>
<td><?php echo $row['author']; ?></td>
</tr>
<?php }} ?>
</tbody>
</table>
</div>
Have you already added id as an index to the table? If not, adding one should help.
ALTER TABLE `me` ADD INDEX `id` (`id`)
You might also be able to cut down on the amount of data that's transferred by selecting only the columns that you need. E.g.
SELECT id, name, language, author FROM me ORDER BY id DESC
Related
I am looking for a tutorial on pagination that only associates with the dependent drop list. In other words, If I made a selection from a dependent drop list with items, {Beavers, Cubs, Scouts, Venturers, Rovers and Leaders}. It will display the associated records and I was able to do that with ajax. However, some of those items have more than 100 records, and I would like to add pagination based on what was selected and can only find the tutorial that has drop list based of total number of record, example {50, 100, 250, 500, 5000 etc}
I have been searching using,
pagination based on the result from a dependent drop list ,
dependent drop list with pagination,
dynamic pagination with a drop list etc
with PHP, MySQL, js and ajax with no success. I am fairly new with this and a tutorial will help me understand better, Can anyone tell me what to look for? thanks
code for AdminPanelFrontEnd.php
<?php
require_once('includes/header.php');
require_once('includes/connection.php');
if(isset($_SESSION['admin']))
{
$query = "SELECT * FROM members";
$result = mysqli_query($con, $query);
}
else
{
header("location:AdminPanelFrontEnd.php");
}
?>
<script type = "text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
<script>
function load_data(SectionID)
{
$.ajax({
type: "POST",
url: "AdminPanelBackEnd.php",
data: 'SectionID=' + SectionID,
success: function(data){
$("#details").html(data);
}
});
}
</script>
<div class="card-body">
<table class="table table-striped bg-info text-white" id="details" >
<tr>
Add Member
<div class="form-inline float-left ml-1">
<select class="form-control mb-2" id="section" onChange="load_data(this.value);">
<option value="null">Select Section</option>
<?php
$query = "SELECT * FROM sections ";
$result = mysqli_query($con, $query);
while($row=mysqli_fetch_assoc($result))
{
?>
<option value = "<?php echo $row["SectionID"]; ?>"> <?php echo $row["SectionName"]; ?> </option>
<?php
}
?>
</select>
</div>
</tr>
<tr class="bg-info text-white">
<td>Member Name</td>
<td>Member Email</td>
<td>Member Contact</td>
</tr>
</table>
</div>
code for AdminPanelBackEnd.php
<?php
require_once('includes/connection.php');
$query = "SELECT * FROM sections, members WHERE
sections.SectionID ='".$_POST["SectionID"]."' AND
sections.SectionID = members.SectionID ORDER BY MemberName
";
$result = mysqli_query($con, $query);
?>
<table class="text-center bg-info text-white">
<tr>
<td>Member Name</td>
<td>Member Email</td>
<td>Member Contact</td>
</tr>
<?php
while($row=mysqli_fetch_assoc($result))
{
$Member = $row['MemberName'];
$MemberEmail= $row['Email'];
$MemberContact = $row['Contact'];
?>
<tr class="bg-light text-dark">
<td><?php echo $Member ?></td>
<td><?php echo $MemberEmail ?></td>
<td><?php echo $MemberContact ?></td>
</tr>
<?php
}
?>
</table>
As you said, you already did the ajax, so the query string should be included in the paging navigation.
The link for next page should look like ?page=2&category=some_id
The ajax call will replace the whole content and the navigation.
There are libraries supporting that on every framework.
errors i get
I have a table that contains details one of this being emails. When i click a link i have outlook mail opening but i want to take the email of that row in the table and put it into the 'to' part of the email. Below i have code for what i am currently doing.
the code below displays the data from my database in a table format
<table class="table table-striped custab">
<thead>
<tr>
<th> </th>
<th>Booking ID</th>
<th> Name</th>
<th>Email</th>
<th>Date</th>
<th>time</th>
<th>No. of guests</th>
<th>Booking Reason</th>
<th>Comments</th>
<th width="110" class="ac">Approved?</th>
</tr>
<thead>
<!-- php function to only select the bookings that have not yet been approved/rejected -->
<?php
include 'config.php';
$select = "SELECT * FROM `booking` WHERE `status`IS NULL ";
$result = $conn->query($select);
while($row = $result->fetch_assoc()){
?>
<tr>
<td><input type="checkbox" class="checkbox" /></td>
<td><?php echo $row['customer_ID'] ?></td>
<td><?php echo $row['Name'] ?></td>
<td><?php echo $row['Email'] ?></td>
<td><?php echo $row['booking_date'] ?></td>
<td><?php echo $row['booking_time'] ?></td>
<td><?php echo $row['attendee_no'] ?></td>
<td><?php echo $row['booking_reason'] ?></td>
<td><?php echo $row['comments'] ?></td>
<td>
Email this Codesnippet</a>
</td>
</tr>
<?php
}
?>
</table>
The function below gets the pop up to display for outlook mail
<script type="text/javascript"> TriggerOutlook(Email)
{
var $to = 'Email';
var body = "your booking has been approved";
<!-- var body = escape(window.document.title + String.fromCharCode(13)+ window.location.href); --->
var subject = "Your booking request";
window.location.href = "mailto:?body="+body+"&to="+$to+"&subject="+subject;
}
</script>
if i put in an email manually into the var $to = the outlook pop up works however if i try to take the email from the table it doesnt, can anyone help me out to identity where i am going wrong? Thanks!
1 You don't need the PHP $ declaration for variables, thus:
var $to = 'Email';
should be:
var to = 'Email';
more descriptive variables could make future updates easier:
var toAddr = 'Email';
2 Your JavaScript function should be preceded with the function tag
<script type="text/javascript"> TriggerOutlook(Email)
{
changes to:
<script type="text/javascript">
function TriggerOutlook(Email){
3 Use a button rather than link
Replace
Email this Codesnippet</a>
With
<button
onclick="TriggerOutlook(<?php echo $row['Email'];?>)"
value="submit"
>Email this Codesnippet</button>
i created an appointments table where user1 would add appointments and user2 would accept/reject the appointment. now my problem is when the accept and reject are clicked it is displayed in the table but it is not updated in the db I've did a lot of research over this but hit a road block after road block, so I really hope your help in this, many thanks!
Here is an image of my appointments table:
Here is my code:
<table cellpadding="0" cellspacing="0" border="0" class="table table-condensed" id="example">
<thead>
<tr>
<th>appoinment ID</th>
<th>Date</th>
<th>time</th>
<th>teacher</th>
<th>parent</th>
<th> accept/reject </th>
<th>state</th>
</tr>
</thead>
<tbody>
<?php
$query=mysqli_query($conn, "select * from `app` left join `par` on par.par_id=app.par_id
left join `tea` on tea.tea_id=app.tea_id
ORDER BY app_id DESC");
if($query === false)
{
throw new Exception(mysqli_error($conn));
}
while($row=mysqli_fetch_array($query))
{
$ann_id=$row['app_id'];
$date=$row['date'];
$msg=$row['time'];
$username = $row['username'];
$username = $row['p_username'];
?>
<tr>
<td><?php echo $row['app_id'] ?></td>
<td> <?php echo date('j/m/y',strtotime($row['date'])); ?></td>
<td><?php echo $row['time'] ?></td>
<td><?php echo $row['p_username'] ?></td>
<td><?php echo $row['username'] ?></td>
<td>
reject
accept
</td>
<td>
<div class="chgtext">PENDING</div>
</td>
</tr>
<?php
if (isset($_GET['state'], $_GET['app_id'])) {
$stmt = mysqli_prepare($conn, "UPDATE app SET state = ? WHERE app_id = ?");
mysqli_stmt_bind_param($stmt, "sd", $_GET['state'], $_GET['s.app_id']);
$stmt->execute();
$stmt->close();
}
}
?>
</tbody>
</table>
</div>
</form>
You have an error there. You are using $_GET request with setting it in url. Change your html tag like below
reject
accept <!-- I assumed that column names in database were app_id and state
And in php code
if (isset($_GET['state'], $_GET['app_id'])) {
$stmt = mysqli_prepare($conn, "UPDATE app SET state = ? WHERE app_id = ?");
mysqli_stmt_bind_param($stmt, "sd", $_GET['state'], $_GET['app_id']);
$stmt->execute();
$stmt->close();
}
}
I hope it will help you. If there is any problem then kindly tell me
I wrote this part of code:
<table class="table table-hover">
<thead>
<tr>
<th>Status</th>
<th>MPK</th>
<th>Full Name</th>
<th>Phone Number</th>
<th>Project Start Date</th>
<th>Project End Sales Date</th>
<th>Project Installation Date</th>
<th>Project End Date</th>
<th>General Manager</th>
<th>IT Project Manager</th>
<th>Notes</th>
</tr>
</thead>
<?php
$file = '/var/lib/jenkins/workspace/Update_JSON/terminarz.json';
$fp_terminarz = fopen($file, 'r');
$toecho = fread($fp_terminarz, filesize($file));
$data = json_decode($toecho, true);
fclose($fp_terminarz);
foreach($data as $key=>$value){ ?>
<tbody>
<tr>
<td>Opening process</td>
<td><?php echo $value["MPK"]; ?></td>
<td><?php echo $value["Restaurant Name"]; ?></td>
<td><?php echo $value["Phone Number"]; ?></td>
<td><?php echo $value["Project Start Date"]; ?></td>
<td><?php echo $value["Project End Sales Date"]; ?></td>
<td><?php echo $value["Project Installation Date"]; ?></td>
<td><?php echo $value["Project End Date"]; ?></td>
<td><?php echo $value["Restaurant Manager"]; ?></td>
<td><?php echo $value["IT Project Manager"]; ?></td>
<td><?php echo $value["Notes"]; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
I show JSON content in HTML table using PHP script.
My problem is - I want to have possibility to edit any row (one at a time) and after saving changes update file terminarz.json.
What is the best way to do this? I was thinking about js scripts, which would change css style of row and show Edit button near row (after pointing it with mouse cursor).
Is this good approach to this task? Or how should I do this? I would appreciate any advice.
For the UI/UX part:
Why not have an edit icon in the last column of table with a column heading Edit.
Have hidden input fields with proper names and value pre-filled.
On click of the edit icon, simply hide the data and show the input fields, so the user can edit the data. Also, switch the edit icon to save and also give an option to cancel out.
Something like this: http://nathancahill.github.io/table-edits/
For updating the data:
$jsonString = file_get_contents('terminarz.json');
$data = json_decode($jsonString, true);
foreach ($data as $key => $entry) {
if ($entry['id'] == $updated_row_id) {
$data[$key]['name'] = "value";
}
}
And then save it back using:
$newJsonString = json_encode($data);
file_put_contents('terminarz.json', $newJsonString);
Its a good approach just do one more thing wrap table in form and make input hidden element for each field when you edit a single row then update that hidden element with new entry and at last submit the form which contain all the entries and on server side catch that data and convert into json and save it
I was try do view in php. But first data now showing in output. Remaining data displayed. how can i get all data in table.
<html>
<body>
<?php
include('connect.php');
$select=mysql_query("SELECT id,username FROM user order by id");
$i=1;
while($userrow=mysql_fetch_array($select))
{
$id=$userrow['id'];
$name=$userrow['username'];
?>
<table width="600" border="1" cellpadding="1" cellspaceing="1" >
<tr >
<th class="active">ID</th>
<th class="active">Name</th>
<tr>
<?php
while ($employee=mysql_fetch_assoc($select))
{
echo "<tr>";
echo "<td>".$employee['id']."</td>";
echo "<td>".$employee['username']."</td>";
}
}
?>
</table>
</body>
</html>
There are multiple issues:
Table rows had not been closed (</tr>)
Usage of mysql_* functions is deprecated in PHP 5 and removed in PHP 7; if you're writing new code, you should not be using mysql_* functions. Instead, use mysqli_* functions or the PDO library.
There were multiple for-loops to get records, but it would create new tables each iteration.
The table attribute "cellspacing" was misspelled.
The results object where they are stored can either be a resource or false.
Given these, I've made the following changes that will fix the above problems:
<?php
// I've commented out the connect.php file inclusion to highlight the change to how
// you'd connect to the MySQL database using mysqli_* instead of mysql_*. I'm guessing
// as to the contents of connect.php, but this should still highlight what needs to be
// done
// include('connect.php');
$db_connection = mysqli_connect('host', 'user', 'pass', 'database_name');
$employee_results = mysqli_query($db_connection, "SELECT id,username FROM user ORDER BY id");
?>
<html>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th class="active">ID</th>
<th class="active">Name</th>
</tr>
<?php if ($employee_results === false): ?>
<tr>
<td colspan="2">No users</td>
</tr>
<?php else: ?>
<?php while ($employee = mysqli_fetch_assoc($employee_results)): ?>
<tr>
<td><?= $employee['id'] ?></td>
<td><?= $employee['username'] ?></td>
</tr>
<?php endwhile ?>
<?php endif ?>
</table>
</body>
</html>
<html>
<body>
<?php
include('connect.php');
$select=mysql_query("SELECT id,username FROM user order by id");
$i=1; ?>
<table width="600" border="1" cellpadding="1" cellspaceing="1" >
<tr>
<th class="active">ID</th>
<th class="active">Name</th></tr>
<?php while($userrow=mysql_fetch_array($select))
{
$id=$userrow['id'];
$name=$userrow['username'];
?>
<?php
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>".$name."</td></tr>";
}
?>
</table>
</body>
</html>`