1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

DB Queries on a Page

Discussion in 'XenForo Questions and Support' started by The Dark Wizard, May 5, 2012.

  1. The Dark Wizard

    The Dark Wizard Well-Known Member

    I want a Xenforo page node to be able to query the database and show results of that query.

    How do I go about this?
     
  2. Floris

    Floris Guest

    You will have to create a php callback and a method.
    create your own add-on, create a class (which will be your callback) and a public function (your method).
    You can have it then connect and query the database, and return $something;
    You can then use that something in your page.
     
  3. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  4. The Dark Wizard

    The Dark Wizard Well-Known Member

    http://www.waywardinn.com/forum/#the-stage.5413
    Is what we are trying to get working. It grabs the descriptions of forums under the category above. http://www.waywardinn.com/pages/testwizard/

    As you can see, on the page the lists are not showing their banners, but it works on the actual script, http://www.waywardinn.com/testlist.php

    Here is what we have for PHP Callback: Dev_PageCallback_TemplateDemo method: respond

    The template has the following.

    <xen:title>{$page.title}</xen:title>

    <xen:navigation>
    <xen:breadcrumb source="$nodeBreadCrumb" />
    </xen:navigation>

    <xen:require css="public.css" />

    <div class="sectionMain">
    <h2 class="sectionMain">Wayward Inn Roleplays</h2>
    <ol>
    <xen:foreach loop="$rps" value="$rp">
    <li class="secondaryContent userThing">
    <div class="userTitle">{$rp.title}</div>
    <div class="userTitle">{$rp.description}</div>
    </li>
    </xen:foreach>
    </ol>
    </div>


    Is there any reason as to why the page would not show the banners?
     
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Can you post the PHP code for your callback (Dev_PageCallback_TemplateDemo::respond) and testlist.php file?
     
  6. The Dark Wizard

    The Dark Wizard Well-Known Member


    Callback TemplateDemo:

    Code:
    <?php
    include("./dbmysql.php");
    class Dev_PageCallback_TemplateDemo
    {
        public static function respond(Xenforo_ControllerPublic_Abstract $controller, Xenforo_ControllerResponse_Abstract $response)
        {
    $db = new dbMysql('localhost', 'Database Name', 'User', 'password', 'false');
    $result = $db->query('SELECT b.title, b.description FROM xf_forum AS a INNER JOIN xf_node AS b ON(a.node_id = b.node_id) WHERE b.parent_node_id = 5413 ORDER BY b.title ASC');
    $response->params['rps'] = $result;
    $response->templateName = 'template_demo';
        }
    }
    ?>
    
    Testlist.php

    Code:
    <?php
     
    include("./dbmysql.php");
    $db = new dbMysql('localhost', 'Database Name', 'User', 'Password', 'false');
    $result = $db->query('SELECT b.title, b.description FROM xf_forum AS a INNER JOIN xf_node AS b ON(a.node_id = b.node_id) WHERE b.parent_node_id = 5413 ORDER BY b.title ASC');
     
    foreach($result as $a)
    {
    echo $a['title'] . '<br />' . $a['description'] . '<br /><br />';
    }
     
    ?>
    
     
  7. The Dark Wizard

    The Dark Wizard Well-Known Member

    Hmm I keep poking at it and looking over it but for the life of me I can't see what is wrong.
     
  8. The Dark Wizard

    The Dark Wizard Well-Known Member

    Naatan helped me! I just had to use {xen:raw $var}
     
  9. Naatan

    Naatan Well-Known Member

    And use &$response instead of $response.
     
    Floris likes this.
  10. The Dark Wizard

    The Dark Wizard Well-Known Member

    ok :D
     

Share This Page