So I was helped in getting the data to populate properly but when trying to get it to create a table and I cant seem to get it to work properly. I looked online and cant seem to find a solution maybe I just missed it.
This is the code I want to change
function getplayers()
{
$json = file_get_contents('http://116.203.39.175:30120/players.json');
$json_data = json_decode($json, true);
foreach ($json_data as $player_data) {
// Initialise the steam id to an empty string in case one is not found
$player_steam_id = "";
$player_lic = "";
// Find the steam id in the identifiers array
if (array_key_exists("identifiers", $player_data)) {
$steam_identifiers = [];
foreach ($player_data["identifiers"] as $identifier_str)
if (preg_match("/^steam:/i", $identifier_str, $m))
$steam_identifiers[] = $identifier_str;
if (!empty($steam_identifiers)) {
$player_steam_id = $steam_identifiers[0];
}
}
if (array_key_exists("identifiers", $player_data)) {
$steam_identifiers = [];
foreach ($player_data["identifiers"] as $identifier_str)
if (preg_match("/^license:/i", $identifier_str, $m))
$steam_identifiers[] = $identifier_str;
if (!empty($steam_identifiers)) {
$player_lic = $steam_identifiers[0];
}
}
$player_id = $player_data["id"];
$player_name = $player_data["name"];
echo '
<table id="allUsers" class="table table-striped table-bordered">
<thead>
<tr>
<th>Player ID</th>
<th>Name</th>
<th>Steam ID</th>
<th>License</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
';
echo '
<tr>
<td>' . $player_id . '</td>
<td>' . $player_name . '</td>
<td>' . $player_steam_id . '</td>
<td>' . $player_lic . '</td>
<td>
<input name="deleteBan" type="submit" class="btn btn-xs btn-link" onclick="deleteBan(' . $player_id . ')" value="Delete" />
';
echo '
<input name="bid" type="hidden" value=' . $player_id . ' />
</form>
</td>
</tr>
';
echo '
</tbody>
</table>
';
}}
New one with Json
and this is a similar code that works from SQL but im not sure how to get the same end point.
function getbans()
{
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, "mayfairg_fivem");
$site = BASE_URL;
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
$query = "SELECT id,identifier,license,targetplayername,sourceplayername,reason,added,
case
when permanent = 1 then 'Yes'
when permanent = 0 then 'No'
end
FROM banlist";
$result = mysqli_query($link, $query);
echo '
<table id="allUsers" class="table table-striped table-bordered">
<thead>
<tr>
<th>Steam ID</th>
<th>License</th>
<th>Banned Player</th>
<th>Admin</th>
<th>Reason</th>
<th>Date</th>
<th>Perm?</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
';
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
echo '
<tr>
<td>' . $row[1] . '</td>
<td>' . $row[2] . '</td>
<td>' . $row[3] . '</td>
<td>' . $row[4] . '</td>
<td>' . $row[5] . '</td>
<td>' . $row[6] . '</td>
<td>' . $row[7] . '</td>
<td>
<form action="'.$site.'/actions/adminActions.php" method="post">
<input name="deleteBan" type="submit" class="btn btn-xs btn-link" onclick="deleteBan(' . $row[0] . ')" value="Delete" />
';
echo '
<input name="bid" type="hidden" value=' . $row[0] . ' />
</form>
</td>
</tr>
';
}
echo '
</tbody>
</table>
';
}
Working with SQL
Here's some code-cleanup for you. A break in your inner loop is best practice to avoid excess iterations. Your preg_match() calls can be merged into one to generate a variable array assignment. I am using the null coalescing operator to declare an empty string as the fallback for missing ids.
Code: (Demo)
$players = json_decode($json, true);
foreach ($players as $player) {
if (!empty($player['identifiers'])) {
foreach ($player["identifiers"] as $id) {
if (preg_match("/^(steam|license):([a-f\d]+)$/", $id, $m)) { // target the two specific ids
$player[$m[1]] = $m[2];
}
if (isset($player['steam'], $player['license'])) {
break; // stop iterating, we have everything we want
}
}
}
$player['steam'] ?? ''; // fallback to empty string when not found
$player['license'] ?? ''; // fallback to empty string when not found
echo '
<tr>
<td>' . $player["id"] . '</td>
<td>' . $player["name"] . '</td>
<td>' . $player["steam"] . '</td>
<td>' . $player["license"] . '</td>
<td>
<input name="deleteBan" type="submit" class="btn btn-xs btn-link" onclick="deleteBan(' . $player["id"] . ')" value="Delete" />
</tr>
';
}
I figured it out it was a simple fix I had a "}" in the wrong location.
function getplayers()
{
echo '
<table id="allUsers" class="table table-striped table-bordered">
<thead>
<tr>
<th>Player ID</th>
<th>Name</th>
<th>Steam ID</th>
<th>License</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
';
$json = file_get_contents('http://116.203.39.175:30120/players.json');
$json_data = json_decode($json, true);
foreach ($json_data as $player_data) {
// Initialise the steam id to an empty string in case one is not found
$player_steam_id = "";
$player_lic = "";
// Find the steam id in the identifiers array
if (array_key_exists("identifiers", $player_data)) {
$steam_identifiers = [];
foreach ($player_data["identifiers"] as $identifier_str)
if (preg_match("/^steam:/i", $identifier_str, $m))
$steam_identifiers[] = $identifier_str;
if (!empty($steam_identifiers)) {
$player_steam_id = $steam_identifiers[0];
}
}
if (array_key_exists("identifiers", $player_data)) {
$steam_identifiers = [];
foreach ($player_data["identifiers"] as $identifier_str)
if (preg_match("/^license:/i", $identifier_str, $m))
$steam_identifiers[] = $identifier_str;
if (!empty($steam_identifiers)) {
$player_lic = $steam_identifiers[0];
}
}
$player_id = $player_data["id"];
$player_name = $player_data["name"];
echo '
<tr>
<td>' . $player_id . '</td>
<td>' . $player_name . '</td>
<td>' . $player_steam_id . '</td>
<td>' . $player_lic . '</td>
<td>
<input name="deleteBan" type="submit" class="btn btn-xs btn-link" onclick="deleteBan(' . $player_id . ')" value="Delete" />
';
echo '
<input name="bid" type="hidden" value=' . $player_id . ' />
</form>
</td>
</tr>
';
}
echo '
</tbody>
</table>
';
}
Related
When my data is outputted from my server onto a website it outputs it onto 1 row instead of going down a row and outputting the next row of data
!
session_start();
$view = new stdClass();
$view->pageTitle = 'Camp Sites';
require_once('views/CampSites.phtml');
require_once('Models/campData.php');
require_once('Models/campDataSet.php');
$campDataSet = new campDataSet();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$search = $_POST['search'];
$_SESSION['search'] = $search;
$sqlQuery = "SELECT * FROM campsites ='$search'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<tr><td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td></tr>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
You need to wrap tags around the code in your foreach like above.
i'm trying to retrieve information from a database and put it into a table in PHP and HTML however, i've encountered a problem where it says ive got an error:
Fatal error: Uncaught Error: Function name must be a string in
F:\Client Server\Website\campsites.php:41 Stack trace: #0 {main}
thrown
when I check the line that the error is in it says :
Method 'fetch_assoc' not found in array
How can I fix this?
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$search = $_POST['search'];
$_SESSION['search'] = $search;
$sqlQuery = "SELECT * FROM campsites ='$search'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
if($result > 0){
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
while($row = $result->fetch_assoc()){
echo '<td>' . $row['id_campsite']. '</td> <td>' . $row['campsite_name'] . '</td> <td>' . $row['address'] . '</td> <td>' . $row['postcode']. '</td> <td>' . $row['country']. '</td> <td>' . $row['lattitude']. '</td> <td>' . $row['longitude']. '</td> <td>' . $row['email']. '</td> <td>' . $row['phone_number']. '</td> </td>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
EDIT :
var_dump($result);
array(1) { [0]=> object(DBdata)#6 (11) { ["db"]=> NULL ["conn"]=> object(PDO)#7 (0) { }
["id_campsite"]=> string(1) "1" ["campsite_name"]=> string(13) "lake district" ["address"]=> string(32) "lake distrcit greater manchester"
["postcode"]=> string(7) "m21 8fx" ["country"]=> string(7) "England" ["lattitude"]=> string(13) "1 0800 243230" ["longitude"]=> string(12) "120312310978"
["email"]=> string(14) "lake#gmail.com" ["phone_number"]=> string(12) "059874327823" } }
One possible reason for this error is that $campDataSet->fetchAllCamps($sqlQuery) returns an array. In this case your code should look like this:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$search = $_POST['search'];
$_SESSION['search'] = $search;
$sqlQuery = "SELECT * FROM campsites ='$search'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<td>' . $row['id_campsite']. '</td> <td>' . $row['campsite_name'] . '</td> <td>' . $row['address'] . '</td> <td>' . $row['postcode']. '</td> <td>' . $row['country']. '</td> <td>' . $row['lattitude']. '</td> <td>' . $row['longitude']. '</td> <td>' . $row['email']. '</td> <td>' . $row['phone_number']. '</td> </td>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
?>
Update:
Your $campDataSet->fetchAllCamps returns an array of objects. This is more appropriate in this case:
<?php
...
foreach ($result as $row) {
echo '<td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td>';
}
...
?>
Assuming that fetchAllCamps() does the fetching for you, this is why you have an array rather than a result set, so instead of the line...
while($row = $result->fetch_assoc()){
replace it with
foreach ( $result as $row ) {
I am presently working on a project in php mysql to display content from db with edit option against each row. In the below is the code, I am not able to return any value only for the first row, Rest of the row buttons are working fine. I am not able to figure out what is issue. It would be great if you could help me out.
Code snippet of the table display page
$sql1 = "SELECT * FROM `computation` WHERE `compute_month` = '$month' and `compensation_type` = '$allowance' and order by `emp_id` ASC";
}
$result1 = mysqli_query($con, $sql1);
echo "
<form action='confirmallowance.php' method='POST'>
<table class=data-table border='1' overflow-x=auto>
<caption class=title><b>Allowance Sheet<b></caption>
<thead>
<tr>
<th>Date</th>
<th>Allowance Month</th>
<th>Attendance Date</th>
<th>Day</th>
<th>Employee Code</th>
<th>Employee Name</th>
<th>InTime</th>
<th>OutTime</th>
<th>Work Duration</th>
<th>Team</th>
<th>Approver</th>
<th>Allowance type</th>
<th>Eligibility</th>
<th>Approved_amount</th>
<th>Status</th>
</tr>
</thead>
<tbody>";
if (mysqli_num_rows($result1) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result1)) {
echo '
<tr id="' . $row['id'] . '">
<td>' . $row['computation_date'] . '</td>
<td>' . $row['compute_month'] . '</td>
<td>' . $row['attendance_date'] . '</td>
<td>' . $row['attend_day'] . '</td>
<td>' . $row['emp_id'] . '</td>
<td>' . $row['emp_name'] . '</td>
<td>' . $row['time_in'] . '</td>
<td>' . $row['time_out'] . '</td>
<td>' . $row['duration'] . '</td>
<td>' . $row['team'] . '</td>
<td>' . $row['manager'] . '</td>
<td>' . $row['compensation_type'] . '</td>
<td>' . $row['eligibility'] . '</td>
<td>' . $row['amount'] . '</td>
<td>' . $row['status'] . '</td>
<td><input type="checkbox" name="checkbox[]" value="' . $row['id'] . '" checked="checked"></td>
<td>
<form action="editallowance.php" method="POST"><input type="hidden" name="tempid"
value="' . $row['id'] . '"/><input type="submit"
name="submit"
value="Edit"/>
</form>
</td>
</tr>
';
}
}
// Code snippet of the table value acceptance page on edit button click
if (isset($_POST["submit"]) && $_POST["submit"]) {
$editrow = $_POST['tempid'];
$sql = "SELECT * FROM `computation` WHERE `id` ='$editrow'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$att_date = $row['attendance_date'];
$att_day = $row['attend_day'];
$emp_id = $row['emp_id'];
$emp_name = $row['emp_name'];
$comptype = $row['compensation_type'];
$eligibility = $row['eligibility'];
$comment = $row['comment'];
$status = $row['status'];
echo "<P>Emp ID : " . $emp_id . "</p> <p>Emp Name :" . $emp_name . "</p><p>Attendance Date : " . $att_date . "</p><p>Day : " . $att_day . "</p>";
}
}
} else {
echo "No data";
If you need any addition info, please let me know.
Your html is not well formed. You are adding unused form open tag before table opening tag (there is not closing tag of it as well) in the first file:
<form action='confirmallowance.php' method='POST'>
<table class=data-table border='1' overflow-x=auto>
Please remove this form open tag html and it will work fine. It is interfering with the html forms that come later.
I have a page which displays some data imported in MySQL database from Excel file
I want to change background color of rows with same values (grouped) on col "Billing Doc" like this picture:
Here is a part of my php code:
<form action="uploadExcel.php" method="POST" enctype="multipart/form-data">
<div>
<input type="file" name="file">
<input type= "submit" value ="Upload" >
</div>
</form>
<table width="100%" border="1" cellpadding="5">
<tr>
<th>ID</th>
<th>Billing Doc</th>
<th>Invoice Date</th>
<th>Ordered Parts</th>
<th>Shipped Parts</th>
<th>Qty</th>
<th>F</th>
<th>G</th>
<th>Amount</th>
<th>Delivery No</th>
<th>D/O Creation Date</th>
<th>Description</th>
<th>P/O No</th>
<th>Ship-to</th>
<th>Tracking No</th>
<th>Obs</th>
</tr>
<?php
while ($row = $result->fetch_object()) {
if (substr( $row->tracking_no, 0, 3 ) === "534") {
$row->tracking_no = "<a href='http://www.dhl.be/en/express/tracking.html?pageToInclude=RESULTS&AWB=$row->tracking_no&type=fasttrack' title='DHL'>$row->tracking_no</a>";
}
if (substr( $row->tracking_no, 0, 3 ) === "730") {
$row->tracking_no = "<a href='http://www.tnt.com/webtracker/tracker.do?navigation=1&respLang=en&respCountry=gb&cons=$row->tracking_no' title='TNT'>$row->tracking_no</a>";
}
$colour = '';
echo '
<tr style="background-color:' .$colour. '">
<td>' . $row->id_factura . '</td>
<td>' . $row->billing_doc . '</td>
<td>' . $row->invoice_date . '</td>
<td>' . $row->ordered_parts . '</td>
<td>' . $row->shipped_parts . '</td>
<td>' . $row->qty . '</td>
<td>' . number_format($row->F, 2) . '</td>
<td>' . number_format($row->G, 2) . '</td>
<td>' . number_format($row->amount, 2) . '</td>
<td>' . $row->delivery_no . '</td>
<td>' . $row->d_o_creation_date . '</td>
<td>' . $row->description . '</td>
<td>' . $row->po_no . '</td>
<td>' . $row->ship_to . '</td>
<td>' . $row->tracking_no . '</td>
<td>' . $row->obs . '</td>
</tr>
';
}
?>
</table>
It seems that the only variable to consider is Billing Doc, if so, inside your while loop use an array to store the billing id as a key and then check every value to see if the key already exists then print the color you want to:
$billing_docs = [];
while ($row = $result->fetch_object()) {
echo '<tr style="background-color:' . (array_key_exists($row->id_factura, $billing_docs) ? '#666' : '#DDD') . '">...';
$billing_docs[$row->id_factura] = '';
}
You could create a variable with the first billing_doc value: $billing = $row->billing_doc and use an if else to echo the same color row if the $billing_doc value is the same. The else would declare the new color variable and echo rows of that color.
This is error appears when I try to open the exported file, when I click yes the file opens but has error above the table:
<?php require '../backend/dbcon.php';
$query=mysql_query( "SELECT * FROM membership");
$total=mysql_num_rows($query);
if ($total>0) { $output .= '
<table border="1">
<tr>
<th>NO</th>
<th>Names</th>
<th>Email</th>
<th>Phone</th>
<th>Amount(N$)</th>
<th>Payment</th>
</tr>'; while ($row = mysql_fetch_array($query)) { $output .= '
<tr>
<td>' . $row["id"] . '</td>
<td>' . $row["names"] . '</td>
<td>' . $row["personal_email"] . '</td>
<td>' . $row["personal_phone"] . '</td>
<td>' . 110 . '</td>
<td>' . $row["payment"] . '</td>
</tr>
'; } $output .= '
</table>';
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=Membe.xls"); echo $output; }