So this is my simple php code just to output them on a simple table. I was hoping I make it a sortable table and i do not understand most of the sources from online. Could someone show me a simple or at least the way to do it? I could also go for a drop down menu to select how to list the following data columns!
$connect = mysql_connect("localhost","root","");
if(!$connect){
die("Can not connect: " . mysql_error());
}
mysql_select_db("safedrive", $connect);
$sql = "SELECT * FROM users";
$myData = mysql_query($sql,$connect);
echo "<table border=1>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<br />";
}
echo "</table>";
I understand it is probably better done in the HTML tag, but I would like to hear some proper opinions.
Use this : TableSorter 2.0 , you just need to install the plugin and configure a bit the js file. All is well explained on the site
Related
I firstly did the table layout and made sure everything is working, after connecting the table to a database and trying to put the records inside it, everything worked perfectly, but the class didnt, i have now the boring table without the layout made.
<body>
<h1>Employees</h1>
<table class="responstable">
<?php
require 'connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee");
echo "<table border='1'>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script>
</body>
</html>
the table class is "responstable"
table tag is defined in 2 places.try removing this.
echo "<table border='1'>
or remove the table tag at the top after h1 tag and add the class to the table tag defined in the echo as,
complete code
<body>
<h1>Employees</h1>
<?php
require 'connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee");
echo "<table border='1' class='responstable'>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script>
</body>
</html>
The following code adds a Accept/Decline Button per row. But for some reason, the decline button does not appear. I dont know whats wrong. Been stuck for a while now. Help would be appreciated. Thanks!!
<?php
$con=mysqli_connect("localhost","root","","rabco");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM service_request");
echo "<table border='1'>
<tr>
<th>Service ID</th>
<th>Service Type</th>
<th>Schuduled Date</th>
<th>Scheduled Time</th>
<th>Client Reference
<th>Client ID</th>
<th>Admin ID</th>
<th>Special Instructions</th>
<th>Status</th>
<th>Approve</th>
<th>Decline</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Service_ID'] . "</td>";
echo "<td>" . $row['Service_type'] . "</td>";
echo "<td>" . $row['Sched_date'] . "</td>";
echo "<td>" . $row['Sched_time'] . "</td>";
echo "<td>" . $row['Client_reference'] . "</td>";
echo "<td>" . $row['Client_IDN'] . "</td>";
echo "<td>" . $row['Admin_IDN'] . "</td>";
echo "<td>" . $row['Special_instructions'] . "</td>";
echo "<td>" . $row['Request_status'] . "</td>";
echo "<td>Approve</td>";
echo "<td>Decline</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You are missing the closing quotes on your href attributes, which is creating malformed HTML output.
You need to change:
echo "<td>Approve</td>";
echo "<td>Decline</td>";
to
echo "<td><a href='approve.php?Service_ID=".$row['Service_ID']."'>Approve</a></td>";
echo "<td><a href='decline.php?Service_ID=".$row['Service_ID']."'>Decline</a></td>";
There is a missing closing quote here:
echo "<td>Approve</td>";
Change to:
echo "<td><a href='approve.php?Service_ID=".$row['Service_ID']."'>Approve</a></td>";
I was hoping to get anyone's opinion on why this certain variable won't print out. Variable $sum from the $qsl_ query is not printing along with the other table row data.
<?php
$sql_ = "SELECT SUM(`points`) AS value_sum FROM `history` WHERE `userid` = '$id'";
$result = mysql_query($sql_);
#echo mysql_error();
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
$sql = "SELECT * FROM users";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $sum . "</td>";
}
echo "</table>";
?>
This is my current output where the points column won't produce:
I apologize if my code appears inefficient, I am a struggling student fanatic of PHP. I appreciate direct answers; however, learning is my key here. Thanks in advance!
$sql = "SELECT users.*, sum(history.points) as points FROM users left join history on users.id=history.userid group by users.id";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $record['points'] . "</td>";
}
echo "</table>";
It seems that I might have had my code jungled, thank you #abhi and #msfoster for your healthy inputs.
<?php
$sql = "SELECT * FROM users";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
$id = $record['id'];
$sql_ = "SELECT SUM(`points`) AS value_sum FROM `history` WHERE `userid` = '$id'";
$result = mysql_query($sql_);
#echo mysql_error();
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
echo "<tr>";
echo "<td>" . $id . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
?>
My output is finally:
query is correct but as you shown
var_dump($row) = "array (size=1) 'value_sum' => null "
so it have 'null' because of that its showing blank;
but for this you can use join also .
$row = mysql_fetch_assoc($result); returns an array of "rows". Since you want the first row you need to access it $sum = $row[0]['value_sum'];
I am brand new to PHP and everything else, so please try to keep any response as simple as possible. Thank you. I just can't figure out how to only get the information from the field that is filled in. When I do my search, it brings up the empty fields as well. I need just the information in the HTML form to be found. For example, if I put in a project code, I need all information that pertains to that code. Or if I put in a specific budget, I need everything that has that budget. I hope this makes sense...I'm such a rookie, I don't even know how to word the question!!!! I have tried using !is_null, (isset), !empty but none of them seem to work.
<?php
include 'connect.php';
// Get values from form
$p_code=$_GET['projectcode'];
$p_name=$_GET['projectname'];
$budget=$_GET['budget'];
$s_date=$_GET['startdate'];
$e_date=$_GET['enddate'];
$c_year=$_GET['cyear'];
$c_qtr=$_GET['cqtr'];
$c_grp=$_GET['cgrp'];
$sponsor=$_GET['sponsor'];
$client=$_GET['client'];
$p_lead=$_GET['projectlead'];
$orig_hrs=$_GET['originalhrs'];
$risk_per=$_GET['riskpercent'];
$notes=$_GET['notes'];
if ($db_found) {
$SQL = "SELECT * FROM minimodtable
WHERE projectcode='$p_code'
OR projectname='$p_name'
OR budget='$budget'
OR startdate='$s_date'
OR enddate='$e_date'
OR cyear='$c_year'
OR cqtr='$c_qtr'
OR cgrp='$c_grp'
OR sponsor='$sponsor'
OR client='$client'
OR projectlead='$p_lead'
OR originalhrs='$orig_hrs'
OR riskpercent='$risk_per'
OR notes='$notes'";
$result = mysqli_query($connect, $SQL);
echo "<table border='1'>
<tr>
<th>Project Code</th>
<th>Project Name</th>
<th>Budget</th>
<th>Start Date</th>
<th>End Date</th>
<th>Year</th>
<th>Quarter</th>
<th>Group</th>
<th>Sponsor</th>
<th>Client</th>
<th>Project Lead</th>
<th>Original Hours</th>
<th>Risk Percent</th>
<th>Notes</th>
</tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo "<tr>";
echo "<td>" . $row['projectcode'] . "</td>";
echo "<td>" . $row['projectname'] . "</td>";
echo "<td>" . $row['budget'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['enddate'] . "</td>";
echo "<td>" . $row['cyear'] . "</td>";
echo "<td>" . $row['cqtr'] . "</td>";
echo "<td>" . $row['cgrp'] . "</td>";
echo "<td>" . $row['sponsor'] . "</td>";
echo "<td>" . $row['client'] . "</td>";
echo "<td>" . $row['projectlead'] . "</td>";
echo "<td>" . $row['originalhrs'] . "</td>";
echo "<td>" . $row['riskpercent'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
}
echo "</table>";
}
mysqli_close($connect);
?>
You've made a good first try there. This method should save you having to repeat yourself so much, though. You were correct to start looking at isset - the issue is that you need to dynamically build up your SQL query.
In fact, you'll probably want to change the glue in the implode() function from 'OR' to 'AND' - that way, you can find things that are a specific budget AND that pertain to a certain code.
I've switched it to AND for now - but feel free to switch it back to OR - as per your example.
<?php
// All possible parameters
$params = array(
'projectcode',
'projectname',
'budget',
'startdate',
'enddate',
'cyear',
'cqtr',
'cgrp',
'sponsor',
'client',
'projectlead',
'originalhrs',
'riskpercent',
'notes'
);
$wheres = array();
foreach ($params as $param) {
// Is the param set?
// If so, let's add it to our list of WHERE clauses
if (isset($_GET[$param])) {
$wheres[] = sprintf("%s = '%s'", $param, mysqli_real_escape_string($connect, $_GET[$param]));
}
}
if ($db_found) {
// Now let's make the SQL.
$SQL = 'SELECT * FROM minimodtable';
// We only want to add the WHERE clause if we had some parameters passed
if (count($wheres) > 0) {
$SQL .= ' WHERE ' . implode(' AND ', $wheres);
}
$result = mysqli_query($connect, $SQL);
echo "<table border='1'>
<tr>
<th>Project Code</th>
<th>Project Name</th>
<th>Budget</th>
<th>Start Date</th>
<th>End Date</th>
<th>Year</th>
<th>Quarter</th>
<th>Group</th>
<th>Sponsor</th>
<th>Client</th>
<th>Project Lead</th>
<th>Original Hours</th>
<th>Risk Percent</th>
<th>Notes</th>
</tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo "<tr>";
echo "<td>" . $row['projectcode'] . "</td>";
echo "<td>" . $row['projectname'] . "</td>";
echo "<td>" . $row['budget'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['enddate'] . "</td>";
echo "<td>" . $row['cyear'] . "</td>";
echo "<td>" . $row['cqtr'] . "</td>";
echo "<td>" . $row['cgrp'] . "</td>";
echo "<td>" . $row['sponsor'] . "</td>";
echo "<td>" . $row['client'] . "</td>";
echo "<td>" . $row['projectlead'] . "</td>";
echo "<td>" . $row['originalhrs'] . "</td>";
echo "<td>" . $row['riskpercent'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
}
echo "</table>";
}
mysqli_close($connect);
I'm trying to display members from my database using PHP, however the last name goes in with the first name and I'm not quite sure why... Could anyone point me in the right direction?
<?php
$mysql_db_hostname = "localhost";
$mysql_db_user = "alex";
$mysql_db_password="";
$mysql_db_database="gym";
$con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database");
mysql_select_db($mysql_db_database, $con) or die("Could not select database");
$query = mysql_query("select * from users WHERE Category='Member'");
echo "<table border=1>
<tr>
<th>Users ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>";
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td";
echo "<td>" . $row['Last_Name']."<br>" . "</td";
echo "</tr>";
}
echo "</table";
?>
You aren't closing the tags. Syntax highlighter would show the error.
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td"; <-----------
echo "<td>" . $row['Last_Name']."<br>" . "</td"; <------------
echo "</tr>";
}
You are not closing your <td> tags properly
echo "<td>" . $row['First_Name']."<br>" . "</td>";
//^here
echo "<td>" . $row['Last_Name']."<br>" . "</td>";
//^and here
So the output is all one cell with both variables printed inside
You forgot to close the td on your first name field.
Just change
</td
to
</td>