When you want to display images dynamically, if you want to make sure they actually exist, you can do fairly easily directly in XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="root">
<html>
<head></head>
<body>
<!-- We suppose node 'id' contains part of the filename -->
<xsl:variable name="imgSrc" select="boolean(document(string(concat('http://www.sillysmart.org/img/users/', id, '.jpg'))))" />
<xsl:choose>
<xsl:when test="$imgSrc">
<img src="{concat('http://www.sillysmart.org/img/users/', id, '.jpg')}" alt="{id}" />
</xsl:when>
<xsl:otherwise>
<img src="http://www.sillysmart.org/img/404.jpg" alt="Not found" />
</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Suppose the following XML feed listing a set of users:
<?xml version="1.0" encoding="UTF-8"?> <users> <user> <id>1</id> <login>lo</login> <email>laurent@sillysmart.org</email> </user> <user> <id>2</id> <login>flo</login> <email>florian@sillysmart.org</email> </user> <user> <id>3</id> <login>charly</login> <email>charly@sillysmart.org</email> </user> </users>
The following css :
table.bo_table_visible {
border:1px solid #000;
border-spacing: 0px;
margin-top:10px;
margin-bottom:10px;
}
table.bo_table_visible tr.summary th {
background:#2E2D2D;
color:#E45A49;
}
table.bo_table_visible tr td {
padding:0 10px;
}
table.bo_table_visible tr.even td, table.bo_table_visible tr th {
background-color:#FFF;
color:#000;
}
table.bo_table_visible tr.even:hover td {
background-color:#FF8ABC;
color:#FFF;
}
table.bo_table_visible tr.odd td {
background-color:#DFDFDF;
color:#000;
}
table.bo_table_visible tr.odd:hover td {
background-color:#64B8FE;
color:#FFF;
}
Simply apply the XSL script below to apply styles to your list of users:
<!--
- Apply css with XSL
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="root">
<html>
<head></head>
<body>
<table class="bo_table_visible">
<tr>
<th>Id</th>
<th>Login</th>
<th>Email</th>
</tr>
<xsl:for-each select="//users/user">
<xsl:variable name="cssClass">
<xsl:choose>
<xsl:when test="(position() mod 2) = 0">
even
</xsl:when>
<xsl:otherwise>
odd
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr class="{$cssClass}">
<td><xsl:value-of select="id" /></td>
<td><xsl:value-of select="login" /></td>
<td><xsl:value-of select="email" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Will produce :
| Id | Login | |
|---|---|---|
| 1 | lo | laurent@sillysmart.org |
| 2 | flo | florian@sillysmart.org |
| 3 | charly | charly@sillysmart.org |
If you are interested in MySQL (or PHP) recover comments tables and / or comments from the columns of a table, remember that it is very easy to access this information directly from a query.
Retrieving comments for a table :
# 'sillysmart' is the name of the database you want to extract information mysql> SHOW TABLE STATUS FROM sillysmart mysql>
will produce the following informations:
| Name | Engine | Version | Rows | Comment |
|---|---|---|---|---|
| Country | MyISAM | 10 | 240 | Countries Listing |
| News | MyISAM | 10 | 30 | News Listing |
Where the column 'Comment' will give you the information, the commentary of the table. Know that many other informations are available through this application.
Retrieve the comments columns of a table :
# 'Country' is the table name you want mysql> SHOW FULL COLUMNS FROM Country mysql>
will produce the following informations:
| Field | Type | Collation | Null | Key | Extra | Privileges | Comment |
|---|---|---|---|---|---|---|---|
| country_id | bigint(20) | NULL | NO | PRI | auto_increment | select,insert,update | Id |
| country_name | varchar(255) | latin1_swedish_ci | NO | UNI | select,insert,update | Country Name | |
| country_tld | varchar(2) | latin1_swedish_ci | YES | select,insert,update | Country TLD |
Where the column 'Comment' will give you the information, the comment column of the table.
Free for you to use this information in PHP. Personally we use these columns in a transparent way on SillySmart to create generic back-office remains speaking for a non-scientist. The only constraint is that the length of the commentary is limited to 60 characters, so be succinct!
If you want to push the generic with PHP and you want to access PHP variables without knowing their names, know that it can fairly easily without using eval() function or other code unreadable.
Indeed, the flexibility of PHP allows you to "generate" variable names using the characters {}, a small example, suppose a class representing a model (here a news), deliberately simplified.
<?php
class News
{
private $news_title;
private $news_description;
public function __construct($_news_title,$news_description)
{
$this->news_title = $_news_title;
$this->news_description = $news_description;
}
public function __get($name)
{
return $this->{$name};
}
}
?>
Thanks to generic getter __get (), we can access class variables without knowing their name. Imagine, in another file a variable contains the string describing the class variable, we can recover the value from an abstract way like this :
<?php
include("News.class.php");
$news = new News("title","content");
$generic = "news_title";
var_dump($news->__get($generic));
// Display "String(7) content";
?>