I have added following PHP code in my Joomla webapplication.
This will create a table of all users from a User Group.
How Can I make this table sortable?
Is there some Joomla/PHP code or plugin to order this list by Name?
Can I add a parameter to sort this by any column?
<?php
$teachers = JAccess::getUsersByGroup(10); //change number in the brackets
echo "<table class=\"table table-striped\">";
echo "<thead>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Street</th>";
echo "<th>Zip</th>";
echo "<th>City</th>";
echo "<th>Phone</th>";
echo "<th>Email</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach($teachers as $user_id) {
$user = JFactory::getUser($user_id);
$profile = JUserHelper::getProfile($user->id);
echo "<tr>";
echo "<td>".$user->name."</td>";
echo "<td>".$profile->profile['address1']."</td>";
echo "<td>".$profile->profile['postal_code']."</td>";
echo "<td>".$profile->profile['city']."</td>";
echo "<td>".$profile->profile['phone']."</td>";
echo "<td>".$user->email."</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
e.g.
if you want it make title sortable use code similar like follwing to add table headers
<?php echo JHTML::_('grid.sort', JText::_('TITLE'), 'title',
$this->lists['order_Dir'], $this->lists['order'] ); ?>
and use following function in modal to build your where clause in SQL.
getUserStateFromRequest(
$option.'filter_order_Dir', 'filter_order_Dir', 'ASC'));
above function will return you the ASC or DSC based upon user choice
JHTMLand JHTMLGRID are useful classes to build interactive tables
details page 279
I think the easiest way would be to use jquery or use a jquery plugin. You can use DataTables or Tablesorter, they are jquery plugins that would put that feature.
I assume you want the table to sortable on the client side, which means you will need some Javascript code. There are some jquery libraries out there that do that (like DataTables), but there also Joomla specific extensions that do exactly that, like Tabulizer for Joomla at http://www.tabulizer.com with many sorting options, plus you avoid any jQuery conflicts with other extensions. Here is a sorting demo http://www.tabulizer.com/index.php/support-menu/tabulizer-tips/63-sort-second-row
You can also use plugin Szaki Table developed directly for Joomla (currently available for J! 2.5 and J! 3.x).
Check it out, it's powerful and highly customizable tool.
Related
I am using woody snippets to insert php code to make an API call to return information in a table. How do I get the table to display using the default css from my wordpress theme?
I am new to PHP, but not to wordpress. And I know just enough CSS to get myself in trouble. So, I need ELI5 instructions.
My programmer provided the basic PHP code to call the API and return the data in a basic table. But, it looks horrid and there's no spacing or styling.
$tableStart = "<table>";
$tableEnd = "</table>";
$trStart = "<tr>";
$trEnd = "</tr>";
$tdStart = "<td >";
$tdEnd = "</td>";
$fname = 'alabama_energy_data.json';
$data = json_decode(file_get_contents($fname));
echo $tableStart;
foreach($data as $bill_data)
{
echo $trStart;
echo $tdStart . $bill_data->bill_id . $tdEnd . $tdStart . $bill_data->title . $tdEnd;
echo $trEnd;
}
echo $tableEnd;
This returns a basic table with the data I need, but it's not styled. How do I get it to use the default CSS from our wordpress site that it's displaying in so that the table renders in a format that looks decent?
If your table isn't styled like it should be, then you're probably missing and id or class attribute at your table. You could check on what current attributes your "wordpress" tables have, and just add them to your table.
Example:
<table class="wp-class-table">
# OR
<table id="wp-id-table">
And one other thing, I personally don't really like the way you "echo" your data. If you want, you can read this answer. It shows a nicer way to do it in my opinion.
running php 5.6.29 here.
Processing about 6,000 rows of data from a MYSQL Table. Looking at the HTML Description column and making changes. Using html_entity_decode to show the changed result in the Browser.
The problem is, each row is displaying the HTML result nested inside the previous row, which makes the display impossible. Obviously I want each row to have a clean break. As these are tables, it seems not to break?
while ($row = mysqli_fetch_array($result))
{
$i++;
echo "<br>$i";
echo "<br>Product: ".$row['id'];
echo "<br>Title: ".$row['title'];
echo "<br>";
$a = $row['html'];
echo "htmlspecialchars_decode($a)";
//...change the html process here ...
$b = html_entity_decode($row['html']);
echo "<br><br><br>".$b;
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
}
I checked this in multiple browsers, so it's not just Chrome. Basically I somehow need to close the HTML before I move onto the next row right? Or is this some kind of limitation or bug? tia.
I have an old PHP4 web app in which most of the pages looks like this(some pages has a left menu, some doesn't have a footer):
<?php
echo "<html>";
echo "<head><title>TITLE GOES HERE</title></head";
echo "<body>";
echo "<h2>THIS IS A TITLE</h2>";
// Here i fetch data from DB
echo "<table>";
echo "<tr>";
echo "</tr>";
foreach($rowsFromDB as $row) {
echo "<tr>";
// here i echo some <td> containing $row data
echo "</tr>";
}
echo "</table>";
echo "</body>";
echo "</html>";
?>
This is a simple example, the real ones contains a lot of spaghetti code (i'm italian, i like spaghetti but not in my code) and i'm trying to refactor/redesign it in some way. Rewrite the entire app from scratch (maybe with an MVC framework) is not an option because the app contains a lot of business logic i would like to keep.
My idea (for now) is to wrap the echos inside a renderer class, something like this:
<?php
class PageRenderer {
public static function renderHeader() {
echo "<html>";
echo "<head><title>TITLE GOES HERE</title></head";
echo "<body>";
echo "<h2>THIS IS A TITLE</h2>";
}
public static function renderContent($rowsFromDB) {
echo "<table>";
echo "<tr>";
echo "</tr>";
foreach($rowsFromDB as $row) {
echo "<tr>";
// here i echo some <td> containing $row data
echo "</tr>";
}
echo "</table>";
}
public static function renderFooter() {
echo "</body>";
echo "</html>";
}
}
$renderer=new PageRenderer();
$renderer->renderHeader();
// Fetch data from DB
$renderer->renderResults($rowsFromDB);
$renderer->renderFooter();
?>
The problem with the above solution is that is difficult to extend and maintain. Do you know any design pattern or any technique i could use for a better refactoring/redesign?
Thanks in advice and sorry for my bad english
I'd add a method, maybe call it renderColumn($tdParams = array()) which only has simple job of returning a single td element (as a string):
Initialize an empty string, $td_cell
Append to $td_cell an opening <td> tag, maybe accept an array of attributes and values for said td tag as paramater $tdParams which has been set a default value of an empty array.
Append to $td_cell a closing </td> tag.
return $td_cell
For rendering out your DB Rows, you may (at later point in time) have a query that has more, or less data points - thus will result in needing more or less td cells.
For your renderHeader method, I would add at least 2 parameters: title and maybe for the <h2> as you specified, as I can see that changing frequently.
I've got a data feed I'm importing that has a load of 'markets', I want to have a main page displaying all the markets, so for that id use a foreach loop to go through the data and on each market make a listing.
Each market has a bunch of attributes as well as nested participants, I want to then make a page for each market, that displays some information about each participant.
So the user would navigate to index.php > event.php?id101
This is the bit were ive become stuck, how can i send the user to the right page, I was thinking of using
<?php
$xml = simplexml_load_file('f1_feed.xml');
foreach($xml->response->williamhill->class->type->market as $event) {
$event_attributes = $event->attributes();
echo "<tr>";
// EVENT NAME WRAPPED IN LINK TO EVENT
echo "<td>";
echo '<a href="event.php?id=' . $market_id . '">';
echo $event_attributes['name'];
echo "</a>";
echo"</td>";
// DATE
echo "<td>";
echo $event_attributes['date'];
echo "</td>";
echo "</tr>";
}
?>
but how can I set a var $market_id (from the xml feed) to add to the end of the url, so it sends me to the right page ?
(f1_feed.xml is the same as the live xml feed, its just local for development)
the feed I'm using is http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N
which im bring in using simplexml
This worked for me;
$xml = simplexml_load_file("openbet_cdn.xml");
foreach($xml->response->williamhill->class->type->market as $market) {
// that gives an object (native)
$market_attributes = $market->attributes();
printf("%s\n",
$market_attributes->id,
$market_attributes->name);
// that gives an array (useless way!)
// $market_attributes = (array) $market->attributes();
// printf("%s\n",
// $market_attributes['#attributes']['id'],
// $market_attributes['#attributes']['name']);
}
I am trying to create a hyperlink from two pieces of text split over two cells in a table row.
I am generating my table using PHP to echo out the results from my database to a table.
When it echo's it generates a hyperlink with GET variables at the end which allow the user to visit a page relevant to that information.
The problem is that I can't seem to generate a hyperlink that will go across those table cells, I have looked around the web and there is nothing that says I cannot do this.
As you can see from the screenshot below I am generating a hyperlink inside one table cell but I want the other table cell to have the same hyperlink.
Code
while ($row = $db->fetch_assoc($newest))
{
echo "<tr>";
echo "<td>";
echo "<a href='manager.php?method=view&id=".$row['id']."'>".$row['first_name']." ". $row['second_name']. "</td><td>".$row['company_name']."</a>";
echo "</td>";
echo "</tr>";
}
I have a feeling that I will just have to generate two separate hyperlinks for the table cells.
However I am hoping someone here can prove me wrong and save me a few lines of code.
Thanks :)
Using native hyperlinks, you will have to create separate wrappers for each cell.
However, if you want to use JS for linking and redirecting, you could do something like:
.....
<tr class="clickable" data-href="http://google.com">
<td>cell-1</td>
<td>cell-2</td>
<td>cell-3</td>
</tr>
....
and then:
$(function(){
$('tr.clickable').click(function(){
window.location.href = $(this).attr('data-href');
});
});
Simply work around it with JS:
echo "<tr onclick=\"location.href='manager.php?method=view&id=".$info.";'\">";
If you do not want to break the table structure (ie. putting the name and the company into one (multi-column) cell), there is IMHO no way other than generating two hyperlinks.
What you might want to do is to use some CSS for a hover effect and some JavaScript to register a user clicked on a cell (which you can, given the structure above, associate with the tr element).
You can not do it like this. Try instead:
while ($row = $db->fetch_assoc($newest))
{
$url = "manager.php?method=view&id=".$row['id'];
echo "<tr>";
echo "<td>" . $row['first_name']." ". $row['second_name']. "</td>";
echo "<td>" . $row['company_name'] . "</td>";
echo "</tr>";
}
while ($row = $db->fetch_assoc($newest))
{
echo "<tr>";
echo "<td>";
echo "<a href='manager.php?method=view&id=".$row['id']."'>".$row['first_name']." ". $row['second_name']."</a></td><a href='manager.php?method=view&id=".$row['id']."'>".$row['company_name']."</a><td></td>";
echo "</td>";
echo "</tr>";
}