XF 2.1 Pages - PHP Callback

Craigr

Active member
I currently have 81 pages on my site created. I was going to use a template for each page so i can easily change the content if i make minor changes.

On each page i have managed to pull web links from a separate SQL table (i imported from previous forum software install).
I am wondering how to show if there is an empty result. This is my code for now:
Code:
<strong>Web Links:</strong><br />
    <xf:foreach loop="$links" value="$links" if="$links.link_catid == 46">
        <xf:if is="$xf.visitor.is_admin"><span style="float:right">[ <em>{$links.link_hits} Hits</em> ]</span></xf:if><strong>&bull; <a href="{$links.link_url}" target="_blank">{$links.link_name}</a></strong> - {$links.link_description}<br />
</xf:foreach>

Works fine when there are 1 or more results, but when i have no results from the database i get Web Links and no text. Ideally i would like to have some text to say no links added. Was wondering if i could do it via conditional statements, but had no success?

Thanks, Craig
 

Kier

XenForo developer
Staff member
Try
PHP:
<xf:if is=“$links”>
    <xf:foreach loop=“$links” ...>
    ...
    </xf:foreach>
<xf:else />
    <p>There are no links</p>
</xf:if>
Or something similar with xf:if ... xf:else
 

Craigr

Active member
Thanks Kier. :)

The first line:
PHP:
<xf:if is=“$links”>
Give this error code though Line 48: Syntax error - Template name: public:_page_node.13 when i try to save.
 

NixFifty

XenForo developer
Staff member
Use this:
PHP:
<xf:if is="$links">
    <xf:foreach loop="$links" ...>
    ...
    </xf:foreach>
<xf:else />
    <p>There are no links</p>
</xf:if>

Notice the slight difference in quotation marks.
 

Craigr

Active member
Thanks folks. :)

This is what i have now, if no results it still isn't displaying the 2nd part. :( If results are found the items are shown.

PHP:
<strong>Recommended:</strong>                
<table border="0">
<xf:if is="$amazon">
    <xf:foreach loop="$amazon" value="$amazon" if="$amazon.amazon_rohcat == 46">
    <tr>
    <td width="70"><img src="store/tb_{$amazon.amazon_filename}" alt="{$amazon.amazon_productname}"></td>
    <td><strong><a href="https://www.amazon.co.uk/gp/product/{$amazon.amazon_asin}/ref=as_li_ss_tl?ie=UTF8&tag=specialforces0a-21&linkCode=as2&camp=1634&creative=19450&creativeASIN={$amazon.amazon_asin}">{$amazon.amazon_productname}</a></strong><br />By {$amazon.amazon_author}<xf:if is="$xf.visitor.is_admin"><br /><em>{$amazon.amazon_clicks} Clicks</em></xf:if></td>
    </tr>
    </xf:foreach>
<xf:else />
    <tr>
    <td colspan="2">No products.</td>
    </tr>
</xf:if>    
</table>

Thanks, Craig
 

Brogan

XenForo moderator
Staff member
Dump $amazon in the page to see what is being returned when you believe there are no products.

It's possible there is still a value.
 

Craigr

Active member
Put
PHP:
$amazon.amazon_filename
outside the table and it shows up file1782060804.jpg which it must have got from the database.
Is it maybe due to this line as i think it pulls all the database entries then i filter to just show category number 46.
PHP:
<xf:foreach loop="$amazon" value="$amazon" if="$amazon.amazon_rohcat == 46">

I did it that way so i didn't have to create a php file for each category.. Maybe better if i create a separate php file for each category?
 

Brogan

XenForo moderator
Staff member
You could just do a check for $amazon.amazon_rohcat == 46 for the if/else statement and remove the check from inside it.
 
Top