您在這裡

Drupal中讓Node歸類的PHP程序

James Qi 在 2011年2月10日 - 16:04 發表

  在向Drupal網站中導入大量數據的時候,需要對頁面進行歸類以便分類展示,所謂歸類也就是讓Node在Taxonomy系統中屬于某個Vocabulary的某個Term,這可以通過調用Drupal的API來批量實現,有兩種調用辦法都可以:

  一是修改$node->taxonomy[] = $tid,然後調用node_save($node)來保存;

  另外一種是先組成$terms,然後調用taxonomy_node_save($node, $terms)來保存。

  這兩種辦法都可以實現,效果也是一樣的,我後來用的第二種辦法。在編寫這個PHP程序前我還真的沒有怎麼接觸PHP程序、Drupal API函數,所以花了很多時間不斷編寫、測試、修改,直到最後可用,其實也隻是不長的一段代碼。

  現在列出來供感興趣的朋友參考:

<?php
$_SERVER['HTTP_HOST'] = 'guizhou.youbianku.com';
$_SERVER['SCRIPT_NAME'] = '/'.'refresh_category.php';

$drupal_path = '/usr/local/apache2/htdocs/drupal.youbianku.com/';
chdir($drupal_path);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$node_type = "minglu";
$list_no = 100;
$offset = 0;
$sql = "SELECT node.title, node.type, node.nid FROM {node} WHERE node.type = '$node_type' LIMIT $list_no OFFSET $offset";
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
/*
print_r ($anode);
echo '<br>\n';
*/
$node=node_load($anode->nid);
/*
print_r ($node);
echo '<br>';
*/
//将CCK數據庫字段讀出賦值給變量
$postcode=$node->field_postcode[0]['value'];
$area_number=$node->field_area_number[0]['value'];
/*
echo '$postcode='.$postcode.'<br>';
echo '$area_number='.$area_number.'<br>';
*/
//從行政區劃編号前2位判斷省份
switch (substr($area_number,0,2))
{
case "52":
 $province="貴州省";
 break;
case "";
 $province="沒有行政區号";
 break;
default:
 $province="貴州省以外";
}

//從行政區劃編号前4位判斷地市
switch (substr($area_number,0,4))
{
case "5200":
 $city="貴州省未細分";
 break;
case "5201";
 $city="貴陽市";
 $areacode="0851";
 break;
case "5202";
 $city="六盤水市";
 $areacode="0858";
 break;
case "5203";
 $city="遵義市";
 $areacode="0852";
 break;
case "5222";
 $city="銅仁地區";
 $areacode="0856";
 break;
case "5223";
 $city="黔西南布依族苗族自治州";
 $areacode="0859";
 break;
case "5224";
 $city="畢節地區";
 $areacode="0857";
 break;
case "5225";
 $city="安順地區";
 $areacode="0853";
 break;
case "5226";
 $city="黔東南苗族侗族自治州";
 $areacode="0855";
 break;
case "5227";
 $city="黔南布依族苗族自治州";
 $areacode="0854";
 break;
default:
 $city="默認行政區号";
}

//從行政區劃編号全部6位判斷區縣
switch ($area_number)
{
case "520101":
 $county="市轄區";
 break;
case "520102":
 $county="南明區";
 break;
case "520103":
 $county="雲岩區";
 break;
case "520111":
 $county="花溪區";
 break;
case "520112":
 $county="烏當區";
 break;
case "520113":
 $county="白雲區";
 break;
case "520121":
 $county="開陽縣";
 break;
case "520122":
 $county="息烽縣";
 break;
case "520123":
 $county="修文縣";
 break;
case "520181":
 $county="清鎮市";
 break;
case "520201":
 $county="鐘山區";
 break;
case "520202":
 $county="盤縣特區";
 break;
case "520203":
 $county="六枝特區";
 break;
case "520221":
 $county="水城縣";
 break;
case "520301":
 $county="市轄區";
 break;
case "520302":
 $county="紅花崗區";
 break;
case "520321":
 $county="遵義縣";
 break;
case "520322":
 $county="桐梓縣";
 break;
case "520323":
 $county="綏陽縣";
 break;
case "520324":
 $county="正安縣";
 break;
case "520325":
 $county="道真仡佬族苗族自治縣";
 break;
case "520326":
 $county="務川仡佬族苗族自治縣";
 break;
case "520327":
 $county="鳳岡縣";
 break;
case "520328":
 $county="湄潭縣";
 break;
case "520329":
 $county="餘慶縣";
 break;
case "520330":
 $county="習水縣";
 break;
case "520381":
 $county="赤水市";
 break;
case "520382":
 $county="仁懷市";
 break;
case "522201":
 $county="銅仁市";
 break;
case "522222":
 $county="江口縣";
 break;
case "522223":
 $county="玉屏侗族自治縣";
 break;
case "522224":
 $county="石阡縣";
 break;
case "522225":
 $county="思南縣";
 break;
case "522226":
 $county="印江土家族苗族自治縣";
 break;
case "522227":
 $county="德江縣";
 break;
case "522228":
 $county="沿河土家族自治縣";
 break;
case "522229":
 $county="松桃苗族自治縣";
 break;
case "522230":
 $county="萬山特區";
 break;
case "522301":
 $county="興義市";
 break;
case "522322":
 $county="興仁縣";
 break;
case "522323":
 $county="普安縣";
 break;
case "522324":
 $county="晴隆縣";
 break;
case "522325":
 $county="貞豐縣";
 break;
case "522326":
 $county="望谟縣";
 break;
case "522327":
 $county="冊亨縣";
 break;
case "522328":
 $county="安龍縣";
 break;
case "522401":
 $county="畢節市";
 break;
case "522422":
 $county="大方縣";
 break;
case "522423":
 $county="黔西縣";
 break;
case "522424":
 $county="金沙縣";
 break;
case "522425":
 $county="織金縣";
 break;
case "522426":
 $county="納雍縣";
 break;
case "522427":
 $county="威甯彜族回族苗族自治縣";
 break;
case "522428":
 $county="赫章縣";
 break;
case "522501":
 $county="安順市";
 break;
case "522526":
 $county="平壩縣";
 break;
case "522527":
 $county="普定縣";
 break;
case "522528":
 $county="關嶺布依族苗族自治縣";
 break;
case "522529":
 $county="鎮甯布依族苗族自治縣";
 break;
case "522530":
 $county="紫雲苗族布依族自治縣";
 break;
case "522601":
 $county="凱裡市";
 break;
case "522622":
 $county="黃平縣";
 break;
case "522623":
 $county="施秉縣";
 break;
case "522624":
 $county="三穗縣";
 break;
case "522625":
 $county="鎮遠縣";
 break;
case "522626":
 $county="岑鞏縣";
 break;
case "522627":
 $county="天柱縣";
 break;
case "522628":
 $county="錦屏縣";
 break;
case "522629":
 $county="劍河縣";
 break;
case "522630":
 $county="台江縣";
 break;
case "522631":
 $county="黎平縣";
 break;
case "522632":
 $county="榕江縣";
 break;
case "522633":
 $county="從江縣";
 break;
case "522634":
 $county="雷山縣";
 break;
case "522635":
 $county="麻江縣";
 break;
case "522636":
 $county="丹寨縣";
 break;
case "522701":
 $county="都勻市";
 break;
case "522702":
 $county="福泉市";
 break;
case "522722":
 $county="荔波縣";
 break;
case "522723":
 $county="貴定縣";
 break;
case "522725":
 $county="甕安縣";
 break;
case "522726":
 $county="獨山縣";
 break;
case "522727":
 $county="平塘縣";
 break;
case "522728":
 $county="羅甸縣";
 break;
case "522729":
 $county="長順縣";
 break;
case "522730":
 $county="龍裡縣";
 break;
case "522731":
 $county="惠水縣";
 break;
case "522732":
 $county="三都水族自治縣";
 break;
default:
 $county="";
}
/*
echo '$province='.$province.'<br>';
echo '$city='.$city.'<br>';
echo '$county='.$county.'<br>';
*/
 $term_province = taxonomy_get_term_by_name ($province);
 $tid_province=$term_province[0]->tid;
 $term_city = taxonomy_get_term_by_name ($city);
 $tid_city=$term_city[0]->tid;
 $term_county = taxonomy_get_term_by_name ($county);
 $tid_county=$term_county[0]->tid;
 $term_postcode = taxonomy_get_term_by_name ($postcode);
 $tid_postcode=$term_postcode[0]->tid;
/*
echo '$term_province=';
print_r ($term_province);
echo '$term_city=';
print_r ($term_city);
echo '$term_county=';
print_r ($term_county);
echo '$term_postcode=';
print_r ($term_postcode);
echo '<br>';
echo '$tid_province='.$tid_province,'$tid_city='.$tid_city,'$tid_county='.$tid_county,'$tid_postcode='.$tid_postcode;
echo '<br>';
*/
echo '$nid='.$anode->nid.
', $tid_province='.$tid_province.
', $tid_city='.$tid_city.
'. $tid_county='.$tid_county.
', $tid_postcode='.$tid_postcode.
"<br>\n";
taxonomy_node_save($node,array($tid_province,$tid_city,$tid_county,$tid_postcode));

}
echo "<br>Done!<br>\n"
?>
  上面的程序是以貴州為例,将行政區号代碼讀出,轉換為省份、地市、區縣三級名稱,再找到對應的Term的ID,另外找到郵編對應的Term的ID,然後将該Node關聯這些分類并保存到數據庫中。上面的代碼中有些隻是調試用的,我也沒有做注釋,僅供參考。

 

發表新回應

Plain text

  • 不允許使用 HTML 標籤。
  • 自動將網址與電子郵件地址轉變為連結。
  • 自動斷行和分段。