I have JSON data I am pulling in:
{
"apiVersion": "0.1",
"data": {
"roomCount": 12,
"rooms": [
{
"roomNumber": "204",
"occupied": false,
"permissions": {
"roomCharges": true,
"adultContent": true
}
},
{
"roomNumber": "205",
"occupied": true,
"permissions": {
"roomCharges": false,
"adultContent": false
}
},
{
"roomNumber": "206",
"occupied": true,
"permissions": {
"roomCharges": false,
"adultContent": false
}
},
{
"roomNumber": "207",
"occupied": true,
"permissions": {
"roomCharges": false,
"adultContent": false
}
},
{
"roomNumber": "208",
"occupied": true,
"permissions": {
"roomCharges": false,
"adultContent": false
}
},
{
"roomNumber": "209",
"occupied": true,
"permissions": {
"roomCharges": false,
"adultContent": false
}
and I can format it just fine. using JSON_Decode and then displaying the $output in my <table> like so:
<table>
<?php foreach($output['data']['rooms'] as $info): ?>
<tr>
<td><?php echo $info['roomNumber'] ?></td>
<td><?php echo ($info['occupied'] == 1) ? 'OCCUPIED' : 'VACANT'; ?></td>
<td><?php echo ($info['permissions']['roomCharges'] == 1) ? 'ENABLED' : 'DISABLED'; ?></td>
<td><?php echo ($info['permissions']['adultContent'] == 1) ? 'ENABLED' : 'DISABLED'; ?></td>
<td><?php echo '<a class="clickhere" href="display_room_report.php?id=' . $info['roomNumber'] .'">Click Here</a>'?></td>
</tr>
<?php endforeach; ?>
</table>
But my problem is, as I have multiple php pages using this same table, I'd like to create a dynamic way to set variables, and then call out those variables in multiple locations. My goal is to edit the JSON parameters in one location and have all php pages update, rather than updating every single key=>value over several php pages. (something like this to set each one to a variable).
<?php foreach($output['data']['rooms'] as $info){
$roomNumber = $info['roomNumber'];
$occupied = ($info['occupied'] == 1) ? 'OCCUPIED' : 'VACANT';
$roomCharges = ($info['permissions']['roomCharges'] == 1) ? 'ENABLED' : 'DISABLED';
$adultContent = ($info['permissions']['adultContent'] == 1) ? 'ENABLED' : 'DISABLED';
$roomReport = '<a class="clickhere" href="display_room_report.php?id=' . $info['roomNumber'] .'">Click Here</a>';
}
?>
And in my table display:
<tr>
<td><?php echo $roomNumber ?></td>
<td><?php echo $occupied ?></td>
<td><?php echo $roomCharges ?></td>
<td><?php echo $adultContent ?></td>
<td><?php echo $roomReport ?></td>
</tr>
Making it a little more dynamic. So in case the JSON key and values change, I can change them in one location i.e. ['permissions']['adultContent'] might turn into ['guestValue']['RoomAuthorized']. I could make the change in one location, and the remaining pages would automatically update.
The problem i'm running into, is that while I have it semi-working with the variables plugged into the <td>, like so:
<tr>
<td><?php echo $roomNumber ?></td>
<td><?php echo $occupied ?></td>
<td><?php echo $roomCharges ?></td>
<td><?php echo $adultContent ?></td>
<td><?php echo $roomReport ?></td>
</tr>
I am getting the same key and value for each line item. Example:
204 VACANT ENABLED ENABLED Click Here
204 VACANT ENABLED ENABLED Click Here
204 VACANT ENABLED ENABLED Click Here
204 VACANT ENABLED ENABLED Click Here
204 VACANT ENABLED ENABLED Click Here
When the original table I use, brings back the data correctly, like so:
table>
<?php foreach($output['data']['rooms'] as $info): ?>
<tr>
<td><?php echo $info['roomNumber'] ?></td>
<td><?php echo ($info['occupied'] == 1) ? 'OCCUPIED' : 'VACANT'; ?></td>
<td><?php echo ($info['permissions']['roomCharges'] == 1) ? 'ENABLED' : 'DISABLED'; ?></td>
<td><?php echo ($info['permissions']['adultContent'] == 1) ? 'ENABLED' : 'DISABLED'; ?></td>
<td><?php echo '<a class="clickhere" href="display_room_report.php?id=' . $info['roomNumber'] .'">Click Here</a>'?></td>
</tr>
<?php endforeach; ?>
</table>
And brings back:
204 VACANT ENABLED ENABLED Click Here
205 OCCUPIED DISABLED DISABLED Click Here
206 OCCUPIED DISABLED DISABLED Click Here
207 OCCUPIED DISABLED DISABLED Click Here
208 OCCUPIED DISABLED DISABLED Click Here
Do you have any idea on how I can make this more dynamic? Thank you for checking out my question. Please let me know if you need anything else to assist me. I tried to explain it clearly, let me know if you need anything further.
As far as I can read, the question is that you want to reuse the variables several places, but if the JSON changes you only need to edit it in one central location.
To accomplish this you can use mappings to map a variable in one place to local variable of your choosing. In line five and so on in the first code here, you change choose any name you want for your local array. If the variable in JSON changes, just change that one and your local mapping is still the same. Now you only have to change the code in the function get_data_in_my_format().
Create a data_parser.php or something like that
<?php
function get_data_in_my_format(){
$input = json_decode('your data here', true);
$output = array();
foreach($input['data']['rooms'] as $info){
$temp['roomNumber'] = $info['roomNumber'];
$temp['occupied'] = ($info['occupied'] == 1) ? 'OCCUPIED' : 'VACANT';
$temp['roomCharges'] = ($info['permissions']['roomCharges'] == 1) ? 'ENABLED' : 'DISABLED';
$temp['adultContent'] = ($info['permissions']['adultContent'] == 1) ? 'ENABLED' : 'DISABLED';
$temp['roomReport'] = '<a class="clickhere" href="display_room_report.php?id=' . $info['roomNumber'] .'">Click Here</a>';
$output[] = $temp;
}
return $output;
}
//Only insert following line if you want to run the function no matter what
$data_in_my_format = get_data_in_my_format();
In the file you want to use this code
<?php
include('data_parser.php');
//If you did not initiate $data_in_my_format in data_parser.php, do it here
//$data_in_my_format = get_data_in_my_format();
foreach($data_in_my_format as $room){
?>
<tr>
<td><?php echo $room['roomNumber'] ?></td>
<td><?php echo $room['occupied'] ?></td>
<td><?php echo $room['roomCharges'] ?></td>
<td><?php echo $room['adultContent'] ?></td>
<td><?php echo $room['roomReport'] ?></td>
</tr>
<?php
}
Is this what you wanted?
EDIT: Changed the description in beginning
Although it's not shown in your code it seems like you are looping through the items a second time outside the first loop which sets the variables thus, repeating the initial set values without actually "updating" the variables to correspond to the values in that row.
A simple loop including the variable declaration and the table output should do the trick:
<?php
foreach($output['data']['rooms'] as $info){
$roomNumber = $info['roomNumber'];
$occupied = ($info['occupied'] == 1) ? 'OCCUPIED' : 'VACANT';
$roomCharges = ($info['permissions']['roomCharges'] == 1) ? 'ENABLED' : 'DISABLED';
$adultContent = ($info['permissions']['adultContent'] == 1) ? 'ENABLED' : 'DISABLED';
$roomReport = '<a class="clickhere" href="display_room_report.php?id=' . $info['roomNumber'] .'">Click Here</a>';
?>
<tr>
<td><?php echo $roomNumber ?></td>
<td><?php echo $occupied ?></td>
<td><?php echo $roomCharges ?></td>
<td><?php echo $adultContent ?></td>
<td><?php echo $roomReport ?></td>
</tr>
<?php
}
?>
Related
<?php if($result["staff_follow"]== 'By Party'&& $result["Status"]== 'PENDING'): ?> style="background-color:#E99E3E;
the below feiled is not working.
"onmouseover="this.bgColor='litegreen'" onmouseout="this.bgColor='#E99E3E'"
<?php elseif($result["Status"]== 'PENDING'): ?>style= "background-color:#DB7093; "
<?php endif; ?> >
<!-- <td><?php echo $result["Roll_No"];?></td> -->
<td style="text-align: center;"><?php echo $counter ;?></td>
<td><?php echo $result["Document_Name"];?></td>
<td><?php echo $result["Document_Num"];?></td>
<td><?php echo $result["Extent"];?></td>
<td><?php echo $result["Status"];?></td>
<td><?php echo $result["Document_App_Det"];?></td>
<td><?php echo $result["staff_follow"];?></td>
Try again with this.bgColor='LightGreen'.
The named colors can be found here:
https://htmlcolorcodes.com/color-names/
I want to have a 'popup' div whenever I click on a row. I tried to do this with javascript to toggle a class.
We are filling a table with WordPress custom post types and the number of rows are therefor unknown.
The id "bedrijfsinfo-x" is being generated correctly, and function counts the right amount of rows. It still toggles the class on all divs instead of just the one associated with the row I clicked on.
Any help would be greatly appreciated!
<?php $counter = 1 ; ?>
<?php
// Als er berichten zijn gevonden.
if ( $posts->have_posts() ) {
// While loop om alle berichten op te halen
while ( $posts->have_posts() ) :
$posts->the_post();
global $post;
?>
<tr id="su-post-<?php the_ID(); ?>" class="su-post contracten">
<td class="meta_data" onclick="showInfo()"><?php bedrijfsnaam(); ?></td>
<td><?php naam_contactpersoon(); ?></td>
<td><?php url(); ?></td>
<td><?php start(); ?></td>
<td><?php eind(); ?></td>
<td><?php frequentie(); ?></td>
<td><?php datum_onderhoud(); ?></td>
</tr>
<div id="bedrijfsinfo-<?php echo $counter?>" class="infobox">
<p>Bedrijfsinformatie<br><?php bedrijfsnaam() . url() . start() . eind(); ?></p>
<button onclick="showInfo()"><p class="button">Sluiten</p></button>
</div>
<?php $counter = $counter + 1; ?>
<?php endwhile; }
<script>
function showInfo() {
var tr_count = document.getElementsByTagName("tr").length;
for (var i = 1; i < tr_count; i++) {
var popup = document.getElementById("bedrijfsinfo-"+ i);
popup.classList.toggle("show");
}
}
</script>
Your showInfo function iterates over the entire number of rows. Sounds like that's not what you want. Maybe try passing in the id of the thing clicked?
<td class="meta_data" onclick="showInfo(<?php $counter; ?>)"><?php bedrijfsnaam(); ?></td>
// ...
<button onclick="showInfo(<?php $counter; ?>)"><p class="button">Sluiten</p></button>
function showInfo(i) {
var popup = document.getElementById("bedrijfsinfo-"+ i);
popup.classList.toggle("show");
}
i need help. i have a table that i need to view the data of the user that already doing report. when the technician update the form, user can know who is the responsible person for their report. upon update the technician will send the id to the database but the problem is the user will see the id when they check the report. i need to change the id to the technician name.
<tr bgcolor="#00aeef">
<td>Tarikh</td>
<td>Staff ID</td>
<td>Kerosakan</td>
<td>Nyatakan</td>
<td>Maklumat Kerosakan</td>
<td>Lokasi Kerosakan</td>
<td>Nama Pegawai</td>
<td>Status</td>
<td>Catatan</td>
</tr>
<?php include('../dbcon.php');
$username=$_SESSION['username'];
$username_view=$_SESSION['username_view'];
$qry=mysql_query("SELECT * FROM aduan_form where staff_id='$username' ORDER BY tarikh DESC") or die(mysql_error());
?>
<?php while ($row_view = mysql_fetch_array($qry)){ ?>
<?php if ($totalRows_view > 0) { // Show if recordset not empty ?>
<?php } // Show if recordset not empty ?>
<tr>
<td><?php echo $row_view['tarikh']; ?></td>
<td><?php echo $row_view['staff_id']; ?></td>
<td><?php echo $row_view['kerosakan']; ?></td>
<td><?php echo $row_view['nyatakan']; ?></td>
<td><?php echo $row_view['m_kerosakan']; ?></td>
<td><?php echo $row_view['tempat_rosak']; ?></td>
<td><?php echo $row_view['nama_pegawai']; ?></td>
<td><?php echo $row_view['status']; ?></td>
<td><?php echo $row_view['catatan']; ?></td>
<?php } while ($row_view = mysql_fetch_assoc($view)); ?></tr>
</table>
here is my current code.
i do try to put some code that will allow the id to change to the name but it view the same name and it is even in the table that is still not been update.
<?php include('../dbcon.php');
$username=$_SESSION['username'];
$username_view=$_SESSION['username_view'];
$qry1=mysql_query("SELECT * FROM login,aduan_form where aduan_form.nama_pegawai=login.username ORDER BY aduan_form.tarikh DESC") or die(mysql_error());
?>
<?php while ($row_view1 = mysql_fetch_array($qry1)){ ?>
how should i do to make it right? i need the table to view the right technician name based on the right id and also display the name only in the table that already been update.
Hope I got your database schema right. So you can query like:
$qry1=mysql_query("
SELECT aduan_form.*, login.username
FROM aduan_form
LEFT JOIN login
ON login.id = aduan_form.staff_id
WHERE aduan_form.nama_pegawai=login.username
ORDER BY aduan_form.tarikh DESC")
or die(mysql_error());
and change your html output to something like:
...
<td><?php echo $row_view['tarikh']; ?></td>
<td><?php echo $row_view['username']; ?></td>
<td><?php echo $row_view['kerosakan']; ?></td>
...
I got the following php foreach loop in the A page of my web appliation. And i want to use the $watson variable which is inside this loop in an other php B page. What i did is used this variable as a value in an extra input button:<input type="button"/ value="<?php echo $watson; ?>,so to be able to capture this var in javascript like you see below.
<tbody>
<?php
foreach($records as $r) {
$watson = $r->case_reference;
?>
<tr>
<td><?php echo escape($r->user); ?></td>
<td><?php echo escape($r->customer); ?></td>
<td><?php echo escape($r->vessel); ?></td>
<td><?php echo escape($r->country); ?></td>
<td><?php echo escape($r->port); ?></td>
<td><?php echo escape($r->eta); ?></td>
<td><?php echo escape($r->service_station); ?></td>
<td><?php echo escape($r->type_of_service); ?></td>
<td><?php echo escape($r->case_reference); ?></td>
<td><?php echo escape($r->status); ?></td>
<td><input type="button" value="<?php echo $watson; ?>" onclick="popEdit(this.value);" /></td>
</tr>
<?php
}
?>
</tbody>
The code for the onclick="popEdit(this.value);"function which pops up the B page mentioned above is :
function popEdit(str) {
window.open("example.php?watson="+str, "pop_edit_jobs",
"width=400,height=350,scrollbars=no,left=460,top=400")
}
And the php B page code starts as $ref_num = $_GET['watson']; so i can use the variable.
My problem is that i dont want the input/button in the A page to show the variable on it instead i want it to just show the word EDIT.But to do that i ll have to change its value and then i wont be able to capture this $watson var in javascript.
Any suggestions regarding how to achieve the above effect?
<td><input type="button" value="EDIT" onclick="popEdit('<?php echo $watson; ?>');" /></td>
should achieve the desired effect.
Wrap a form around your entire table, then use submit buttons:
<input type="submit" name="watson" value="<?php echo htmlspecialchars($watson); ?>" />
I need some help.
I used echo to display the ID but after I click each ID, the popup window appeared and will display the same page for all ID. I want to display a different page for each ID. How can I solve this? Thank you..
<td><a href="d1.php" onClick="javascript:void
window.open('d1.php','1382580890418','width=800,height=600,toolbar=1,menubar=1,location=1,s
tatus=1,scrollbars=1,resizable=1,left=0,top=0');return false;"><?php echo $row["ID"];
?></a></td>
<td><?php echo $row["NO"]; ?></td>
<td><?php echo $row["CATEGORY"]; ?></td>
<td><?php echo $row["TYPE"]; ?></td>
<td><?php echo $row["BRAND"]; ?></td>
<td><?php echo $row["MODEL"]; ?></td>
<td><?php echo $row["SERIAL"]; ?></td>
<td><?php echo $row["YEAR"]; ?></td>
<td><?php echo $row["DUEDATE"]; ?></td>
<td><?php echo $row["REGION"]; ?></td>
<td><?php echo $row["STATUS"]; ?></td>
Try with passing different name every row like :
<td>
<?php echo $row["ID"]; ?>
</td>
Different window name for every row by passing the row id as window names.
Ideally all rows should be handled by single window, by setting your logic at d1.php, but I still this can be a solution.