2012-04-25 5 views
1

私はいくつかの項目を含むmysqlデータベースを持っていますが、このmysqlデータベースの項目数に応じてjquery経由でフッタセクションのトッププロパティを変更したいと思います。mysqlデータベース内の項目数に応じてdivにトッププロパティを適用

これはこれはnewsletter.php

<script> 
    $.getJSON('newsletter.php?rowCount', function(data) { 
     var jsonData = $.parseJSON(data); 
     var ntop = jsonData.rowCount * 250; 
     $('#footer').css('top', ntop); 
    }); 
</script> 

のクライアント側の部分はこれがtheoricalソリューションですが、ブラウザがエラーログを送信newsletter.php

<?php 
    //allow sessions to be passed so we can see if the user is logged in 
    session_start(); 
    //connect to the database so we can check, edit, or insert data to our users table 
    $con = mysql_connect('localhost', 'user', 'pwd') or die(mysql_error()); 
    $db = mysql_select_db('newsletter', $con) or die(mysql_error()); 
    $SQL = "SELECT * FROM papers"; 
    if(array_key_exists('rowcount', $_GET)) { 
     $query = mysql_query("select count(*) as total FROM papers"); 
     $result = mysql_fetch_array($query); 
     $json = array('rowCount' => $result); // can add more data here 
     return json_encode($json); 
    } 
?> 

のサーバー側の一部である「jsonDataがありますnull '

答えて

3

これを行う1つの方法は、エンドポイントを提供することです.JQueryは$ .post、$ .get、$ .ajaxなどのクエリを実行できます。

$.getJSON('ajax.php?rowcount', function(data) { 
    var jsonData = $.parseJSON(data); 

    var ntop = jsonData.rowCount * 250; 
    $('#footer').css('top', ntop); 
} 

などPHPスクリプトの何か:

if(array_key_exists('rowcount', $_GET)) { 
    $query = mysql_query("select count(*) as total from table_name"); 
    $result = mysql_fetch_array($query); 
    $json = array('rowCount' => $result); // can add more data here 
    return json_encode($json); 
} 
+0

OK。私はできるだけ早くしようとしていますが、私はそれがうまくいくかどうかを教えてくれるでしょう –

+0

infensus、私は容赦しますが、私は理解しますが、コードのどの部分をmysql接続のユーザ/ – 7thkernel

+0

mysqlはサーバ側であるため、あなたはそのスクリプトのどこかに自由にデータベースに接続することができます。あるいは、別のmysqlスクリプトを作成してインクルードして、データベースを開く、閉じるなどの方法があります。 mysql-> open()..do query .. mysql-> close(); jsonを返す。 –

1
function redraw(){ 
    var ntop = <?php echo $result['total'];?> * 250 
    ..... 
} 

これは何ですか?

関連する問題