Ranking by number of points with php - php

I have a question. I'm currently working on a ranking list webprogram for a shooting club made with php and html.
Now I'm at the point, where I want to print out the ranking table with the results from my sql database. The ranking table is showing a rank, personal informations and the results. The results where sort by the scored points.
My problem now is, that I dont know how to make an automatic rank per person. The only thing I managed to do is this:
Output
Here is my code:
<!DOCTYPE html>
<html>
<body>
<div>
<h3 style='color:green; font-weight: 700;'> <?php echo "Rangliste $infos" ?> </h3>
<a class='btn btn-default' href='?page=rangliste'>Zurück</a>
<a class='btn btn-default' href='?page=rangliste_veteranencup'> <span class=''></span> Veteranen Cup</a>
<a class='btn btn-default' href='?page=rangliste_dianacup'><span class=''></span>Diana Cup</a>
<a class='btn btn-default' href='?page=rangliste_gäste'><span class=''></span>Gäste Cup</a>
<style>
table {
border-collapse: collapse;
width: 90%;
color: black;
font-size: 12px;
text-align: left;
}
div {
margin-left: 50px;
}
th {
border: 1px solid black;
background-color: #D3DFDD;
}
td {
text-align: center;
border: 1px solid black;
background-color: white;
}
.outlined {
border: 2px solid black;
}
</style>
<p></p>
<table>
<colgroup>
<col>
<col>
<col>
<col>
<col>
<col>
<col>
<col>
<col>
<col class='outlined'>
<col span='3'>
</colgroup>
<tr>
<th scope='col'>Rang</th>
<th scope='col'>Vorname</th>
<th scope='col'>Nachname</th>
<th scope='col'>Jahrgang</th>
<th scope='col'>Wohnort</th>
<th scope='col'>Total Reh</th>
<th scope='col'>Total Gams</th>
<th scope='col'>Total Klapphas</th>
<th scope='col'>Total Rollhas</th>
<th scope='col'>Gesamttotal</th>
<th scope='col'>Tiefschuss Reh</th>
<th scope='col'>Tiefschuss Gams</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "art");
if ($conn-> connect_error) {
die("Connection failed" . $conn-> connect_error);
}
$sql = "SELECT schuetze.vorname, schuetze.nachname, schuetze.jahrgang, schuetze.ort,
resultate.vmTotalReh, resultate.vmTotalGams, resultate.vmTotalKlapphas, resultate.vmTotalRollhas, resultate.vmTotal, resultate.vmTiefschussReh, resultate.vmTiefschussGams
FROM schuetze
JOIN resultate ON schuetze.id = resultate.schuetzeID
WHERE resultate.anlassID = ? ORDER BY resultate.vmTotal DESC, resultate.vmTotalKlapphas DESC, resultate.vmTotalRollhas DESC, resultate.vmTotalReh DESC, resultate.vmTotalGams DESC, resultate.vmTiefschussReh DESC, resultate.vmTiefschussGams DESC, schuetze.jahrgang DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $res_anlassID);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result-> fetch_assoc()) {
echo "</td><td>"/* . $rang . "</td><td>" */. $row['vorname'] . "</td><td>" . $row['nachname'] . "</td><td>" . $row['jahrgang'] . "</td><td>" . $row['ort'] . "</td><td>" . $row['vmTotalReh'] . "</td><td>" . $row['vmTotalGams'] . "</td><td>" . $row['vmTotalKlapphas'] . "</td><td>" . $row['vmTotalRollhas'] . "</td><td>" . $row['vmTotal'] . "</td><td>" . $row['vmTiefschussReh'] . "</td><td>" . $row['vmTiefschussGams'] . "</td></tr>";
}
echo "</table>";
$conn-> close();
?>
</div>
</body>
</html>

Related

PHP Column Styling

I need my table to include submissions into three columns then a new row start with three more columns, etc. So submission 1, first column, submission 2, second column, submission 3, 3 column. Then a new row and the process starts again. (HTML Example Code below)
Every time I try with my php code my page comes up blank so I am missing something somewhere.
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, website, url, email, date, description FROM basic";
$result = $conn->query($sql);
if ($result->num_rows>0){
<tr>
while ($row=$result->fetch_assoc()) {
echo "<table><tr><th><b>Company:</b> " . $row["name"]. "</th></tr><tr><td>
<b>URL:</b> <a href='".$row["url"]."'>".$row["url"]."</a></td></tr><tr><td>
<b>Email:</b> " . $row["email"]. "</td></tr><tr><td><b>Launch Date:</b> " .
$row["date"]. "</td></tr><tr><td><b>Announcement:</b> " .
$row["description"]. "</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
Example: This is what I am trying to get it to look like.
<style>
table, td, th {
border: 10px solid #ffffff;
text-align: left;
}
table {
border-collapse: initial;
width: 100%;
}
td {
padding: 15px;
width: 25%;
line-height: 2;
}
th {
background-color: grey;
color: white;
padding: 5px;
width: 25%;
}
</style>
<body>
<table class="ex1">
<tr>
<th>Company</th>
<th>Company</th>
<th>Company</th>
</tr>
<tr>
<td>
<b>URL:</b><br>
<b>Location:</b><br>
<b>Inductry:</b><br>
<b>Start Date:</b><br>
<b>Announcement:</b><br>
<br>
</td>
<td>
<b>URL:</b><br>
<b>Location:</b><br>
<b>Inductry:</b><br>
<b>Start Date:</b><br>
<b>Announcement:</b><br>
<br>
</td>
<td>
<b>URL:</b><br>
<b>Location:</b><br>
<b>Inductry:</b><br>
<b>Start Date:</b><br>
<b>Announcement:</b><br>
<br>
</td>
</table>
Your PHP code is not generating HTML in proper format. Please try below:
// Query executed here and resultset returned
if ($result->num_rows>0){
echo "<table>";
while ($row=$result->fetch_assoc()) {
echo "<tr><th><b>Company:</b> " . $row["name"]. "</th></tr>
<tr><td><b>URL:</b> <a href='".$row["url"]."'>".$row["url"]."</a></td></tr>
<tr><td><b>Email:</b> " . $row["email"]. "</td></tr>
<tr><td><b>Launch Date:</b> " . $row["date"]. "</td></tr>
<tr><td><b>Announcement:</b> " . $row["description"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}

Displaying result of same column but having different values in php

I have the following php code which queries a table of my database according to place.
The table structure is as follows :
s/n | inventory no | asset no | place
=====+==============+============+=============
1 | 125A | 5245 | London
2 | 1254B | 7545 | London
3 | 128A | 5645 | New York
4 | 254B | 1545 | Tokyo
5 | 6545 | 1456 | Tokyo
And the code:
$location=$_POST['loc'];
foreach ($location as $chk1)
{
echo "<br>";
$sql="SELECT * FROM desktop WHERE Place='$chk1';";
$result=mysql_query($sql);
if($result==null)
{
echo '<script> alert("No entry for location '.$chk1.' anditem '.'desktopreturned");</script>';
continue;
}
$row = mysql_fetch_array($result);
if($row==null)
{
echo '<script>alert("No entry for location '.$chk1.' and item '.'desktop returned");</script>';
continue;
}
mysql_data_seek($result, 0);
?><table border='1' id="desktop" caption=<?php $location ?> >
<tr>
<th>S/N</th>
<th>Inventory No</th>
<th>Asset No</th>
<th>Place</th>
</tr>
<?php
while($row = mysql_fetch_array($result)) {?>
<tr class="alt">
<?php
echo "<td>" . $row['S/N'] . "</td>";
echo "<td>" . $row['Inventory no'] . "</td>";
echo "<td>" . $row['Asset No'] . "</td>";
echo "<td>" . $row['Place'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
Edit :
CSS code :
#desktop {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
width: 100%;
border-collapse: collapse;
}
#desktop th {
font-size: 1.1em;
text-align: left;
padding-top: 5px;
padding-bottom: 4px;
background-color: #FFCC33;
color: #ffffff;
}
#desktop td, #desktop th {
font-size: 1em;
border: 1px solid #FFCC33;
padding: 3px 7px 2px 7px;
}
#desktop tr.alt td {
color: #000000;
background-color: #FFFFCC;
}
Here, $location is storing the locations selected in the html form.
I want the returned result of the query for different places to be displayed as a single table. Currently it is displaying different place's result as different tables having their own table headers. I want the returned query to be appended into one variable.
Just place your table before the foreach loop.As it is inside the loop it repeats each time the loop continues
<table border='1' id="desktop" caption=<?php $location ?> >
<tr>
<th>S/N</th>
<th>Inventory No</th>
<th>Asset No</th>
<th>Place</th>
</tr>
foreach ($location as $chk1)
{
echo "<br>";
$sql="SELECT * FROM desktop WHERE Place='$chk1';";
$result=mysql_query($sql);
...
...
}
}
echo "</table>";
try with this code, I have changed location array default to london,
you have changed case for field name first letter should not be capital. Hope this help :)
$location=array('London','Tokyo');
$str.='<table border="1" id="desktop">
<tr>
<th>S/N</th>
<th>Inventory No</th>
<th>Asset No</th>
<th>Place</th>
</tr>';
foreach ($location as $chk1)
{
echo "<br>";
$sql="SELECT * FROM desktop WHERE Place='$chk1';";
$result=mysql_query($sql);
if($result==null)
{
echo '<script> alert("No entry for location '.$chk1.' anditem '.'desktopreturned");</script>';
continue;
}
$row = mysql_fetch_array($result);
if($row==null)
{
echo '<script>alert("No entry for location '.$chk1.' and item '.'desktop returned");</script>';
continue;
}
mysql_data_seek($result, 0);
while($row = mysql_fetch_array($result)) {
$str.='<tr class="alt">
<td>' . $row['sr'] . '</td>
<td>' . $row['inventory_no'] . '</td>
<td>'. $row['asset_no'] . '</td>
<td>' . $row['place'] . '</td>
</tr>';
}
}
$str.='</table>';
echo $str;
?>

Failed to load pdf, dompdf error -> PDF.js v0.8.787 (build: 58407cf) Message: InvalidPDFException

Following is my code to display a pdf using dompdf but the error was failed to load pdf
PDF.js v0.8.787 (build: 58407cf)
Message: InvalidPDFException
table name is acad_act and database name is pbas_db. Details of database connection is stored in db_connect.php. Any help would be appreciable
<?php
include('db_connect.php');
require 'dompdf/dompdf_config.inc.php';
$sqlinv="SELECT * FROM acad_act";
$res_inv = mysql_query($sqlinv) or die(mysql_error());
while ($sql3=mysql_fetch_array($res_inv)) {
$inv_id = $sql3['User_id'];
$date = $sql3['Year'];
$cname = $sql3['Gen_Info_AQ'];
$subtotal = $sql3['Gen_Info_NOC'];
$disc = $sql3['Gen_Info_Place'];
$subtotal2 = $sql3['Gen_Info_Duration'];
$tax = $sql3['Gen_Info_SA'];
$gtotal = $sql3['Gen_Info_Aqyear'];
}
$dompdf= new DOMPDF();
$html .="<html>
<head>
<style>
body {
font-family: arial;
font-size: 13px;
}
table {
width: 100%;
border-collapse: collapse;
}
th {
font-weight: bold;
font-size: 14px;
border: 1px solid #333333;
}
tr {
width: 100%;
}
td {
border: 1px solid #333333;
text-align: center;
padding: 10px;
font-weight: normal!important;
}
</style>
</head><body>";
$html .="
<table>
<thead>
<tr>
<th class=''>Sr.</th>
<th class=''>Particulars</th>
<th class=''>Size</th>
<th class=''>Quantity / Insertion</th>
<th class=''>Rate</th>
<th class=''>Amount</th>
<th class=''>Amount 2</th>
<th class=''>Amount 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>" . $inv_id . "</td>
<td>" . $date . "</td>
<td>" . $cname . "</td>
<td>" . $subtotal . "</td>
<td>" . $disc . "</td>
<td>" . $subtotal2 . "</td>
<td>" . $tax . "</td>
<td>" . $gtotal . "</td>
</tr>
</tbody>
</table>
</body>
</html>";
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
exit(0);
?>
The exception is due to pdf.js plugin installed in the browser. Instead of rendering the error the pdfjs shows an exception which doesn't display the error. Just remove the pdfjs plugin than you will be able to see the required error.

how to select and echo an image from mysql

I am trying to get information from mysql including a blob image which i shall echo with php and will have an onclick event within the php, redirecting it to another page. The onlick event will contain a mysql result which it will carry with it as seen in the code below.
My main issue is with the syntax of the code or if there is another way to do it all together. please keep in mind the output when the script is run is similiar to that of google images, bing images etc. Thank you.
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
echo "<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding-right: 2em;'><img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" width="160px" height="200px";" onclick="window.location='lookup.php?pattern=" . $row['title'] . "';>";
</td>
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Untested but here is one way you could clean up your code move style and javascript (and use some jquery) into the head:
<?php
$con=mysqli_connect("localhost","root","*******","media") or die("Failed to connect to MySQL: " . mysqli_connect_error());
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('img').each(function() {
var img = $(this);
img.click(function() {
window.location="lookup.php?pattern=" + img.attr('title');
});
});
});
</script>
<style>
table {
margin: auto;
text-align: left;
background: white;
padding: 3em;
border: 2px solid #000000;
}
table tr td {
padding-right: 2em;
}
table tr td img {
width: 160px;
height: 200px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Movie Title</th>
<th>Language</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
echo "
<tr>
<td>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\" title=\"" . $row['title'] . "\">
</td>
</tr>
";
}
?>
</table>
</body>
</html>
<?php mysqli_close($con); ?>
Or if you don't want to use javascript, you could always wrap the image around the anchor tag instead:
<td>
<a href='lookup.php?pattern={$row['title']}'>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\">
</a>
</td>
You could further separate PHP and HTML code:
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
$img = 'data:image/jpeg;base64,'
. base64_encode( $row['image'] );
?>
<tr>
<td style='padding-right: 2em;'>
<img src="<?php echo $img; ?>"
style="width: 160px; height: 200px;"
onclick="window.location='lookup.php?pattern=<?php echo $row['title']?>;'"
/>
</td>
</tr>
<?php } ?>
</table>
<?php
mysqli_close($con);
?>
You could also use some sort of templating engine to do this, but the results would be pretty much the same - I don't see much point in writing, say,
<strong>{{ title }}</strong>
instead of
<strong><?php echo $title; ?></strong>

Border around specific table row

I am trying to create a border around the top row ONLY! Currently, a border will be displayed only around the outside of the table. But I also want a border around the first row. Can someone help me do this? I want the row with 'Team, Correct Picks, and Points' to have a border around it.
<body>
<?=$leaguename?>
<center><table cellspacing="0" style="width:400px; border:1px solid gray">
<?
echo "<tr border=\"1\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr style=\"border:1px solid gray\" bgcolor=\"gray\"><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
</table></center>
</body>
This is presentational, so mixing it into the PHP is a bad idea. Instead, use CSS:
tr:first-child td { border-top: 1px solid black; border-bottom: 1px solid black; }
tr:first-child td:first-child { border-left:1px solid black; }
tr:first-child td:last-child { border-right:1px solid black; }
IE7+8 support for the *-child selectors can be a bit buggy. If it's not working in those browsers, consider using JavaScript polyfills.
You may also want to put the headers inside <thead> and <th> elements to make the <tr> with data the first row in a <tbody> element.
e.g.
<table>
<thead>
<tr>
<th>Team</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
</tr>
</tbody>
</table>

Categories