PHP, trying to make row ID's work - php

So I basically been coding this quick stat shower for my game server, but I want it to ID numbers.
<html>
<head>
<meta charset="utf-8">
<title>ExileMod Stats</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
$con=mysqli_connect("******","******","******","******"); //server address, username, password, dbname
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//check order ascending or descending
if (isset($_GET["order"])) {
$sort = $_GET["order"];
if ($_GET["order"] == "asc"){
$sort = "desc";
$order = "asc";
}
else{
$sort = "asc";
$order = "desc";
}
}
//check filter
if (isset($_GET["name"])) {
$name = $_GET["name"];
}
else {
$name = "name";
}
$list=array('name', 'money', 'score', 'kills', 'deaths', 'uniform', 'vest', 'last_updated_at');
if (in_array($name,$list))
{
//variable ok
}
else
{
$name = "name";
}
$query = mysqli_query($con,"SELECT * FROM account a INNER JOIN player p ON a.uid = p.account_uid ORDER BY a.$name $order");
?><!--//echo "<table border='1'>-->
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<table class="table">
<tr>
<?php echo "<th>Player Name</th>";?>
<?php echo "<th>Money</th>";?>
<?php echo "<th>Score</th>";?>
<?php echo "<th>Kills</th>";?>
<?php echo "<th>Deaths</th>";?>
<?php echo "<th>Uniform</th>";?>
<?php echo "<th>Vest</th>";?>
<?php echo "<th>Last Updated</th>";?>
</tr>
<!--//";-->
<?php
while($row = mysqli_fetch_array($query))
{
?><tr class="danger"><?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['uniform'] . "</td>";
echo "<td>" . $row['vest'] . "</td>";
echo "<td>" . $row['last_updated_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
© HeroesOfGaming 2016 - 2017
</div>
</body>
</html>
So what I am trying to do is put numbers so it counts 1 2 3 4 5 6 7 to however many players are selected from the database? Hopefully this makes sense.

You can do like this
<?php
$i = 0; // <--- Added this
while($row = mysqli_fetch_array($query))
{
$i++; // <--- Added this
?><tr class="danger"><?php
echo "<td>". $i . "</td>"; // <--- Added this
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['uniform'] . "</td>";
echo "<td>" . $row['vest'] . "</td>";
echo "<td>" . $row['last_updated_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

Related

How to make search based on pickup date and return date?

In the car page,i already input all the data into mysql database(car table).So i want to ease the user to find the car that available on pickup date and return date.So,I made search pickup date and return date in search page.But the error is "could not be execute".
This is using mysql and php.
car.php
</body>
<table align='center' width='40%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<form action="search.php" method="post" enctype="multipart/form-data">
<td><input type="date" name="Pickup_date" placeholder="Pickup_date" style='height:38px' required /></td>
<td><input type="date" name="Return_date" placeholder="Return_date" style='height:38px' required /></td>
<td><input type="submit" name="submitbook" value="Search" style='background-color: blue; border: none; color: white; padding: 10px 10px; text-align: center; text-decoration: none; display: inline-block; font-size: 14px;'/></td>
</div>
</form>
</table>
<?php
$link = mysqli_connect("localhost", "root", "", "online_car_rental");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>Add to Booking</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo '<input type="hidden" name="pickup_date"
value="'.$pickup_date.'">';
echo '<input type="hidden" name="return_date"
value="'.$return_date.'">';
echo "<td><button onclick=\"booking_car('" . $row['car_id'] .
"')\">Book</button>
</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</html>
search.php
<?php
$link = mysqli_connect("localhost", "root", "", "online_car_rental");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$search=$_GET['s'];
$query="SELECT * FROM car WHERE date BETWEEN 'pickup_date' AND
'return_date'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>Add to Booking</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100' width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "<td>" . $row['pickup_date'] . "</td>";
echo "<td>" . $row['return_date'] . "</td>";
echo "<td><button onclick=\"booking_car('" . $row['car_id'] .
"')\">Book</button>
</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</html>
i expect the output is based on pickup_date and return_date.But the output is "ERROR:Could not able to execute".
You Can check with this , When you have two tables you can use inner join query to fetch the data
SELECT columns
FROM table WHERE pickup_date < 'your_pickup_date' AND return_date >= 'your_pickup_date' ORDER
BY car or date;
Without knowing your schema, it's difficult to provide a detailed solution, but an example of a plausible query might be as follows:
"
SELECT c.olumns
, y.ou
, a.ctually
, w.ant
FROM car c
WHERE pickup_date < :return_date
AND return_date >= :pickup_date
ORDER
BY pickup_date
, car_id;
"

PHP MYSQL display table with action row clickable to open specific file

I am having this type of problem. I have a table displayed on my webpage. Wherein I have an Action column. I am trying to make my (a href) code open different files depending on what was indicated on my table whether it is pdf or an excel file.
<?php
$con=mysqli_connect("localhost","root","","annualdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM aviation_report");
echo "<head><style>
table {
border: 2px solid black;
width: 100%;
height: 100px;
text-align: center;
}
</style></head>";
echo "<table>
<tr>
<th>Agency</th>
<th>FileName</th>
<th>FileType</th>
<th>Date</th>
<th>Action</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['agency'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "<td>" . $row['filetype'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='sample.pdf'>OPEN FILE</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You would have this code:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['agency'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "<td>" . $row['filetype'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a target='_blank' href='" . $row['filename'] . "'>OPEN FILE</a></td>";
echo "</tr>";
}

How to make an dropdown menu to sort data from an database

I am working on an application to use on an website. The application let you add job offers to a database to post it on the website and on social media. I have done a lot already but now I am trying to make an sort menu. In my database I have 1 row that is called Status. The Status is an enum with the data Open and Closed so I would like to have a sort option. I would like to sort on Open, Closed and all.
I already have a table and I would like to see the changes on that existed table if that is possible but I have no idea how to do this. Can somebody help me with this? I am using HTML, PHP and as database PHPMyAdmin. Here is my code:
<!DOCTYPE HTML>
<html>
<head>
<style>
table {
border-collapse: collapse;
border: 1px solid black;
}
td {
border: 1px solid black;
width: 120px;
}
</style>
</head>
<body>
<h2>Jobs Overview (Admin)</h2>
<form action="/action_page.php">
<select id="sorting">
<option value="All" >All</option>
<option value="Open">Open</option>
<option value="Closed">Closed</option>
</select>
<br><br>
<?php
include 'Connection.php';
echo "<table>";
echo "<tr>";
echo "<th>" . "jid" . "</th>";
echo "<th>" . "role" . "</th>";
echo "<th>" . "type" . "</th>";
echo "<th>" . "availability" . "</th>";
echo "<th>" . "location" . "</th>";
echo "<th>" . "status" . "</th>";
echo "<th>" . "Verwijder?" . "</th>";
echo "</tr>";
$select = "SELECT * FROM test";
$result = mysql_query($select);
if($result) {
} else {
echo "Error! <br>";
echo mysql_error();
}
while($data = mysql_fetch_assoc ($result)) {
echo "<tr>";
echo "<td>" . $data["jid"] . "</td>";
echo "<td> <a href='jobsupdate.php?jid=" . $data["jid"] . "'>" . $data["role"] . " </a></td>";
echo "<td>" . $data["type"] . "</td>";
echo "<td>" . $data["availability"] . "</td>";
echo "<td>" . $data["location"] . "</td>";
echo "<td>" . $data["status"] . "</td>";
echo "<td> <center>Verwijder</center></td>";
}
echo "</tr>";
echo "</table>";
?>
<br><br>
Vacatures Toevoegen
</body>
</html>
Thanks in advance
You can add HTML class to open and closed jobs.
while($data = mysql_fetch_assoc ($result)) {
echo "<tr>";
echo "<td>" . $data["jid"] . "</td>";
echo "<td> <a href='jobsupdate.php?jid=" . $data["jid"] . "'>" . $data["role"] . " </a></td>";
echo "<td>" . $data["type"] . "</td>";
echo "<td>" . $data["availability"] . "</td>";
echo "<td>" . $data["location"] . "</td>";
if ($data["status"] == 0) {
echo "<td class='open'>" . $data["status"] . "</td>";
} else {
echo "<td class='closed'>" . $data["status"] . "</td>";
}
echo "<td> <center>Verwijder</center></td>";
}
echo "</tr>";
echo "</table>";
After, this you have to write a jquery script to hide the jobs .on change.
$('#sorting').on('change', function(){
var status = $('#sorting:selected').val();
if (status == "Open") {
$('.closed').hide();
} else if (status == "Closed") {
$('.open').hide();
} else if (status == "All"){
$('.closed').show();
$('.open').show();
}
});​
I hope this solves your problem.
You can make a Ajax post request on select change to get the records and put those records on success in your table.
More about how to do this with Jquery / Ajax here
More about Jquery on change here

If functions per $row in a table

I have a table that shows me data from a call flow.
I need the Data from this table to be manipulated per row in such a way that all the values of my table, which are being looked up from my DB, (which are now in code) will be translated into a text value. Let me show U and explain:
My Table:
<?php
include_once "Connect2Martijn1.php";
?>
<link rel="stylesheet" href="CSSMartijn1.css">
</link>
<head>
<meta charset="iso-8859-1">
<meta name="description"content="VoizXL ">
<meta name="keywords"content="VoizXL ">
<meta name="author"content="Kenn Lo-A-Tjong">
</meta>
<title>Call Flow</title>
</head>
<fieldset>
<article class="rondehoeken">
<header>
<div class="streep1"></div>
<div class="streep2"></div>
<div class="streep3"></div>
<div class="streep4"></div>
<div class="streep5"></div>
<h1 id="artikel-titel" >Call Flow</h1>
</header>
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Call Flow</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","","voizxl_wachtrij");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Callflow");
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $row['statusAnswered'] . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Example of (how I want to be) Translating the Data:
<?php
if ($row['statusAnswered'] ="NULL")
{
echo "Not Answered!";
}
else
{
echo "Answered!";
}
?>
What I want to Achieve is for eg. that the value in this table from $row['statusAnswered'] will be displayed in text as "Answered or Not Answered" if the Value of this row in the DB is NULL or Not...
How do I do this?
Right now I can only achieve to have 1 echo under the table saying Answered :S
No Idea how to put it per $row.
while($row = mysqli_fetch_array($result))
{
if (!isset($row['statusAnswered']))
{
$answered = "Not Answered!";
}
else
{
$answered = "Answered!";
}
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $answered . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
if ($row['statusAnswered'] =="NULL"||$row['statusAnswered'] =="Null" || $row['statusAnswered'] =="null" || $row['statusAnswered'] =="")
{
echo "<td>Not Answered!</td>";
}
else
{
echo "<td>Answered!</td>";
}
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . ($row['statusAnswered']=='NULL'?'Not Answered!':'Answered!') . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
You could use;
$checkstatus = if($row['statusAnswered'] = NULL) { echo "Not Answered!"; } else {echo "Answered!";};
then call that by replacing;
echo "<td>" . $row['statusAnswered'] . "</td>";
with;
echo "<td>{$checkstatus}</td>"

How to make PHP html table have color mismatch between rows?

I am learning PHP as I code. From an example on w3schools it showed using PHP and msql to display database results on a html table. My questions is, I now have too many rows and I could not make them have mismatch colors between rows. I've tried adding style span and font color after <td but it doesn't take it. the entire PHP just don't work if I do so.
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
The output of the code above will be:
Firstname Lastname
Glenn Quagmire
Peter Griffin
http://www.w3schools.com/php/php_mysql_select.asp
Not entirely sure what you mean by color mismatch. Assuming that you mean alternating row colors I'd do the following:
$odd = false;
while (...)
{
echo '<tr class="'.($odd ? "odd" : "even").'">';
...
echo "</tr>";
$odd = !$odd;
}
Now you have the tr element being of class odd or even alternatingly, and may specify some additional background color for one of them in your CSS, e.g.:
tr.odd { background-color: rgba(0, 0, 0, 0.05); }
Replace this:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
with this:
$i = 0;
while($row = mysql_fetch_array($result))
{
echo "<tr ". ($i % 2 == 0 ? 'style="background-color:grey;"' : '' .">";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$i++;
}
Every other row will have grey color.
Maybe you can use this approach with jquery
<script src="text/javascript">
$('#table tbody tr:odd').addClass('odd');
$('#table tbody tr:even').addClass('even');
</script>
and then add the styles
.odd { background-color: #color }
.even { background-color: #color }
$class = "even";
while($row = mysql_fetch_array($result))
{
if($class == "even")
{
echo "<tr class='$class'>";
$class = "odd";
}
else
{
echo "<tr class='$class'>";
$class = "even";
}
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
CSS
tr.even
{
background-color:blue;//Pick your own color
}
tr.odd
{
background-color:green;
}
Here is a list of color names. If you want a more detailed color choices, click here.
Use
$flag = 0;
while($row = mysql_fetch_array($result))
{
if ($flag%2 == 1)
echo "<tr bgcolor=#123345>";
else
echo echo "<tr bgcolor=#643235>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$flag = $flag +1;
}

Categories