XF 2.0 Regarding custom coding

Hello, I have a query regarding creating a new page. I want to create a custom page and what to use the database query to fetch the data from database
 
I have done this via PHP Callback method. Might not be the best way to do it, but works for me. :)

Category details, the weblinks and recommended books are all from separate tables in the database.

PHP Callback : Site\Pages\CatFiftyTwo :: getData
In my SRC\Addons\Site\Pages i upload file CatFiftyTwo.php (You need this file uploaded before you create the page or you get an error)

Template HTML
PHP:
<div class="block">
        <div class="block-container">
            <div class="block-body block-row">

                <div class="bbWrapper"><div class="bbTable">
            <table border="1" cellpadding="2" cellspacing="2">
<xf:foreach loop="$catinfo" value="$catinfo">
<tr>
<td colspan="4">
    <div align="center">
    <h1>{$catinfo.cat_name} - {$catinfo.cat_description}</h1>
    </div>
</td>
</tr>
<tr>
<td colspan="4">
<strong>HISTORY:</strong><br />
Formed for service in Northern Ireland<br />
absorbed into new regiment 6.4.2005<br />
Special Reconnaissance Regiment<br />
HQ Hereford<br />
formed to act as intelligence for Special Forces<br /><br />

I have also included<br />
Force Research Unit (FRU),Intelligence Corps<br />
formed 1980/81 to handle agents in Northern Ireland</td>
</tr>
<tr>
    <td width="25%"><strong>Roll Of Honour: <a href="index.php?forums/roll-of-honour.4/&prefix_id=1&order=title&direction=asc">{$catinfo.cat_stats_roll}</a></strong></td><td width="25%"><strong>Awards: <a href="index.php?forums/awards.5/&prefix_id=1&order=title&direction=asc">{$catinfo.cat_stats_awards}</a></strong></td><td width="25%"><strong>Images: <a href="index.php?media/categories/14th-intell.52/&order=title&direction=asc">{$catinfo.cat_stats_images}</a></strong></td><td width="25%"><strong>Links: {$catinfo.cat_stats_links}</strong></td>
</tr>
</xf:foreach>
                    </table></div></div>


<strong>Web Links:</strong><br />                
    <xf:if is="$links">
        <xf:foreach loop="$links" value="$links">
        <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>
<xf:else />
There are no links<br />
</xf:if>                
                              
<strong>Recommended:</strong><br />               
<table border="0">
    <xf:if is="$amazon">
    <xf:foreach loop="$amazon" value="$amazon">
    <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 recommended items added to this category.</td>
        </tr>
</xf:if>
</table>
                
<xf:widget key ="images1014thintell"/>
</div></div></div>

<xf:widget key ="threads1014thintell" position="sidebar"/>

CatFiftyTwo.php
PHP:
<?php

namespace Site\Pages;

/**
 * Static methods for the Providers page.
 */

class CatFiftyTwo
 {
   /**
     * @param \XF\Pub\Controller\AbstractController $controller
     * @param \XF\Mvc\Reply\AbstractReply           $reply
     */
    public static function getData(
      \XF\Pub\Controller\AbstractController $controller,
       \XF\Mvc\Reply\AbstractReply &$reply
    ) {
        if ($reply instanceof \XF\Mvc\Reply\View) {

////
////LINKS
////
            $links = \XF::db()->fetchAll(
                "SELECT
                    link_id, link_catid, link_name, link_description, link_added, link_url, link_hits    
                    FROM craig_links WHERE link_catid = 52
                    
                    ORDER BY link_name ASC
                    "
            );

            $reply->setParam('links', $links);
////
////PRODUCTS
////
            $amazon = \XF::db()->fetchAll(
                "SELECT
                    amazon_id, amazon_rohcat, amazon_productname, amazon_author, amazon_asin, amazon_description, amazon_clicks, amazon_filename    
                    FROM craig_amazon WHERE amazon_rohcat = 52
                    ORDER BY amazon_productname ASC
                    "
            );

            $reply->setParam('amazon', $amazon);

////
////CATEGORIES
////
        $catinfo = \XF::db()->fetchAll(
                "SELECT
                    cat_id, cat_name, cat_xenname, cat_description, cat_history, cat_stats_roll, cat_stats_awards, cat_stats_links, cat_stats_images
                    FROM craig_categories WHERE cat_id = 52
                    ORDER BY cat_name ASC
                    "
            );


            $reply->setParam('catinfo', $catinfo);


        }
   }    
} 

?>

Hope this is of use. :)
 
Hello,
looks great. But can you please tell me the path , where I have to put this code? I want to create a new page which should only be visible after login and will have some custom coding of fetching the data from the database, so basically what all steps should I need to follow?
On which path do I need to create a new file, it will be great If you can explain a bit more in details.
 
You would upload your PHP file to SRC\Addons\Site\Pages\CatFiftyTwo.php You can have it in any addon directory, but then you have to get path below right.
In the php file i name my class the same as my filename to make it easy.
PHP:
class CatFiftyTwo
Then when creating your new page under PHP Callback you put Site\Pages\CatFiftyTwo :: getData
Your custom page you can probably make it visible to logged in members using conditional statements.

Easiest way to get it to work is to play around with it. I mainly followed
 
Hello,
I have followed the same steps as you mentioned but having an error:

1593151101913.webp

This is the error that occurred.
I have created the node like this:
1593151184718.webp

1593151202358.webp

Added the code on src\addons\Site\Pages\CatFiftyTwo.php file path.
Please let us know and help where we have made the mistake.
 
The PHP file, did you edit it to point to your own database tables?
I am pointing them to craig_amazon craig_categories and craig_links in the PHP file you uploaded.
 
Hello,
Thanks for your help. I am able to remove my error through my code and changed the query to as per my database but still the data is not being shown there.
1593233946022.webp

As per above screenshot I have changed the query to xf_user which is there in the database.
I also made the changes in the node html section

1593234187941.webp

but still at front page username is not coming. See below screenshot

1593234226079.webp

Can you please help me to find out what mistake I am being doing here?
 
Replace all your template HTML code with this code to see if it works:
PHP:
        <xf:foreach loop="$catinfo" value="$catinfo">
Username : {$catinfo.username} {$catinfo.email}<br />
        </xf:foreach>
 
Hello,
I have made the changes as per your last message but still facing the same issues.
1593406559876.webp

Above is database table which I am trying to link.

1593406623419.webp

Above screenshot is code at my page CatFiftyTwo.php at path "src\addons\Site\Pages\CatFiftyTwo.php".

1593406704550.webp
Above screenshot is of my node from Admin panel.

but still facing same issue:

1593406747879.webp

Not able to get the data.
 
I see the problem now. :) You need to move the code from the "description" part of the node to the "template" part of the node.screeny.png
 
Hello,
I moved the code from description to template but still it doesn't work.
1593510055391.webp
Above is the screenshot of the Node in my admin.

1593510082579.webp

Above screenshot is my page which is blank now. Can you please let me know what mistake I am doing here?
 
It is very strange as seems to be same as what i have, try normal text in the template html box to see if anything appears.
PHP:
TEST TEXT 
       <xf:foreach loop="$catinfo" value="$catinfo">
Username : {$catinfo.username} {$catinfo.email}<br />
        </xf:foreach>
TEST TEXT

I also have under navigation section "Default (forums)" selected, don't know if that would make a difference though..
 
Just tried exact thing on my site and it has worked fine.. We are on 2.1, but worked previously on 2.0

usertest.php
1593521232492.png

File usertest.php uploaded into SRC\Addons\Site\Pages folder.
1593521147961.png


xen1.png
The output:
1593521333573.png
 
@charlesfoote13 have you filled in the PHP Callback fields in your page node, as shown in @Craigr screen shot?

edit: also you should be using a finder instead of directly accessing the DB.

PHP:
$finder = \XF::finder('XF:User')->where('user_id', '<', 100);
$catinfo = $finder->fetch();
 
Last edited:
Hello,
Can you please let me know how did you manage to get the result at front? I mean, how did you get the menu to show at the menu bar at front? Can you please share the screenshot of the public navigation page as well, so that I'll get to know If I am making any mistake there.

I have also create a file with usertest.php on same path.
1593525229830.webp

I also made the changes in previous code as per Lawrence reply, but no luck. I think I am doing some mistake with the public navigation to get the menu.

Can you please share the screenshot of public navigation where you managed to show this as your page menu?
1593525438591.webp

This is what I have done in the public navigation menu. So at front my menu is visible as Front page.
1593525499711.webp

Am I doing something wrong here? Please suggest
 
Hello,
Can you please let me know how did you manage to get the result at front? I mean, how did you get the menu to show at the menu bar at front? Can you please share the screenshot of the public navigation page as well, so that I'll get to know If I am making any mistake there.

I have also create a file with usertest.php on same path.
View attachment 229518

I also made the changes in previous code as per Lawrence reply, but no luck. I think I am doing some mistake with the public navigation to get the menu.

Can you please share the screenshot of public navigation where you managed to show this as your page menu?
View attachment 229519

This is what I have done in the public navigation menu. So at front my menu is visible as Front page.
View attachment 229520

Am I doing something wrong here? Please suggest
Can you be a little more specific on what you want to achieve? I see from your screen shot you have "Front Pages" set in your nav bar, what else did you want, to set it under the forums tab?

Just to let you know, I followed the example @Craigr posted above, to test it before replying here, and it works fine, and then I set the Page as a landing page (home page) as I thought that is what you wanted to achieve(it appears I may have been wrong), but here is the screen shot:
 

Attachments

  • page-test.webp
    page-test.webp
    21.2 KB · Views: 14
Hello,
I just want to create a new page from Admin , that new page should be visible in front end as a new menu and clicking on it I want to show the users and email from the Admin.
I am not sure where I am doing the mistake.
For this I followed following steps:
1) Created a new file at path "src\addons\Site\Pages\CatFiftyTwo.php" with following code:
<?php

namespace Site\Pages;

/**
* Static methods for the Providers page.
*/

class CatFiftyTwo
{
/**
* @Param \XF\Pub\Controller\AbstractController $controller
* @Param \XF\Mvc\Reply\AbstractReply $reply
*/
public static function getData(
\XF\Pub\Controller\AbstractController $controller,
\XF\Mvc\Reply\AbstractReply &$reply
) {
if ($reply instanceof \XF\Mvc\Reply\View) {

$catinfo = \XF::db()->fetchAll("SELECT * FROM xf_user");


$reply->setParam('catinfo', $catinfo);

}
}
}

?>


2) Now, I have created a new node from Admin
1593602122241.webp

1593602144128.webp

3) Than I move to public navigation in the Admin and created a new page with Visitor page name and linked it with the Front Page Node.

1593602663999.webp

4) After doing all these steps I opened the front end website
1593602707985.webp

there is a menu named Front Pages at the top but nothing data is displayed here.
My Aim is just to create a new page from Admin and showing the data at front from the database.

Please let me know if I have misunderstood something or made any mistake here
 
Hello,
I just want to create a new page from Admin , that new page should be visible in front end as a new menu and clicking on it I want to show the users and email from the Admin.
I am not sure where I am doing the mistake.
For this I followed following steps:
1) Created a new file at path "src\addons\Site\Pages\CatFiftyTwo.php" with following code:
<?php

namespace Site\Pages;

/**
* Static methods for the Providers page.
*/

class CatFiftyTwo
{
/**
* @Param \XF\Pub\Controller\AbstractController $controller
* @Param \XF\Mvc\Reply\AbstractReply $reply
*/
public static function getData(
\XF\Pub\Controller\AbstractController $controller,
\XF\Mvc\Reply\AbstractReply &$reply
) {
if ($reply instanceof \XF\Mvc\Reply\View) {

$catinfo = \XF::db()->fetchAll("SELECT * FROM xf_user");


$reply->setParam('catinfo', $catinfo);

}
}
}

?>


2) Now, I have created a new node from Admin
View attachment 229619

View attachment 229620

3) Than I move to public navigation in the Admin and created a new page with Visitor page name and linked it with the Front Page Node.

View attachment 229622

4) After doing all these steps I opened the front end website
View attachment 229623

there is a menu named Front Pages at the top but nothing data is displayed here.
My Aim is just to create a new page from Admin and showing the data at front from the database.

Please let me know if I have misunderstood something or made any mistake here
Here is the code I used for my testing:

PHP:
<?php

namespace Site\Pages;

class CatFiftyTwo
{
    public static function getData(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)
    {
        if ($reply instanceof \XF\Mvc\Reply\View)
        {
            $finder = \XF::finder('XF:User')->where('user_id', '<', 100);
            $finder->order('username', 'ASC');
       
            $reply->setParam('catinfo', $finder->fetch());
        }
    }
}

Attached is my settings for the page node and navigation, and as well the screenshot of the resulting output:

navigation.pngnode-settings.pngthe-output.png

@Chris D I inserted the attachments as thumbnails, these are pretty large thumbnails...
 
Hello,
Thanks for your help, the code is working now. But I am having one other issue,
I put the query with user table and got the results, but when I tryed to run the query from my table which I created into the database it gives an error.

My database table name is : xf_schedule_match
The code that I have applied is:




namespace Site\Pages;



class CatFiftyTwo

{

public static function getData(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)

{

if ($reply instanceof \XF\Mvc\Reply\View)

{

$finder = \XF::finder('XF:schedule_match');
$reply->setParam('catinfo', $finder->fetch());

}

}

}


It gives me an error:
1593865475374.webp

I have used only html code in the template, but as soon as I put this code i found the error. Is there a way I can print the query here, so that I get to know the error, or can you help me to find out the error?
 
Top Bottom