使用SQL和PHP返回MySQL的排名结果

时间:2015-01-13 00:46:05

标签: php sql drupal

我试图使用PHP(在drupal站点中)为SQL查询提供正确的语法,但我需要一些指导。

这是我的想法(在Drupal模块中 - 已经设计过)。用户从多页表单中进行选择(每页3页,单选按钮,每页只允许1个选项)。我希望将这些提交到数据库并让MySQL数据库根据用户的选择返回推荐的用户,按照他们选择的数量与数据库资源中的条件匹配的顺序排列。例如,用户可以为exp级别选择 novice ,为角色选择创始人,并选择 corporateuralship (组成单词)作为他们感兴趣的主题(表格中的3个问题)。

如果数据库中的资源为这些字段设置了所有这3个字段(作为字段列表的一部分),我们希望它返回到结果顶部附近(如果超过1,那么我们所有3个& #39; d希望按字母顺序排序 - 升序),然后是任何具有至少2个匹配列表术语的资源(在这些第二层资源之间具有相应的字母排名),然后是仅匹配一个已检查项目的项目。返回0匹配的项目根本不会显示。

我所做的是将基于Drupal和PHP代码的概念放在一起,我希望能够就此提供反馈。下面的代码是用户提交表单后调用的函数,并被重定向到/recommendations页面。我主要想要反馈的是将PHP变量设置在顶部,我的$result var中的SQL是我最好的猜测它是如何工作的,然后是一堆if/while语句进行排序并将数据返回到屏幕。

任何帮助制作这些功能更强大的代码都将非常感激。谢谢!

function pathfindertool_recommendations_page() {
$output = '';

$level = $_SESSION['level'];
$level = mysql_real_escape_string($level);
$role = $_SESSION['role'];
$role = $level = mysql_real_escape_string($role);
$topic = $_SESSION['topic'];
$topic = mysql_real_escape_string($topic);

$result = db_query("SELECT n.nid, n.title, n.type, fdb.body_value, fdfe.field_experience_level_value, fdfi.field_icon, fdfl.field_link_url, fdfr.field_role_value, fdft.field_topics_value FROM {node} as n INNER JOIN {field_data_body} as fdb INNER JOIN {field_data_field_experience} as fdfe INNER JOIN {field_data_field_icon} as fdfi INNER JOIN {field_data_field_link} as fdfl INNER JOIN {field_data_field_role} as fdfr INNER JOIN {field_data_field_topics} as fdft on n.nid = fdb.entity_id on n.nid = fdfe on n.nid = fdfi on n.nid= fdfl on n.nid = fdfr on n.nid = fdft WHERE n.type = :type", array(':type' => 'resource',) "AND fdfe.field_experience_level_value = :level", array(':level' => '$level',) "AND fdfr.field_role_value = :role", array(':role' => '$role',) AND fdft.field_topics_value = :topic", array(':topic' => '$topic',) ORDER BY ASC);

if($result) {
    while ($row = $result->fetchAssoc()) {
    // If all three match
        while (($row['field_data_experience_level'] = $level) && ($row['field_data_field_role'] = '$role') && ($row['field_data_field_topics'] = $topic)) {
            $title = row['title'];
            $link = row['field_data_field_link'];
            $body = row['body_value'];
            $icon = row['field_icon'];
            $output .= '<p> . $title . <br> . $icon . <br> . $body . <br> . $link';
            return $output;
        }       
        // If at least two match
        while (($row['field_data_experience_level'] = $level) && ($row['field_data_field_role'] = '$role') OR ($row['field_data_field_topics'] = $topic)) {
            $title = row['title'];
            $link = row['field_data_field_link'];
            $body = row['body_value'];
            $icon = row['field_icon'];
            $output .= '<p> . $title . <br> . $icon . <br> . $body . <br> . $link';
            return $output;
        }
        while (($row['field_data_experience_level'] = $level) OR ($row['field_data_field_role'] = '$role') && ($row['field_data_field_topics'] = $topic)) {
            $title = row['title'];
            $link = row['field_data_field_link'];
            $body = row['body_value'];
            $icon = row['field_icon'];
            $output .= '<p> . $title . <br> . $icon . <br> . $body . <br> . $link';
            return $output;
        }
        // Just if only one matches
        while (($row['field_data_experience_level'] = $level) OR ($row['field_data_field_role'] = '$role') OR ($row['field_data_field_topics'] = $topic)) {
            $title = row['title'];
            $link = row['field_data_field_link'];
            $body = row['body_value'];
            $icon = row['field_icon'];
            $output .= '<p> . $title . <br> . $icon . <br> . $body . <br> . $link';
            return $output;
        }
    }
}

这是我的SQL:

$result = db_query("
SELECT n.nid,
       n.title,
       n.type,
       fdb.body_value,
       fdfe.field_experience_level_value,
       fdfi.field_icon,
       fdfl.field_link_url,
       fdfr.field_role_value,
       fdft.field_topics_value
FROM {node} AS n
INNER JOIN {field_data_body} AS fdb
INNER JOIN {field_data_field_experience} AS fdfe
INNER JOIN {field_data_field_icon} AS fdfi
INNER JOIN {field_data_field_link} AS fdfl
INNER JOIN {field_data_field_role} AS fdfr
INNER JOIN {field_data_field_topics} AS fdft ON n.nid = fdb.entity_id ON n.nid = fdfe ON n.nid = fdfi ON n.nid= fdfl ON n.nid = fdfr ON n.nid = fdft
WHERE n.type = :type
",
array(':type' => 'resource',) "AND fdfe.field_experience_level_value = :level", array(':level' => '$level',) "AND fdfr.field_role_value = :role", array(':role' => '$role',) AND fdft.field_topics_value = :topic", array(':topic' => '$topic',) ORDER BY ASC);

0 个答案:

没有答案