How to create a dynamic php page - php

So it's actually hard to explain what I mean.
So I have this list here. Its pulls data from the db and then displays it in a DIV on my webpage.
I want it to only show the first 4 listing. Then after 4 seconds. I want it to show the next 4 listening and keep revolving like that infinitely. If someone could point me in the direction to be able to do that. Also, It needs to be updating dynamically every 4 seconds. Not when the user hits refresh.
<?php session_start(); ?>
<head>
<link rel="stylesheet" type="text/css" href="tablecss.css">
<style type="text/css">
.extra {
position: relative;
top: 50px;
}
body {
overflow: hidden;
}
</style>
</head>
<body>
<?php
$con = mysqli_connect('##', '##', '##', '##');
if(mysqli_connect_errno()) {
echo 'Failed to Connect to MySQL' . mysqli_connect_errno();
}
$user = $_SESSION['username'];
$result = mysqli_query($con, "SELECT * FROM corporations");
echo "<table id='tb'border='1'>
<tr>
<th>Name</th>
<th>Owner</th>
<th>Last Price</th>
<th>Volume</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name']. "</td>";
echo "<br>";
echo "<td>" . $row['owner'] . "</td>";
echo "<br>";
echo "<td>" .'$'. $row['shareValue'] . "</td>";
echo "<br>";
echo "<td>" . $row['publicShares'] . "</td>";
echo "<br>";
}
mysqli_close($con);
?>
</div>
</body>

What you can do is javascript, and use ajax. to load it every four seconds you can use setInterval(http://www.w3schools.com/jsref/met_win_setinterval.asp), but for me its better if you use jQuery load more data on scroll.
to limit output use the limit of mysql.
see this article: http://www.mysqltutorial.org/mysql-limit.aspx

Related

How to update data in a table using dropdownlist box in PHP?

Im trying to make an orders table in PHP where i can choose in a dropdownlist box the driver and the truck to each order from the existing sql db.
i managed to create the dropdownlist inside the table but i dont know how to update the db.
im trying using isset function but im probably not doing it right.
here is the code i made + a screenshot:
screen
<!DOCTYPE html>
<html>
<head>
<title>Orders</title>
<link rel="stylesheet" type="text/css" href="table-general.css">
<style type="text/css">
body
{
font-family: Arial, Verdana, sans-serif;
font-size: 90%;
color: #666;
background-color: #f8f8f8;
}
</style>
</head>
<body>
<h1>Orders</h1>
Done</br></br>
<table class="general">
<tr class="head">
<th>Order_ID</th>
<th>Customer_ID</th>
<th>Driver_ID</th>
<th>Truck_ID</th>
<th>Date</th>
<th>Project_Name</th>
<th>Project_Place</th>
<th>Amount</th>
</tr>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("login");
$result = mysql_query("select * from orders_table") or die("Failed".mysql_error());
$result2 = mysql_query("select * from trucks_table") or die("Failed".mysql_error());
if(mysql_num_rows($result2))
{
$select= '<select name="select">';
while($record2=mysql_fetch_array($result2))
{
$select.='<option value="'.$record2['TruckID'].'">'.$record2['TruckID'].'</option>';
}
}
$select.='</select>';
while($record = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $record['Order_ID'] . "</td>";
echo "<td>" . $record['Customer_ID'] . "</td>";
echo "<td>" . $record['Driver_ID'] . "</td>";
echo "<td>" . $select . "</td>";
if(isset($_POST['select']))
{
$t=$_POST['select'];
$sql = mysql_query("update orders_table set TruckID='$t' where Order_ID='".$record['Order_ID']."' ");
}
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Project_Name'] . "</td>";
echo "<td>" . $record['Project_Place'] . "</td>";
echo "<td>" . $record['Amount'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
You probably need to learn something more about HTTP FORMS, how to send data from client to the server with POST or GET and how to use this data on the server with PHP.
see tutorials like these:
http://www.w3schools.com/php/php_forms.asp
In short - to send data from HTML page to the server, you need to wrap the inputs, selects, textareas in
<form action="url"> </form>
where url is pointing to your server php script, where your handling the form data
then you need to include
<input type="Submit">
which will generate a button on which when the user clicks the data are sent back to the server

styling mysql connect page

does anybody know how to style the 'echo' tags on my page? i am pulling from a mysql table, but cannot figure out how to style the individual rows. any help would be appreciated. here is my code:
<?
$con=mysqli_connect("localhost","ameriex9_eric","fitness2003","ameriex9_blog");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM blog_entries ORDER BY date DESC");
while($row = mysqli_fetch_array($result))
{
echo $row['name'];
echo "<br>" ;
echo $row['date'] ;
echo "<br>";
echo $row['title'];
echo "<br>";
echo $row['blog'];
echo "<br>";
echo $row['pics'];
echo "<hr>";
}
mysqli_close($con);
?>
You can style the individual rows like this, for example:
echo "<span class=\"myclass\">{$row['name']}</span>";
where .myClass can be a class somewhere in your CSS file.
This would work too:
echo '<span class="myClass">Name' . $row['name'] . '</span><br>';
Inline styles is possible too, though being more messier:
echo '<span style="color:red; font-size:12px;">Name' . $row['name'] . '</span><br>';
We can add a small style rule
<style scoped>
table,td{ border:1px solid #f4f4f4; border-collapse:collapse; padding:10px; }
</style>
and slightly modify your code around while loop like this...
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>".$row['name']."</td><td>".$row['date']."</td><td>".$row['blog']."</td><td>".$row['pics']."</td></tr>";
}
echo "</table>";

Style 1 <td> in a table with 2 <td>s

I have a table that gets it´s content from a database, and I want to make the text in the column to the right align-right. I know how to do it when its HTML and the content is in the code, but I can't get it to work when the content is outputted from a database.
Down here you can see my code for the table: (in the collumn "ingredienser", I want to set the text-align to right.
<?php
$con = mysql_connect("****","****","****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("paj", $con);
$result = mysql_query("SELECT * FROM meny");
echo "<div id='menyer'>";
echo "<table border='1'>
<tr>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<div class='mall" . $row['mall'] . "'>";
echo "<tr>";
echo "<td>" . $row['namn'] . "</td>";
echo "<td>" . $row['ingredienser'] . "'</td>";
echo "</tr>";
Just add some CSS above your php code
<style>
table td {
text-align: right;
}
</style>
The above will right align text inside table cells
echo "<td><div align='right'>" . $row['ingredienser'] . "'</div></td>";
if this is the extent of your table, then you can definitely just use the following css:
.mall{
text-align:right;
}
I would suggest putting it in your external css file, but if you don't have one, then put it in a style tag inside of your body or table tag, depending on what kind of scope you want it to have like this:
<style>
.mall{
text-align:right;
}
</style>
set align="right" to your tr
while($row = mysql_fetch_array($result))
{
echo "<div class='mall" . $row['mall'] . "'>";
echo "<tr align='right'>";
echo "<td>" . $row['namn'] . "</td>";
echo "<td>" . $row['ingredienser'] . "'</td>";
echo "</tr>";
Just add styling to that one particular cell, it will apply to the entire column as it loops. As such,
echo "<td style=\"text-align:right;\">" . $row['ingredienser'] . "'</td>";
If you align the Table TD to text-align:right, EVERY single cell will be effected.

Deleting Checked box data in Mysql Table from form

I am working on an inventory program and I need help on the following:
I have a table that works through selecting data from my Database and showing this in a table.
I now need to make it possible for a user to be able to delete bad/faulty records from the form ( Ofcourse u don't want to everytime have to go in the database itself to remove a record, would be stupid)
My code:
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Inventory Grid.html</title>
<meta charset = "UTF-8" />
<style type = "text/css">
table, td, th {
border: 1px solid black;
}
</style>
</thead>
<tbody>
<?php
$con = mysqli_connect("localhost", "root", "admin", "inventarisdb");
// Check connection
if ( mysqli_connect_errno() ) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query( $con, "SELECT * FROM BCD" );
echo "<table border='0'>
<tr>
<th>Categorie</th>
<th>SerieNummer</th>
<th>MacAdress</th>
<th>ProductCode</th>
<th>Prijs</th>
<th>RekNummer</th>
<th>PaletNummer</th>
<th>Hoeveelheid</th>
<th>Aantekeningen/th>
</tr>";
while( $row = mysqli_fetch_array( $result ) ) {
echo "<tr>";
echo "<td>" . $row['Categorie'] . "</td>";
echo "<td>" . $row['SerieNummer'] . "</td>";
echo "<td>" . $row['MacAdress'] . "</td>";
echo "<td>" . $row['ProductCode'] . "</td>";
echo "<td>" . $row['Prijs'] . "</td>";
echo "<td>" . $row['RekNummer'] . "</td>";
echo "<td>" . $row['PaletNummer'] . "</td>";
echo "<td>" . $row['Hoeveelheid'] . "</td>";
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</article>
</fieldset>
</tbody>
This code works perfectly!
What i want to achieve is to select Records from this table with a checkbos and delete them by clicking on a delete button in the bottom of the page programmed like this:
<Input type="submit" id="submit" name="submit" value="Verwijderen" />
Or at least with this principle!
(This is the second ever application I wrote, 1st was a webpage, and I have no idea how to research this. I keep finding questions and posts that are just not what i need :S Hope U can help me )
I suggest you to use jQuery ajax so you will be able to delete entry one by one without page reload.
At first include jQuery library in HTML HEADER and the Javascript script file
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="script.js"></script>
script.js:
$(document).ready(function() {
$(".delete").click(function(event) {
var entryid = $(this).closest('tr').find('.entryid').val();
alert('This entry will be deleted: ' + entryid); // this will alert you the value what you get. After finisgin to get correct value you can delete this
$.ajax({
type: "GET",
url: "delete.php",
data: "id=" + entryid
});
$(this).closest('tr').remove();
});
});
in MySQL database must be uniqe ID for every entry named 'id'
Grid.html:
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo '<td><input type="hidden" class="entryid" name="id" value='.$row['id'].' />delete</td>';
delete.php:
// connect to database
$id = $_GET['id'];
$sql="DELETE FROM BCD WHERE id='$id'";
First of all you need to wrap the table into a form
<div id="artikel-container">
<form name="form1" method="post" action="...">
<table class="table 1">
...
echo "</table></form>";
mysqli_close($con);
Next you need the checkboxs on the form $row['ID'] is the primary key of the table
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo "<td><input type='checkbox' name='del[]' value='".$row['ID'] . "></td>";
echo "</tr>";
In the php file called in the form's action attribute:
<?php
if( isset( $_POST['del'] ) {
$del = $_POST['del'];
for(x=0; x<count(del); x++ ) {
$sql = 'DELETE FROM BCD WHERE ID='.$del[$x];
//---execuste the query
}
}
?>

Table html/CSS output error: unexpected T_STRING

I am new to web dev, PHP, CSS and html. I want to put a CSS in my table displaying the data on my database, but it doesn't work.
Here is my code:
My CSS file is named "table.css" ...
<html>
<head>
<title>WEW</title>
<head>
<link href="table.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
$con = mysql_connect("localhost","abc123","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_ME", $con);
$result = mysql_query("SELECT * FROM id");
$data->set_css_class("table");
echo "<table class="table">
<tr>
<th>id</th>
<th>password</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysql_close($con);
?>
</body>
</html>
I'm assuming the CSS file is well written, and that it uses the .table selector.
There's several syntax errors in there, all because you need to escape the inner " like so:
echo "A 'string with several \"nesting\" levels' needs escaping.";
echo "<table class="table">
change to
echo "<table class='table'>
AND
echo "<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">";
change to
echo "<tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">";

Categories