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

mysql rows php callback

Discussion in 'XenForo Development Discussions' started by rmb938, Jun 21, 2011.

  1. rmb938

    rmb938 Member

    I am trying to get mysql data to show in a xenForo page.

    This is my original php file that I want to show on the page:

    Code:
    <?php
    
    // change these things
    
      $server = "localhost";
      $dbuser = "xxxxxxx";
      $dbpass = "xxxxxxxx";
      $dbname = "xxxxxxxx";
    
    mysql_connect($server, $dbuser, $dbpass);
    mysql_select_db($dbname);
    
    $result = mysql_query("SELECT * FROM banlist ORDER BY time DESC");
    
    echo "<table width=70% border=1 cellpadding=5 cellspacing=0>";
    
    echo "<tr style=\"font-weight:bold\">
    <td>Name</td>
    <td>Reason</td>
    <td>Admin/Mod</td>
    <td>Time of ban</td>
    <td>Time of unban</td>
    </tr>";
    
    while($row = mysql_fetch_assoc($result)){
    
    if($col == "#eeeeee"){
    $col = "#ffffff";
    }else{
    $col = "#eeeeee";
    }
    echo "<tr bgcolor=$col>";
    
    echo "<td>".$row['name']."</td>";
    echo "<td>".$row['reason']."</td>";
    echo "<td>".$row['admin']."</td>";
    echo "<td>".$row['time']."</td>";
    if($row['temptime'] == "0000-00-00 00:00:00"){
    echo "<td>Permanent</td>";
    }else{
    echo "<td>".$row['temptime']."</td>";
    }
    
    echo "</tr>";
    }
    
    echo"</table>"
    
    ?>
    
    So I made a new page in xenForo and made a callback php

    Code:
    <?php
    
    class CallBack_Bans
    {
        public static function respond(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract $response)
        {
            $server = "localhost";
            $dbuser = "xxxxx";
            $dbpass = "xxxxxx";
            $dbname = "xxxxx";
    
            mysql_connect($server, $dbuser, $dbpass);
            mysql_select_db($dbname);
    
            $result = mysql_query("SELECT * FROM banlist ORDER BY time DESC");
            while($row = mysql_fetch_assoc($result)){
    
    //send row data to template
            }
            $response->templateName = 'banlist';
        }
    
    }
    and a banlist template

    Code:
    <xen:title>{$page.title}</xen:title>
    
    <div class="sectionMain">
    <h2 class = "subHeading">Ban List</h2>
    <table width=70% border=1 cellpadding=5 cellspacing=0>
    
    <tr style=\"font-weight:bold\">
    <td>Name</td>
    <td>Reason</td>
    <td>Admin/Mod</td>
    <td>Time of ban</td>
    <td>Time of unban</td>
    </tr>
    
    </table>
    </div>
    I am not to sure how to get the row data into the template from the while loop.
     
  2. Korenwolf

    Korenwolf New Member

    You can return the $result var and put the while in the banlist template
     
  3. SheepCow

    SheepCow Well-Known Member

    Is the MySQL server you're talking to different to the one the forum is running on? If it's not you can just use the XenForo DB object instead of connecting again. Also, you could use the Zend Framework Database objects instead of the mysql_* functions.

    You won't be able to do that as $result is a resource in the above example, you'd have to fetch all the rows in to an array then pass the array to the template. Then you can use a for loop in the template (search templates for "xen:for" to get some examples) to display the rows.
     
  4. Korenwolf

    Korenwolf New Member

    SheepCow the $result is the mysql fetched array so why not just us it and mysql_fetch_array it ? Do you have to put it in a new array first or is it just to make it cleaner?
     
  5. SheepCow

    SheepCow Well-Known Member

    In the code above, $result is the query result resource, it's passed to mysql_fetch_assoc() and $row is a single row as an array of data.

    e.g.
    PHP:
    $allRows = array();

    $result mysql_query("SELECT * FROM banlist ORDER BY time DESC");
    while(
    $row mysql_fetch_assoc($result))
    {
        
    $allRows[] = $row;
    }
     
  6. Korenwolf

    Korenwolf New Member

    Allrighty then, i seem to have misread your comment. Thanks for your explanation!
     

Share This Page