Showing Mysql table data in html form with php - php

I'm showing data from Mysql table with php inside HTML table, but i need 2 more things to accomplish to the table:
1- How to Alternate colors for table rows to use 4 different css classes, I am using now class='success' i have 3 more that i want to use and each one should apply to each table row, how to do it? any simple example like a loop or something?
2- Data showing from the oldest record in the table to the latest one, and i want to show the reverse, so the last record shows first in the html table.
My code for this table:
<?php echo "<table class='table'>
<thead>
<tr>
<th>Order#</th>
<th>Name</th>
<th>Total</th>
<th>Submitted On</th>
<th>Status</th>
</tr> </thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tr class='success'>";
echo "<td>" . $row['lite_order_id'] . "</td>";
echo "<td>" . $row['lite_item_name'] . "</td>";
echo "<td>" . $row['lite_email'] . "</td>";
echo "<td>" . $row['lite_country'] . "</td>";
echo "<td>" . $row['lite_order_total'] . "</td>";
echo "</tr>"; }
echo "</table>"; ?>

1 - Use id attribute to style individual elements. Check it out here
2 - In your MySQL query, use ORDER BY:
(I presume you have an id column here)
$query = "SELECT * FROM `yourtable` ORDER BY `id` ASC";
See here

To get reverse records you can use mysql ORDER BY clause or PHP function array_reverse()

Related

Populate a drop down box from sql query in a table and strip symbols

At present I have this code
$id = (isset($_GET['id']) ? $_GET['id'] : '');
$result = mysqli_query($con,"SELECT * FROM garage WHERE PlayerUID = '$id'
ORDER BY DateStored");
$row_cnt = $result->num_rows;
printf("Player has %d Vehicles stored.\n <br>", $row_cnt);
echo "<br>";
echo "<table border='1'>
<tr>
<th>Player Name</th>
<th>Vehicle</th>
<th>Date Stored</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='https://www.battlemetrics.com/rcon/players?filter%5Bsearch%5D=%22".$row['PlayerUID']."%22&filter%5Bservers%5D=2191805%2C2315320%2C2384537&sort=score&showServers=true' target='_blank'>" . $row['Name'] . "
</td>";
echo "<td><a href='https://www.google.co.uk/search?q=Dayz+Epoch+".$row['DisplayName']."' target='_blank'>" . $row['DisplayName'] . "
</td>";
echo "<td>" . $row['DateStored'] . "</td>";
echo "<td><a href='deletegarage.php?del=$row[id]' Onclick='return ConfirmDelete()'>Delete</a>";
echo "</tr>";
}
echo "</table>";
Which gives me this output :
Current output
What I would like to achieve is adding another table after Vehicle field which displays the information from the database in the "Inventory" field into a dropdown list, but the current information that is in a single row looks like this:
[[[],[]],[["CinderBlocks","cinder_door_kit","cinder_garage_kit","full_cinder_wall_kit","ItemComboLock","metal_floor_kit","ItemVault","PartVRotor","ItemPole","PartGeneric","MortarBucket","ItemSledgeHandle","ItemSledgeHead","ItemTankTrap","plot_pole_kit"],[61,3,5,25,4,11,4,1,22,1,15,2,2,21,1]],[[],[]]]
so I would like to try and display each item in that list without symbols and on different lines within that dropbox if it's at all possible.
I would like it to look something like this :
What i need the dropdown to look like
I hope this provides you with enough information as to what I need and would appreciate any support!

How to add ajax pagination?

I am using this
PHP Ajax example from w3schools.This is working fine but it's not an efficient solution when the record list will be huge.So i want to add pagination to handle large records.How can i add pagination (Ajax pagination so that i can avoid page reloading) with this?
Here is my code:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE type_no = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
What I normally do is use the LIMIT feature in mysql. When I look at your example I would assume that the sql statement would only return one result.
Lets assume you had a table called user with many users if you had a query like this:
SELECT * FROM user
You would get all the users back in one query. This is not practical for most uses. The most elegant solution in my opinion is the LIMIT feature.
For example if I wanted to see results in a range:
SELECT * FROM user LIMIT 0,50 #return first 50 results - records 0 to 49
SELECT * FROM user LIMIT 50,50 #return the next 50 results - records 50 to 99
LIMIT syntax looks like this LIMIT <offset>,<count> Where offset is the starting result (starts at 0) and count is the number of results you want back.
So lets assume you had pages size of 50 and and were on the 5th page you would want:
LIMIT 100,25
Handling this at the database level is the simplest and quickest method.

cannot delete row from database using php

I am unable to delete any record from database. I cannot find any error in this.
I have deleted records from another table by just changing a little bit but here it's not working. Below is the code to apply the delete query.The name of table is from where I want to delete records but its not happening here.
deleteSupplier.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_kiln");
$id1 = $_GET['id1'];
$query0 = "DELETE FROM tbl_supplier WHERE sup_id='$id1'";
if(mysql_query($query0)){
echo "<script>window.open('supplier_connect.php','_self')</script>";
}
else{
echo "Not deleted";
}
?>
This is the file where I fetch data from database and have delete button against each record. When I click on the button it does not delete record and show the error message. I can't find any error I think there is a logical error in this code. Please help.
supplier_connect.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_kiln");
$query = "Select * from tbl_supplier";
$run = mysql_query($query);
echo "<table border='1'>
<tr>
<th>Supplier Id</th>
<th>Name</th>
<th>Contact Number</th>
<th>Quotation </th>
<th>Remove</th>
</tr>";
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td>" . $row['sup_id'] . "</td>";
echo "<td>" . $row['sup_name'] . "</td>";
echo "<td>" . $row['sup_contact'] . "</td>";
echo "<td>" .$row['sup_quotation']. "</th>";
echo '<td>Delete</td>';
echo "</tr>";
}
echo "</table>";
?>
You probably have the ID of the supplier referencing to another table, and upon DELETE, no action is set. You need to set the option 'upon DELETE: Cascade or SET NULL.' I'd go for SET NULL, else your whole reference record will be deleted as well.
EDIT: check the relationship between your contacts and quotations, see where your supplierid is going to.

creating a chart (line or bar) from php and mysql

i am new somehow to PHP and mysql,
I've simple database that receives the values from php form as id, date, time, max, today, tomorrow... i can insert into the table, all ok, i can also fetch the data to a php page using the below code, all ok, i can see the table of the values inserted in the db, all ok...
my need is to crate a simple line or bar chart that shows the max (which is static number each day) and the today number will be the variable that will be defined by the chart, so if it is a line it will show better that each day where is the number from the max... forget about tomorrow, it is not important...
here is my code of the display of the table of the page...
i want the simplest way to complete this, imagine only i have one column (today) that is changed each while or each day, so i want to show it in a graph against the max number... so i think the line chart will be suite best...
<?php
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT id, date, time, max, today, tomorrow, FROM testtable1");
echo "<table border='1'>
<tr>
<th>id</th>
<th>date</th>
<th>time</th>
<th>max</th>
<th>today</th>
<th>tomorrow</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td>" . $row['max'] . "</td>";
echo "<td>" . $row['today'] . "</td>";
echo "<td>" . $row['tomorrow'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I recommend using something like Google Visualizations for line charts. Visualizations are easy to implement and you can write them directly on the page see their example Line charts

Can you store selected sql query values in the url and then retrive them with $_GET?

I'm trying to make it so that when a users clicks one of the rows,it will take them to a new page whose link is given as the value of the row they selected and then retrieve the value with $_GET["timesub"].
Anyone know how to do this?
mysql_select_db("RRRC", $con);
$result = mysql_query("SELECT * FROM mainreq WHERE roomnum=$loc");
echo "<table border='1'>
<tr>
<th> Submitted </th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> . $row['timesub'] . </td>";
echo "</tr>";
}
echo "</table>";
Assuming that $row['timesub'] identifies a row in your data set (I doubt it), just fix your echo instruction as:
echo "<td>" . $row['timesub'] . "</td>";
Escaping the html quotes properly.
echo "<td><a href='roomdata.php?timestamp=".$row['timesub']."'>".$row['timesub']."</a></td>";
Close the outer " before the . concatenator, replace the inner " with '
A good practice is to use the row's primary key to reference your get query; but yes - this can be done.
All you have to do is store the get data into a sanitized variable, and perform the required SQL lookup / data display.
EX:
$roomnum=mysql_real_escape_string(preg_replace("/[^a-zA-Z0-9]+/", "", $_GET['roomnum']));
Now, given that "roomnum" is your primary key just look it up and display:
$result = mysql_query("SELECT * FROM mainreq WHERE roomnum='$roomnum'");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> . $row['timesub'] . </td>";
echo "</tr>";
}
echo "</table>";

Categories