Reading Field From Table

MichaelB

Member
I am not a php/mysql coder but I am trying to learn the basics for what I need. I want to read the value from a certain field in a table and then display it on the user info panel on the sidebar. The table is "xf_user" and the field is "reviews_count". Since this is user specific, it will need to display the value for the logged in user.

Below is what I have but I'm not sure how to display it on the sidebar area or if it is even correct at all. Of course, I would create the ReadCount.php file and place it in library/Reviews/Model/ folder.

Code:
<?php
    class Reviews_Model_ReadCount extends XenForo_Model
    {
        public function getReviewsCount($reviews_count)
        {
            return $this->_getDb()->fetchRow('
                SELECT * FROM xf_user WHERE reviews_count = ?', $reviews_count);
        }
    }
?>


I read on another page that the following code would be used to get a value but do I replace 'user_id' with 'reviews_count' and 'xf_admin' with 'xf_user'? Once the code is correct in grabbing the value, how do I display it on the page? Assuming a callback function is needed?

Code:
$db->fetchCol("SELECT `user_id` FROM `xf_admin`");
Code:
<xen:callback class="Reviews_Model_ReadCount" method="getReviewsCount"></xen:callback>
 
Are you trying to fetch it for a user? You'll want to use user_id in the WHERE clause, callback doesn't have the parameter you are using, but since it's for the visitor you will want to use XenForo_Visitor to fetch the ID.
 
It will be to fetch the value of the user currently logged in (same as displaying that user's number of messages/likes/points).

Is this correct then? Replacing * with user_id and changing fetchRow with fetchOne?

Code:
<?php
    class Reviews_Model_ReadCount extends XenForo_Model
    {
        public function getReviewsCount($reviews_count)
        {
            return $this->_getDb()->fetchOne('SELECT user_id FROM xf_user WHERE reviews_count = ?', $reviews_count);
        }
    }
?>
 
No, you need to update the WHERE clause, not the SELECT clause. Are you sure that the visitor_panel doesn't already include the data you need? The $visitor object pulls a full record, IIRC. Try adding {$visitor.reviews_count} to the template.
 
Many thanks, Jeremy! I previously tried using $user.reviews_count since I saw it in the message_user_info template and it was blank on the visitor panel. $visitor.reviews_count worked
 
I think it's worth noting that I don't think Callback tags are designed to extend Models.

The only reason I can see that you might want to be extending the Model is to use $this->_getDb()

There are numerous ways of accessing the database object. It is always available by default to models and DataWriters. But in actual fact you can call the database object from anywhere.

Easiest way is:

$db = XenForo_Application::getDb();

That would then allow you to use the code like this:

PHP:
<?php
    class Reviews_Model_ReadCount
    {
        public function getReviewsCount($reviews_count)
        {
            $db = XenForo_Application::getDb();
            return $db->fetchOne('SELECT user_id FROM xf_user WHERE reviews_count = ?', $reviews_count);
        }
    }
?>
I have't fully read what you're trying to do, though. Just thought I'd stick my ore in and highlight a better way to access the data and functions you need.
 
Top Bottom