Dynamically Updating PHP HTML Table with AJAX - php

I have a PHP HTML table loads in data from a SQL database. How would I be able to update that data using an onclick button function?
Basically I want the whole data to be updated so instead of the SQL query looking for the month of January, I want to display data from March.
I have setup php files to look for different datasets, im just not sure on how to use Ajax to refresh the table.
Here is a snippet of what im using to populate the HTML table:
echo "<table class='content-table is-striped'>";
echo "<tr>";
echo "<th>Person</th>";
echo "<th>Weight (Kg)</th>";
echo "<th>Date</th>";
echo "</tr>";
$month = date('m');
if ($month == "01") {
$sql = "SELECT * from WeightTracker WHERE person = 'Ayush' AND date_month = 'January' ORDER BY weight_date DESC";
$result = mysqli_query($conn, $sql);
$x = 0;
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
if ($x % 2 == 0) {
$bgcolour = oddbg;
}
else {
$bgcolour = evenbg;
}
echo "<tbody><tr class='$bgcolour'><td>". $row["person"] ."</td><td>". $row["kg"] ."</td><td>". $row["weight_date"] ."</td></tr></tbody>";
$x++;
}
echo "</table>";
}
else {
echo "0 results";
}
}
Any ideas would be awesome!

Related

Create dynamic table in php with mysql data

I would to like to create a dynamic table that the rows and columns of the table depends on the data in mysql data. Please see below my codes.
<?php
require "connect/db.php";
$query = mysqli_query($mysqli, "SELECT COUNT(level) level, shelf_no, bin_id FROM location_bin WHERE rack_id =1 GROUP BY shelf_no");
while($res=mysqli_fetch_array($query)){
$col = $res['level']; //col
$row = $res['shelf_no']; //rows
echo "<table border='1'>";
$i = 0;
while ($i < $col){
if ($i==$col){
echo "<tr>";
}
echo "<td>".$res['bin_id']."</td>";
$i++;
}
echo "</tr>";
echo "</table>";
}
?>
What I want is to display A-1-01 up to A-1-06 in the first row then A-2-01 to A-2-03 on the second row. Note that the data is dynamic.
Can you try this code . It works. change db.php path as your system path.
require "db.php";
$query = mysqli_query($conn, 'SELECT level, shelf_no, bin_id FROM location_bin ORDER BY shelf_no asc, level asc ');
echo "<table border='1'>";
echo "<tr>";
$i=0;
while($res = mysqli_fetch_assoc($query)) {
$row=$res['shelf_no'];
if ($i < $row){
$i=$row;
echo "</tr><tr>";
}
echo "<td>".$res['bin_id']."</td>";
}
echo "</table>";
?>

PHP incomplete result table from sql query

I am new to web development and starting learning about CRUD.
My question is that while I successfully show the table listing 3 product on 1 row, that on the second row the product no 4 are missing and skipping to the product no 5 and keep missing every last 3 row.
function getData(){
global $connect;
$query = "SELECT id, name, category, price, image, info FROM product_data";
$results = mysqli_query($connect, $query) or die(mysql_error());
echo "<table border = 0 >";
$x=1;
echo "<tr class='homepagetable'>";
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
if($x<=3){
$x = $x + 1;
extract($row);
echo "<td class='homepagetable'>";
echo "<a href=itemdetails.php?id=$id>";
echo '<img src=img/' . $image . ' style="max-width:220px;max-height:240px;width:220px;height:240px;"></img><br/>';
echo '<div class="truncate">'. $name .'</div><br/>';
echo "</a>";
echo 'Rp.'.$price .'<br/>';
echo "</td>";
} else {
$x=1;
echo "</tr><tr>";
}
}
echo "</table>";
}
Thanks in advance!
Your problem is that you have a wrong condition here:
if($x <=3)... else{...}
Change the if/else to this:
if($x <3){$x++;}
//...do something
if($x == 3){
$x = 1;
//Close and open tr
}
And you need to close the <img> tag, and the last <tr> tag outside of the loop
Perhaps you can write it like this:
function getData()
{
global $connect;
$query = 'SELECT id, name, category, price, image, info FROM product_data';
$result = mysqli_query($connect, $query) or die(mysql_error());
echo '<table border="0">';
$x=0;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
if($x % 3 == 0)
{
// number of articles is a multiple of 3 - so close the row
if($x) echo '</tr>'; // but only if there was at least 1 article
echo '<tr class="homepagetable">';
}
$x ++;
echo '<td class="homepagetable"><a href="itemdetails.php?id='.$row['id'].'">
<img src="img/'.$row['image'].'" style="max-width:220px;max-height:240px;width:220px;height:240px;"><br/>
<div class="truncate">'.$row['name'].'</div><br/>
</a>Rp. '.$row['price'].'<br/>
</td>';
}
if($x) echo '</tr>'; // only close the row if there was at least 1 article
echo '</table>';
}

How to make a PHP page have two "column" regions?

Basically I'm doing digital signage and I'm trying to get names to be pulled from a MySQL database to a PHP page. Right now its all centered in one column, but I want the results to be in two columns side by side. How can I do this?
$sql = "SELECT * FROM donor WHERE DonationAmount = 5000 AND Category = '1' or DonationAmount = 5000 AND Category IS NULL ORDER BY LastName ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
// test if the DisplayName field is empty or not
if(empty($row['DisplayName']))
{
// it's empty!
if(empty($row['FirstName'])){
echo $row['LastName']. "<br>";
}
else{
echo $row["LastName"]. ", " . $row["FirstName"]. "<br>";
}
}else{
// Do stuff with the field
echo $row["DisplayName"]. "<br>";
}
}
} else {
}
Basically I want this data to be spread across two columns instead of 1 single page.
output the strings like this:
echo "<span style=\"width:50%;float:left;\">".$row['LastName']."</span>";
do not forget to remove <br /> from each output
You can use tables, and count the rows to determine if you need to start a new table row.
$i = 0;
$total_rows = $result->num_rows;
echo "<table><tr>";
while($row = mysqli_fetch_assoc($result)) {
// test if the DisplayName field is empty or not
echo "<td>";
if(empty($row['DisplayName']))
{
// it's empty!
if(empty($row['FirstName'])){
echo $row['LastName'];
}
else{
echo $row["LastName"]. ", " . $row["FirstName"];
}
}else{
// Do stuff with the field
echo $row["DisplayName"]. "";
}
echo "</td>";
$i++;
if($i % 2 == 0 && $i != $total_rows) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
if your content is in <div id="myDiv"> use this JS function and call it after the content loads
function splitValues() {
var output = "";
var names = document.getElementById('myDiv').innerHTML.split("<br>");
for(var i in names) {
output += "<span style=\"width:50%;float:left;display:inline-block;text-align:center;\">"+names[i]+"</span>";
}
document.getElementById('myDiv').innerHTML = output;
}

Displaying data horizontally using php and mysql

Hi I'm attempting to display data retrieved from a mysql table horizontally in an html table using php. The code below works well except for the fact that it leaves out the first record (starts at the second record) in my database. I'm sure it has something to do with the counter but I can't seem to figure out how to get it to stop doing this. If anyone can point out my error I'd really appreciate it!
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
$i = 0;
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
if ($i==0) {
echo "<tr>\n";
}
echo "\t<td align=\center\">$first_name</td>\n";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}//end while loop
if ($i > 0) {
for (;$i < $items; $i++) {
echo "<td> </td>\n";
}
echo '</tr>';
}//end ($i>0) if
echo '</table>';
}else {
echo 'no records found';
}
try and remove the 1st
$row = mysql_fetch_array($result);
you are calling it twice, that's why it skips 1 row in your while loop
try this simpler.
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
echo "<tr>";
for ($i=0 ; $i <= $items ;$i++) {
echo "<td align='center'>".$first_name."</td>";
}
}//end while loop
echo "</tr>";
echo '</table>';
}else{ echo 'no records found'; }
I have run into this issue before. Try the do while loop instead. Example
do {
// code
} while($row = mysql_fetch_array($result)); //end while loop
$row = mysql_fetch_array($result);
1) remove this line of code from ur scripts
2) only use while loop code instead.

PHP Insert Multidimensional Array into mysql

i try to store the booking booths into database based on user selection, there are 10 check boxes for each booths and user can choose which day they want to reserve booths. For each check box has it own field in database, if user choose booth A01, D1 and D2, when he press reserve button, it will insert value into D1 and D2. But I dont know how to get the checked checkbox value to store in database
my booth interface
http://i.imgur.com/umYcI.gif
my table structure
http://i.imgur.com/vKh6R.gif
My coding
<?php
session_start();
if ( !isset($_SESSION['AUTHORIZED_USERNAME']) || empty($_SESSION['AUTHORIZED_USERNAME']) ) {
header("location:index.php");
}else{
$user=$_SESSION['AUTHORIZED_USERNAME'];
}
include('db.php');
if($_REQUEST){
$id = $_REQUEST['search_category_id'];
$query2 = mysql_query("SELECT filenameBig, filename, url FROM eventinfo where eventID ='$id'");
$row = mysql_fetch_array($query2, MYSQL_ASSOC);
if($id == -1)
{
echo "<style type='text/css'>#btn_submit{visibility:hidden}</style>";
}
else{
/*echo "<a href='{$row['url']}'>Click me!</a>";*/
echo "<p><br><img src='{$row['filename']}' alt='' /></p>";
echo "<p></p>";
echo "<p align='right'><a href='$row[filenameBig]' target='_blank'>Click to view large image</a></p>";
echo "<hr size='1'>";
echo "<div style='padding-left:4px;' align='left'><strong>Booths Listing</strong>";
echo "<p></p>";
$query = "select boothAlias, totalDay from booths, eventinfo where booths.eventID=eventinfo.eventID && booths.eventID = ".$id."";
$_SESSION['EVENT_ID']=$id;
$result = mysql_query($query);
$result2= mysql_query($query);
echo "<table border='0' style='width:400px;table-layout:fixed' >";
$rows2 = mysql_fetch_array($result);
$Day=$rows2['totalDay'];
echo "<table>";
for ($day = 0; $day <= $Day; ++$day) {
if($day==0){
echo "<th>Booth</th>";
}else{
echo "<th>D".$day."</th>";
}
}
while($rows = mysql_fetch_array($result2)){
$boothAlias=$rows['boothAlias'];
$totalDay=$rows['totalDay'];
echo "<tr><td>$boothAlias</td>";
for ($day2 = 1; $day2 <= $totalDay; ++$day2) {
echo "<td><input name='day2[]' type='checkbox' value='$day2' /></td>";
}
echo "</tr>";
}
echo "</table>";
}
}
?>
I think that SET type would be good solution for this.
http://dev.mysql.com/doc/refman/5.0/en/set.html

Categories