将节点id与mysql表id匹配 [英] Matching node id with mysql table id

查看:121
本文介绍了将节点id与mysql表id匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做一些功能,我可以从url( example:localhost / site / node-1 )获取节点ID,并链接到mysql数据库行,其中(示例:id = 1),我需要动态地与几个节点做..我尝试这个代码:

 <?php 

function module_name_menu(){
$ items ['node-%'] = array(
'title' =>'数据库行',
'page callback'=>'module_name_node_page_view',
'access callback'=>'node_access',
);
return $ items;
}

函数module_name_node_page_view(){

$ id = $ _GET ['id'];
$ results = db_query(SELECT * FROM csvupload WHERE id = $ id);

$ header = array(t('First name'),t('Last name'),t('Email'));
$ rows = array();

foreach($ results as $ result){
$ rows [] = array(
$ result - > first_name,
$ result - > last_name,
$ result - > email,

);
}

返回主题('table',array('header'=> $ header,'rows'=> $ rows));

}

当我去 localhost / site / node-1 我找不到页面。我需要使用 node-2,node-3 ...有人可以帮我解决这个问题吗?

解决方案

有几种方法来获取当前页面节点ID,例如:

  if (arg(0)=='node'){
$ nid = arg(1);
}

  if($ node = menu_get_object()){
$ nid = $ node-> nid;
}

然后,您的格式:



localhost / site / node-1



不是drupal的url格式,而是为您定制的。为此,您可以先使用:

  $ _ SERVER ['REQUEST_URI'] 
/ pre>

获取您的自定义路径。然后,由于您的路径只包含一个 - 标志,您可以使用php explode()函数来拆分该符号,并从减号中找到字符串部分。



http://php.net/manual/en/function.explode.php



此外,使用视图是从数据库获取内容的更自然(drupalish)方式,但如果您的作业是具体的...您必须做就这样但是如果你必须加载一个节点,那么为什么要选择csvupload表呢? Drupal的数据库组织比您预期的复杂一些。不是您添加到内容类型的所有字段都是一个表的一部分。更多的是,您添加的每个字段都是不同的表,您需要大量的联接来获取所需的内容。这就是为什么最好使用视图...


I have to make function where I can get node id from url (example: localhost/site/node-1)and make link to mysql databes row where is (example: id=1) and i need to do it dynamically with several nodes.. I try this code:

<?php

function module_name_menu() {
  $items['node-%'] = array(
    'title' => 'Row from database',
    'page callback' => 'module_name_node_page_view',
    'access callback' => 'node_access',
  );
  return $items;
}

function module_name_node_page_view(){

        $id = $_GET['id'];
        $results=db_query("SELECT * FROM csvupload WHERE id = $id");

        $header = array ( t('First name'), t('Last name'), t('Email'));
        $rows = array();

  foreach($results as $result){
    $rows[] = array(
      $result -> first_name,
      $result -> last_name,
      $result -> email,

      );
  }

  return theme('table', array('header' => $header, 'rows' => $rows));

}

When I go to localhost/site/node-1 I get Page not found. I need to do it with node-2, node-3... Can someone help me with this problem?

解决方案

There are several ways to get current page node id, i.e.:

if (arg(0) == 'node') {
  $nid = arg(1);
}

Or

if ($node = menu_get_object()) {
  $nid = $node->nid;
}

Then, your format:

localhost/site/node-1

is not drupal's url format, but something custom for you. For that you can first use:

$_SERVER['REQUEST_URI']

to get your custom path. Then, since your path contains only one "-" sign you can use php explode() function to split by that sign and find string part right from the minus sign.

http://php.net/manual/en/function.explode.php

Also, using views is "more natural" ("drupalish") way for getting content from database, but if your assignment is specific...you have to do it that way. But if you have to load a node, then why are you selecting "csvupload" table instead? Drupal's database organization is a bit more complex than you expect. Not all fields you add to your content type are part of one table. It's more that every field you add is different table and you'll need a lot of joins to get the content you need. That's why it's better to use views...

这篇关于将节点id与mysql表id匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆