2012-09-24 74 views
0

我想將當前文章ID傳遞給ajax文件。 ajax文件的URL類似於www.web.com/plugins/system/ajax.php,所以使用JRequest :: getInt(id)總是分析0整數。在非ajax文件中,我可以用同樣的方法獲得ID。所以我想知道如何傳遞整數值,或者有其他方法可以在ajax文件中獲取文章ID?發送文章ID到ajax文件或直接在ajax文件中獲取文章ID?在joomla!插件

<?php 
defined('_JEXEC') or die; 
define('DS', DIRECTORY_SEPARATOR); 
?> 

<?php 
class plgSystemRatingx extends JPlugin 

{ 
    public function onContentBeforeDisplay() 
    { 
?> 
<?php echo JRequest::getInt('id'); ?> 
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() 
{ 
$(".like").click(function() 
{ 
var id=$(this).attr("id"); 
var name=$(this).attr("name"); 
var dataString = 'id='+ id + '&name='+ name; 
$("#votebox").slideDown("slow"); 

$("#flash").fadeIn("slow"); 

$.ajax 
({ 
type: "POST", 
url: "/joomla/plugins/system/ratingx/conf.php", 
data: dataString, 
cache: false, 
success: function(html) 
{ 
$("#flash").fadeOut("slow"); 
$("#content").html(html); 
} 
}); 
}); 

$(".close").click(function() 
{ 
$("#votebox").slideUp("slow"); 
}); 

}); 
</script> 
<?php echo JURI::current(); ?> 

<div style="margin:50px"> 
<a href="#" class="like" id="1" name="up">Like</a> -- <a href="#" class="like" id="1" name="down">Dislike</a> 
<div id="votebox"> 
<span id='close'><a href="#" class="close" title="Close This">X</a></span> 
<div style="height:13px"> 
<div id="flash">Loading........</div> 
</div> 
<div id="content"> 

</div> 

</div> 
</div> 
<?php 


     return true; 
    } 
} 

AJAX FILE:

<?php 
// Set flag that this is a parent file 
define('_JEXEC', 1); 

// No direct access. 
defined('_JEXEC') or die; 

define('DS', DIRECTORY_SEPARATOR); 

define('JPATH_BASE', dirname(__FILE__).DS.'..'.DS.'..'.DS.'..'); 

require_once (JPATH_BASE .DS.'includes'.DS.'defines.php'); 
require_once (JPATH_BASE .DS.'includes'.DS.'framework.php'); 



$db = &JFactory::getDbo(); 

if(JRequest::getInt('id')) 
{ 

$id = JRequest::getInt('id'); 
$name = JRequest::getVar('name'); 

$queryx = "SELECT id from messages"; 
$db->setQuery($queryx); 
$db->query($queryx); 
$idx = $db->loadObjectList(); 




$query = "update messages set $name=$name+1 where id='$id'"; 
$db->setQuery($query); 
$db->query($query) or die('blo5gai'); 

$query2 = "select up,down from messages where id='$id'"; 
$db->setQuery($query2); 
$db->query($query2) or die('blo5gai'); 

$vote = $db->loadObject(); 

$up_value= $vote->up; 
$down_value = $vote->down; 

$total=$up_value+$down_value; 

$up_per=($up_value*100)/$total; 
$down_per=($down_value*100)/$total; 

?> 
<div style="margin-bottom:10px"> 
<b>Ratings for this blog</b> (<?php echo $total; ?> total) 
</div> 
<table width="700px"> 
<?php echo JURI::current(); ?> 
<tr> 
<td width="30px"></td> 
<td width="60px"><?php echo $up_value; ?></td> 
<td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td> 
</tr> 

<tr> 
<td width="30px"></td> 
<td width="60px"><?php echo $down_value; ?></td> 
<td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td> 
</tr> 

</table> 

<?php 

} 
+0

你說的 「讓文章ID在AJAX文件」 是什麼意思?你想用阿賈克斯或其他東西來實際加載文章嗎? – Lodder

+0

不,ajax文件用於將投票結果插入當前文章ID。 – Jonuux

+0

我需要在ajax文件中有文章ID。 – Jonuux

回答

0

你有一個屬性添加爲vid並通過VID屬性傳遞id的值

例如:

<a href="#" class="like" id="1" name="up" vid="<?php echo JRequest::getInt('id'); ?>">Like</a> 

在腳本文件,你必須調用像這樣的屬性。

<script type="text/javascript"> 
    $(document).ready(function() 
    { 
     $(".like").click(function() 
     { 
      var vid=$(this).attr("vid"); 
      alert(vid); 
     } 
    }); 
</script> 
+0

我編輯的第一篇文章,我想你可能想看看代碼的外觀。我試過並且不認爲你提交的代碼會在這裏工作,但是謝謝。 – Jonuux

+0

我編輯了答案,並檢查它。 –

+0

我還是不明白。如何在mysql查詢中使用這個變量? $ query =「update messages set $ name = $ name + 1 where id ='$ id'」; $ db-> setQuery($ query); $ db-> query($ query)或die('blo5gai'); $ query2 =「從消息中選擇其中id ='$ id'」; $ db-> setQuery($ query2); $ db-> query($ query2)或die('blo5gai'); – Jonuux

0
<?php 
// Set flag that this is a parent file 
define('_JEXEC', 1); 

// No direct access. 
defined('_JEXEC') or die; 

define('DS', DIRECTORY_SEPARATOR); 

define('JPATH_BASE', dirname(__FILE__).DS.'..'.DS.'..'.DS.'..'); 

require_once (JPATH_BASE .DS.'includes'.DS.'defines.php'); 
require_once (JPATH_BASE .DS.'includes'.DS.'framework.php'); 

$id = JRequest::getInt('id'); 
$db = &JFactory::getDbo(); 

if($id) 
{  
    $name = JRequest::getVar('name'); 

    $query->select('id'); 
    $query->from('messages'); 
    $db->setQuery($query); 
    $db->query($query); 
    $idx = $db->loadResult(); 


    $query->update('messages'); 
    $query->set('name = '.$name+1); 
    $query->where('id='.$id); 
    $db->setQuery($query); 
    $db->query(); 

    $query->select('up,down'); 
    $query->from('messages'); 
    $query->where('id='.$id); 
    $db->setQuery($query); 
    $db->query($query); 
    $vote = $db->loadObject(); 

    $up_value= $vote->up; 
    $down_value = $vote->down; 

    $total=$up_value+$down_value; 

    $up_per=($up_value*100)/$total; 
    $down_per=($down_value*100)/$total; 

?> 
    <div style="margin-bottom:10px"> 
     <b>Ratings for this blog</b> (<?php echo $total; ?> total) 
    </div> 

    <table width="700px"> 
     <?php echo JURI::current(); ?> 
     <tr> 
      <td width="30px"></td> 
      <td width="60px"><?php echo $up_value; ?></td> 
      <td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td> 
     </tr> 

     <tr> 
      <td width="30px"></td> 
      <td width="60px"><?php echo $down_value; ?></td> 
      <td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td> 
     </tr> 
    </table> 
<?php 

} 
+0

這裏'$ query-> set('name ='。$ name + 1);'$ name是一個字符串。所以不能使用'$ name + 1' –

+0

感謝您的幫助。 RHR的答案正在起作用。 – Jonuux