0

嘿,我有这个用于冒泡的 javascript... 这个脚本从 xml 文件中获取 InfoID 和 InfoData 标记...

<script type="text/javascript">
$(document).ready( function ( ) {
    // Get the XML data from your file
    $.get('scores.xml', function( data ) {

        // Because we've given jQuery the XML datatype, we can jump straight to finding the element.    
        $(data).find('Game').each( function ( ) {

            // The current object now holds a single "GAME" - find the elements we need
            var game_id = $(this).find('InfoID').text( );
            var game_info = $(this).find('InfoData').text( );

            // Create the popup.
            $('.'+game_id).CreateBubblePopup({
                    position : 'left', align : 'center',
                    innerHtml: game_info,
                    innerHtmlStyle: { color:'#FFFFFF', 'text align':'center' },
                    themeName: 'all-black',
                    themePath: 'images/jquerybubblepopup-themes'
            });
        }); // end of each
    }, 'xml'); // The 'xml' tells jQuery to expect XML back from the request
});
</script>

我需要让这个脚本从数据库表而不是 xml 中获取数据。

我的数据库中的表中有相同的 InfoID 和 InfoData 行...

我使用这个 php 脚本从 db 获取数据:

<?php
    // Connect to database server
    mysql_connect("localhost", "root", "asnaeb") or die (mysql_error ());

    // Select database
    mysql_select_db("scores") or die(mysql_error());

    // SQL query
    $strSQL = "SELECT * FROM latest";

    // Execute the query (the recordset $rs contains the result)
    $rs = mysql_query($strSQL);

    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) { 

       // Write the value of the column FirstName (which is now in the array $row)?>
      <?php echo $row['Header'].""; ?>
      <?php echo $row['Row'].""; ?>
      <?php echo $row['Date'].""; ?>
      <?php echo $row['Time'].""; ?>
      <?php echo $row['AwayTeam'].""; ?>
      <?php echo $row['Score'].""; ?>
      <?php echo $row['HomeTeam'].""; ?>
      <?php echo $row['Other'].""; ?>
      <?php echo $row['InfoID'].""; ?>
      <?php echo $row['InfoData'].""; ?>

<?php } mysql_close(); ?>

知道我该怎么做吗?所以我可以删除我的 xml 文件并使用数据库:) 提前致谢。

4

1 回答 1

0

您可以使用带有单独回调函数的 ajax 帖子并从您的 php 脚本返回 json 数据。

试一试:

// try this for your javascript
<script type="text/javascript">
$(document).ready( function ( ) {
    function getGameInfo() {
  $.post("path/to/phpScript.php", 
   // this is the success callback
   function (json) {
    // this calls the function with the returned data
    parseReturnedData(json);
  });
  return false;
};

// process json data to set your game_id and game_info vars
function parseReturnedData(data) {
  var obj = jQuery.parseJSON(data);
  var game_id = obj.InfoID;
  var game_info = obj.InfoData;
}

// Create the popup.
$('.'+game_id).CreateBubblePopup({
  position : 'left', align : 'center',
  innerHtml: game_info,
  innerHtmlStyle: { color:'#FFFFFF', 'text align':'center' },
  themeName: 'all-black',
  themePath: 'images/jquerybubblepopup-themes'
});
</script>

// try this for your php file
<?php

    // declare vars
    $response = array();

    // Connect to database server
    mysql_connect("localhost", "root", "asnaeb") or die (mysql_error ());

    // Select database
    mysql_select_db("scores") or die(mysql_error());

    // SQL query
    $strSQL = "SELECT * FROM latest";

    // Execute the query (the recordset $rs contains the result)
    $rs = mysql_query($strSQL);

    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) { 

       // Write the value of the column FirstName (which is now in the array $row)?>
      $response['Header'] = $Header;
      $response['Row'] = $Row;
      $response['Date'] = $Date;
      $response['Time'] = $Time;
      $response['AwayTeam'] = $AwayTeam;
      $response['Score'] = $Score;
      $response['HomeTeam'] = $HomeTeam;
      $response['Other'] = $Other;
      $response['InfoID'] = $InfoID;
      $response['InfoData'] = $InfoData;

    } 

    echo json_encode($response);



    mysql_close(); 

?>
于 2012-05-31T16:48:52.317 回答