I have successfully put together a PHP script to read content of an xml file and output the result to HTML page. The only bit I am struggling with is how to format the output into a table.
PHP Script:
<?php
// Loading the XML file
$xml = simplexml_load_file("ftpxml.xml");
echo "<h2>".$xml->getName()."</h2><br />";
foreach($xml->children() as $ftpxml)
{
echo "PID : ".$ftpxml->attributes()->pid."<br />";
echo "Account : ".$ftpxml->attributes()->account." <br />";
echo "Time : ".$ftpxml->attributes()->time." <br />";
echo "<hr/>";
}
?>
HTML Result:
PID : 279
Account : account001
Time : 137
----------------------------------------------------------------
PID : 268
Account : account002
Time : 301
----------------------------------------------------------------
PID : 251
Account : account003
Time : 5
----------------------------------------------------------------
I am lost as to how to display each table headings and the corresponding contents. I am new to PHP so please guide me or if already answered else where, please provide link so I can learn from it.
Thanks
<?php
// Loading the XML file
$xml = simplexml_load_file("ftpxml.xml");
echo "<h2>".$xml->getName()."</h2><br />";
echo "<table>";
foreach($xml->children() as $ftpxml)
{
echo "<tr><td>PID : ".$ftpxml->attributes()->pid."</td></tr>";
echo "<tr><td>Account : ".$ftpxml->attributes()->account." </td></tr>";
echo "<tr><td>Time : ".$ftpxml->attributes()->time." </td></tr>";
}
echo "</table>";
?>
echo '<table>';
echo '<thead><tr><th>PID</th><th>Account</th><th>Time</th></tr></thead><tbody>';
foreach($xml->children() as $ftpxml)
{
echo '<tr>';
echo "<td>PID : ".$ftpxml->attributes()->pid."</td>";
echo "<td>Account : ".$ftpxml->attributes()->account."</td>";
echo "<td>Time : ".$ftpxml->attributes()->time." </td>";
echo '</tr>';
}
echo '</tbody></table>';
I assume you want to make the different values different fields in the table:
// Loading the XML file
$xml = simplexml_load_file("ftpxml.xml");
echo "<h2>".$xml->getName()."</h2><br />";
echo "<table><thead><tr><th>PID</th><th>Account</th><th>Time</th></tr></thead><tbody>";
foreach($xml->children() as $ftpxml)
{
echo '<tr>';
echo '<td>' . $ftpxml->attributes()->pid . "</td>";
echo '<td>' . $ftpxml->attributes()->account . "</td>";
echo '<td>' . $ftpxml->attributes()->time . "</td>";
echo '</tr>';
}
echo '</tbody></table>';
Related
I have been following some courses on how to create a session object, which has worked out fine, If I place the complete code onto a PHP file, all works great!
What I would like to do is place this in another module (PHP file) and just use one line (or equivalent) to do this, such as GetSessiondata();
<?php
$SqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");
$UserDataSet = $SqlCon->GetUserList("SELECT * FROM Users");
echo "<br /><br />";
echo "<br /><br />";
if ($UserDataSet)
{
echo "<table>" . "<thead>" ;
echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";
foreach($UserDataSet as $data)
{
echo "<td>" .$data->GetUsrName()."</td>" ;
echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;
}
echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
echo "</tbody>" . "</table>" ;
}
else
echo "Nothing Found in DB!";
?>
You need to "require" your file, where you want to use it.
Here an example
Working with classes:
Whatever.php
class Whatever {
public function __construct() {
// Ever when the code is instantiated, this will be called too!
}
public function myMethod() {
echo 'hello';
}
}
index.php
require_once('./Whatever.php');
$whatever = new Whatever();
$whatever->myMethod();
Without classes:
functions.php:
function whatever(){ echo 'hello'; }
index.php:
require_once('./functions.php');
whatever();
Read more:
Require: http://php.net/manual/es/function.require.php
Require_once: http://php.net/manual/es/function.require-once.php
My advise is to split this refactoring process into 2 steps:
1.Wrap your code into function:
function someFunctionName() {
$SqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");
$UserDataSet = $SqlCon->GetUserList("SELECT * FROM Users");
echo "<br /><br />";
echo "<br /><br />";
if ($UserDataSet)
{
echo "<table>" . "<thead>" ;
echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";
foreach($UserDataSet as $data)
{
echo "<td>" .$data->GetUsrName()."</td>" ;
echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;
}
echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
echo "</tbody>" . "</table>" ;
}
else
echo "Nothing Found in DB!";
}
// and call your function
someFunctionName();
2.Create another file, let's say functions.php, in the same dir and move function into it. Now you can require this file inside your php page:
require_once 'functions.php';
// and call your function
someFunctionName();
I think you are looking for include
Save your file, and then you can include it in another file as:
include 'my_file.php';
You can also use:
include_once
require
require_once
Read the documentation for further explanations or see this question
I'd like to add an active class with PHP instead of using javascript.
But I probably broke the code somewhere.
I'm using bootstrap.js version 3 and I'm building my tabs using PHP because I need to use my tabs on the control panel in my app.
So essentially I didn't add any javascript or jquery so far, I'm just using the native functions for tabs in bootstrap.js (and jquery):
<?php foreach ($database as $item): ?>
<?php
if (isset($item['associations'])) {
foreach ($item['associations'] as $value) {
echo "<div class='modal' id='des_" . $value['des_id'] . "'>";
echo "<div class='modal-sandbox'></div>";
echo "<div class='modal-box'>";
echo "<div class='modal-body'>";
echo "<div class='close-modal'>✖</div>";
echo "<h3>Credit and values</h3>";
echo "<h5>" . $value["designation_name"] . " - Credit: " . $value["designation_credits"] . "</h5>";
echo "<ul class='nav nav-pills'>";
if (isset($value['credits']) && !is_string($value['credits'])) {
foreach ($value['credits'] as $credits) {
$first_element = reset($value["credits"]);
if (isset($value['credits'])) {
if ($first_element[4] == $credits[4]) {
echo "<li class='active'><a href='tab_" . $credits[4] . "' data-toggle='tab'>Identifier: " . $credits[0] . "</a></li>";
} else {
echo "<li><a href='tab_" . $credits[4] . "' data-toggle='tab'>Identifier: " . $credits[0] . "</a></li>";
}
}
}
}
echo "</ul>";
echo "<div class='tab-content clearfix'>";
if (isset($value['credits']) && !is_string($value['credits'])) {
foreach ($value['credits'] as $credits) {
if (isset($value['credits'])) {
if ($first_element[4] == $credits[4]) {
echo "<div class='tab-pane active' id='tab_" . $credits[4] . "'>";
} else {
echo "<div class='tab-pane' id='tab_" . $credits[4] . "'>";
}
echo "<div class='single-credit'>";
?>
<?=form_open();?>
<?php
echo "<input type='hidden' name='id' value='" . $credits[4] . "'>";
echo "<div>Identifier: <span>" . $credits[0] . "</span></div>";
echo "<input name='edit_credits' type='submit' value='Edit' class='submit'>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
<?=form_close()?>
<?php
}
}
}
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
}
?>
<?php endforeach;?>
To explain my code I will start from the list:
Since it's a really complex array, I'm taking the first element with the reset function to add the active class only on that very element.
Then I added the href element to point to the id of the matching div later on, and then I close my list.
Later on, I create another loop and I use the id to match both the list and the div and I used the trick that I used before to add the class active only on my first element of my array.
Everything works except for the fact that, if I click another element of the list, the matching div won't work, the very first element of the array will keep the class active, so I can't switch beteween element.
I need to spot the error.
UPDATE:
The problem that I have with jquery, in this case, is that if I use a toogleClass for example like that:
$(".nav-pills li").click(function(e) {
e.preventDefault();
tab = $(this).find('a').attr('href');
$("#" + tab).toggleClass('active');
});
It won't be able to toggle properly the class on the first element, that one to which I added the class active with php.
I think you miss to change the URL when click it, in the backend (PHP) you need to know where the user is, so, you check the "parameter" and mark active or not the current tab.
For example:
printf("<div class='tab-pane %s' id='...'>", $credits[4] == $_GET['tab'] ? 'active':'');
I want to show only one time the heading of parts of speeches for example when user enter the word "Spell" all the parts of speeches and its meaning come every time .
The headings are repeating multiple times . i want to print only one time
There should be a heading Of Noun,Verb,Adjective etc..
like this
Noun.
Verb.
adjective.
this is my code
while($row=mysql_fetch_array($sql)){
echo "<div id='wordid' style='display:none'>".$row["wordid"]."</div>";
echo $count++." : ".$row['definition']. " ";
if($row['pos']=="n"){
echo "(Noun) <br/>";
}
if($row['pos']=="s"){
echo "(Subject) <br/>";
}
if($row['pos']=="p"){
echo "(Proverb) <br/>";
}
if($row['pos']=="a"){
echo "(Adjective) <br/>";
}
if($row['pos']=="v"){
echo "(Verb) <br/>";
}
}
You can keep track of what the last wordid was and only echo the header if it changed.
// initialize to avoid notices
$lastWordId = '';
while($row=mysql_fetch_array($sql)){
if ($lastWordId != $row['wordid']) {
echo "<div id='wordid' style='display:none'>".
$row["wordid"].
"</div>";
$lastWordId = $row['wordid'];
}
echo $count++." : ".$row['definition']. " ";
if($row['pos']=="n"){
echo "(Noun) <br/>";
}
if($row['pos']=="s"){
echo "(Subject) <br/>";
}
if($row['pos']=="p"){
echo "(Proverb) <br/>";
}
if($row['pos']=="a"){
echo "(Adjective) <br/>";
}
if($row['pos']=="v"){
echo "(Verb) <br/>";
}
}
I have a code with an array that saves what i need each time I press a button, so these items saved into an array I showed later with a erase buttons, but I don't know how to delete it, so there is the part of the code that shows what I meant:
echo "<table border=1>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
for ($x=0;$x<count($savedArray[4]);$x++){
echo "<tr>";
echo " <td>".$savedArray[0][$x]."</td>";
echo " <td>".$savedArray[1][$x]."</td>";
echo " <td>".$savedArray[2][$x]."</td>";
echo " <td>".$savedArray[3][$x]."</td>";
echo " <td>".$savedArray[4][$x]."</td>";
echo " <td><input type='submit' onclick='eliminar(".$savedArray[0][$x].",".$savedArray[1][$x].",".$savedArray[2][$x].",".$savedArray[3][$x].",".$savedArray[4][$x].")' class='carritoElim' value='elim'></td>";
echo "</tr>";
}
a pic with all the forms:
the other pic that shows the items on the array:
Anyone knows how to delete the selected row that references the item on the array with the delete button? Thanks
In the client side, the following code works.
for ($x=0;$x<count($savedArray[4]);$x++){
echo '<tr id="line' . $x . '">';
echo " <td>".$savedArray[0][$x]."</td>";
echo " <td>".$savedArray[1][$x]."</td>";
echo " <td>".$savedArray[2][$x]."</td>";
echo " <td>".$savedArray[3][$x]."</td>";
echo " <td>".$savedArray[4][$x]."</td>";
echo " <td><input type='submit' onclick='eliminar('line" . $x . "')' class='carritoElim' value='elim'></td>";
echo "</tr>";
}
function eliminar($id) {
var elem = document.getElementById($id);
elem.parentNode.removeChild(elem);
}
However, if you want to delete column in the server side also, the ajax codes should be added.
php is a server side language, what meant once your response is sent out to the client(browser), under most case all your "saved" data/variables will be lost.
I am not a fan of what your language of choice for this task but here is a way to work around.
The trick is to scan the whole table of data when the user clicked the eliminate button, then send all the data back to the sever and recreate the array with the selected row taken out. Then print the whole array back to html format and send it to user again.
The critical part to of this method is that you NEED to print everything you wish to retain in every response, so that you could scan them all back in to your server side program.
I've some sample code for your reference, but I didn't pay much attention when I wrote them so expect bugs and incorrect syntax, this is just for pure demostration purpose to show you the idea.
the sample code to print the table you've shown:
echo "<form action='".$_PHP_SELF."' method='post'>";
echo "<input type='hidden' name='type' value='yourarrayname'/>";
echo "<table border=1>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
for ($x=0;$x<count($savedArray[4]);$x++){
if(strcasecmp($savedArray[0][$x],"-999")!=0){
echo "<tr>";
echo "<td><input type='text' name='0_".$x."' value='".$savedArray[0][$x]."'/></td>";
echo "<td><input type='text' name='1_".$x."' value='".$savedArray[1][$x]."'/></td>";
echo "<td><input type='text' name='2_".$x."' value='".$savedArray[2][$x]."'/></td>";
echo "<td><input type='text' name='3_".$x."' value='".$savedArray[3][$x]."'/></td>";
echo "<td><input type='text' name='4_".$x."' value='".$savedArray[4][$x]."'/></td>";
echo "<td><button name='elim' value='e".$x."' type='submit'>elim</button></td>";
echo "</tr>";
}
}
echo "</table>";
echo "</form>";
and here's the sample code to handle the incoming data, put this after you created the array but before the printing code:
if($_POST!=null){
if(strcasecmp($_POST['type'],"yourarrayname")==0){
for ($x=0;$x<count($savedArray[4]);$x++){
if(strcasecmp($_POST['elim'],"e".$x)!=0){
$savedArray[0][$x] = $_POST['0_'.$x];
$savedArray[1][$x] = $_POST['1_'.$x];
$savedArray[2][$x] = $_POST['2_'.$x];
$savedArray[3][$x] = $_POST['3_'.$x];
$savedArray[4][$x] = $_POST['4_'.$x];
}
if(strcasecmp($_POST['elim'],"e".$x)==0){
$savedArray[0][$x] = "-999";
$savedArray[1][$x] = "-999";
$savedArray[2][$x] = "-999";
$savedArray[3][$x] = "-999";
$savedArray[4][$x] = "-999";
}
}
}
}
I have a php page that will grab data from an SQL database and print it into a table, now if certain rows from the database have a certain parameter, a hidden row is inserted below that with a button to expand the row. This all works fine and I can query it with ajax into a div on a different page, but if someone were to expand the hidden row (with jquery) and the ajax update timer were to come along, it would reset and hide that row.
Here is the page that it is filled into and the ajax code to go with it:
<script type="text/javascript">
$(document).ready(function() {
$("#query").load("query.php");
var refreshId = setInterval(function() {
$("#query").load('query.php');
}, 5000);
$.ajaxSetup({ cache: false });
});
</script>
<div id="query"></div>
And from the query.php:
while($row = mysql_fetch_array($result))
{
$decoded = json_decode($row['B'],true);
$r = $decoded['r'];
$t = $decoded['t'];
$l = $decoded['l'];
$g = $decoded['g'];
$tp = (int)$t + 3;
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td align=\"center\"><font color=\"red\">[$t]</font></td>";
echo "<td align=\"center\"><font color=\"blue\">[$r]</font></td>";
echo(((int)$t != 0) ? '<td><button class="expand stylebutton">+</button>' : '<td>');
echo $row['Name_'] . "</td>";
echo "<td>" . $row['Date_'] . "</td>";
echo "<td>" . $row['IP'] . "</td>";
echo "<td>" . $row['G'] . "</td>";
echo "<td>" . $row['A'] . "</td>";
echo "<td>" . $row['Add'] . "</td>";
echo "<td>" . $row['Remove'] . "</td>";
echo "<td>" . $row['Comments'] . "</td>";
echo "</tr>";
if((int)$t != 0)
{
echo "<tr class=\"b\"style=\"display: none;\">";
echo "<td></td><td colspan=\"$tp\"><div style=\"color: #FC0;\"> Local:</div>";
foreach($l as $ll)
{
echo $ll . "<br>";
}
echo "<br><div style=\"color: #F00;\">Global:</div>";
foreach($g as $gg)
{
echo $gg . "<br> ";
}
echo "</td></tr>";
}
}
echo "</table>";
echo "<script type=\"text/javascript\">";
echo "$(document).ready(function() {
$(\".stylebutton\").click(function(){
$(this).parent().parent().next().toggle();
if($(this).text() == \"+\")
{
$(this).text('-');
}
else
{
$(this).text('+');
}
});
});
</script>";
Here's an image of what one of the rows looks like when expanded.
So basically I don't want the rows to contract when the ajax update rolls through, but instead just add more data on to the page. Thanks for any help!
You should have the php file return the data in json format to be used by your jQuery. Instead of replacing the html tags and causing your UI to retract, you will replace the data.
For Example
Your php code:
$data_array = array();
while($row = mysql_fetch_array($sql)) {
array_push( $row['some_row'] , $data_array ); // add the data to an array that will later be json encoded
}
echo json_encode($data_array);
jQuery
$.ajax({
url: 'query.php',
success: function(data) {
alert('This is the JSON: ' + JSON.stringify(data));
$('#selector').text(data.some_row); // place the data inside the element with id = 'selector'
}
});