Use prism for code syntax highlighting
This commit is contained in:
parent
356c9dca43
commit
adf3c67355
54 changed files with 471 additions and 324 deletions
|
@ -23,39 +23,39 @@ When I compare this to the previous gallery, I can see several differences whic
|
|||
|
||||
To do this, I'd need to query my website's database. To begin with, I wanted to have a list of all the galleries on my site which are published, and what they're unique node ID values are. To do this, I opened Sequel Pro and entered the following code:
|
||||
|
||||
~~~sql
|
||||
```language-sql
|
||||
SELECT title
|
||||
AS title, nid
|
||||
AS gallery_idFROM node
|
||||
WHERE type = 'gallery'
|
||||
AND status = 1;
|
||||
~~~
|
||||
```
|
||||
|
||||
As the nid value of each gallery corresponds with the 'field_gallery_nid' field within the content_type_photo field, I can now query the database and retrieve information about each specific gallery.
|
||||
|
||||
For example, using [aliasing](http://www.w3schools.com/sql/sql_alias.asp) within my SQL statement, I can retrieve a list of all the published photos within the 'British Squad 2008' gallery by using the following code:
|
||||
|
||||
~~~sql
|
||||
```language-sql
|
||||
SELECT n.title, n.nid, p.field_gallery_nid
|
||||
FROM node n, content_type_photo p
|
||||
WHERE p.field_gallery_nid = 105
|
||||
AND n.status = 1
|
||||
AND n.nid = p.nid;
|
||||
~~~
|
||||
```
|
||||
|
||||
I can easily change this to count the number of published nodes by changing the first line of the query to read SELECT COUNT(*).
|
||||
|
||||
~~~sql
|
||||
```language-sql
|
||||
SELECT COUNT(*)
|
||||
FROM node n, content_type_photo p
|
||||
WHERE p.field_gallery_nid = 105
|
||||
AND n.status = 1
|
||||
AND n.nid = p.nid;
|
||||
~~~
|
||||
```
|
||||
|
||||
As I've used the [Views Attach](http://drupal.org/project/views_attach) module, and I'm embedding the photos directly into the Gallery nodes, I easily add this to each gallery by creating a custom node-gallery.tpl.php file within my theme. I can then use the following PHP code to retrieve the node ID for that specific gallery:
|
||||
|
||||
~~~php
|
||||
```language-php
|
||||
<?php
|
||||
$selected_gallery = db_result(db_query("
|
||||
SELECT nid
|
||||
|
@ -64,11 +64,11 @@ WHERE type = 'gallery'
|
|||
AND title = '$title'
|
||||
"));
|
||||
?>
|
||||
~~~
|
||||
```
|
||||
|
||||
I can then use this variable as part of my next query to count the number of photos within that gallery, similar to what I did earlier.
|
||||
|
||||
~~~php
|
||||
```language-php
|
||||
<?php
|
||||
$gallery_total = db_result(db_query("
|
||||
SELECT COUNT(*)
|
||||
|
@ -76,11 +76,11 @@ FROM {content_type_photo}
|
|||
WHERE field_gallery_nid = $selected_gallery
|
||||
"));
|
||||
?>
|
||||
~~~
|
||||
```
|
||||
|
||||
Next, I wanted to display the date that the last photo was displayed within each album. This was done by using a similar query that also sorted the results in a descending order, and limited it to one result - effectively only returning the created date for the newest photo.
|
||||
|
||||
~~~php
|
||||
```language-php
|
||||
<?php
|
||||
$latest_photo = db_result(db_query("
|
||||
SELECT n.created
|
||||
|
@ -90,11 +90,11 @@ AND n.nid = p.nid
|
|||
ORDER BY n.created DESC LIMIT 1
|
||||
"));
|
||||
?>
|
||||
~~~
|
||||
```
|
||||
|
||||
This was all then added into a 'print' statement which displayed it into the page.
|
||||
|
||||
~~~php
|
||||
```language-php
|
||||
<?php
|
||||
if ($selected_gallery_total != 0) {
|
||||
$output = '<i>There are currently ' . $selected_gallery_total . ' photos in this gallery.';
|
||||
|
@ -102,13 +102,13 @@ if ($selected_gallery_total != 0) {
|
|||
print $output;
|
||||
}
|
||||
?>
|
||||
~~~
|
||||
```
|
||||
|
||||
OK, so let's take a look at the Gallery so far:
|
||||
|
||||
You will notice that the returned date value for the latest photo added is displaying the UNIX timestamp instead of in a more readable format. This can be changed by altering the 'print' statement to include a PHP 'date' function:
|
||||
|
||||
~~~php
|
||||
```language-php
|
||||
<?php
|
||||
if ($selected_gallery_total != 0) {
|
||||
$output = '<i>There are currently ' . $selected_gallery_total . ' photos in this gallery.';
|
||||
|
@ -116,7 +116,7 @@ if ($selected_gallery_total != 0) {
|
|||
print $output;
|
||||
}
|
||||
?>
|
||||
~~~
|
||||
```
|
||||
|
||||
The values that I've entered are from [this page](http://php.net/manual/en/function.date.php) on PHP.net, and can be changed according on how you want the date to be displayed.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue