在javascript中使用动态php变量

时间:2014-08-22 10:28:54

标签: javascript php jquery mysql

我有一个图像数据库和一个搜索表单,所以我想在下一页显示图像,我必须使用javascript来显示(openlayer库) 我写了这段代码

<?php mysql_connect('localhost','root',"");
    mysql_select_db('geo-image');
    $am=0 ;
    $sql="SELECT * FROM images WHERE satellite='".$_POST["satellite"]."'";
    $result=mysql_query($sql)or die(mysql_error());
    $count = mysql_num_rows($result);
    for ($i=1; $i<=$count; $i++)
    {
    $rows = mysql_fetch_array($result);
    ${'a'.$i}=$rows['xpos'];
    ${'b'.$i}=$rows['ypos'];
    ${'c'.$i}=$rows['width'];
    ${'d'.$i}=$rows['height'];
    ${'e'.$i}=$rows['xoffset'];
    ${'f'.$i}=$rows['yoffset'];
    ${'g'.$i}=$rows['rotation'];
    ${'h'.$i}=$rows['resolution'];
    ${'i'.$i}=$rows['name'];
    ${'j'.$i}=$rows['url'];
    }
    ?>

和javascript代码:

 var a1="<?php echo $a1?>";
    var b1="<?php echo $b1?>";
    var c1="<?php echo $c1?>";
    var d1="<?php echo $d1?>";
    var e1="<?php echo $e1?>";
    var f1="<?php echo $f1?>";
    var g1="<?php echo $g1?>";
    var h1="<?php echo $h1?>";
    var a2="<?php echo $a2?>";
    var b2="<?php echo $b2?>";
    var c2="<?php echo $c2?>";
    var d2="<?php echo $d2?>";
    var e2="<?php echo $e2?>";
    var f2="<?php echo $f2?>";
    var g2="<?php echo $g2?>";
    var h2="<?php echo $h2?>";
    var l1=340;
    var l2=346;

    if("<?php echo $count?>"==1){
    anArray[anArray.length] = [ a1,b1, c1, d1, e1, f1, g1, h1,l1,"image/<?php echo $j1?>.jpg"];
    } else if("<?php echo $count?>"==2){
    anArray[anArray.length] = [ a1,b1, c1, d1, e1, f1, g1, h1,l1,"image/<?php echo $j1?>.jpg"];
    anArray[anArray.length] = [ a2,b2, c2, d2, e2, f2, g2, h2,l2,"image/<?php echo $j2?>.jpg"];
    } else if("<?php echo $count?>"==3){
    anArray[anArray.length] = [ a1,b1, c1, d1, e1, f1, g1, h1,l1,"image/<?php echo $j1?>.jpg"];
    anArray[anArray.length] = [ a2,b2, c2, d2, e2, f2, g2, h2,l2,"image/<?php echo $j2?>.jpg"];
    anArray[anArray.length] = [ a3,b3, c3, d3, e3, f3, g3, h3,l3,"image/<?php echo $j3?>.jpg"];
    } 

当在数据库搜索中我有两个结果这个代码正常工作,但当结果超过两个代码不起作用,我知道问题是关于我的变量在JavaScript中但我不知道修复它,可以任何一个人帮我解决了吗?

3 个答案:

答案 0 :(得分:0)

PHP:

<?php mysql_connect('localhost','root',"");
    mysql_select_db('geo-image');

    $ARR_DATA = array(); 
    $sql="SELECT * FROM images WHERE satellite='".$_POST["satellite"]."'";
    $result=mysql_query($sql)or die(mysql_error());
    while($rows =  mysql_fetch_array($result)
    {
        $arr_temp['a'] = $rows['xpos'];
        $arr_temp['b'] = $rows['ypos'];
        $arr_temp['c'] = $rows['width'];
        $arr_temp['d'] = $rows['height'];
        $arr_temp['e'] = $rows['xoffset'];
        $arr_temp['f'] = $rows['yoffset'];
        $arr_temp['g'] = $rows['rotation'];
        $arr_temp['h'] = $rows['resolution'];
        $arr_temp['i'] = $rows['name'];
        $arr_temp['j'] = $rows['url'];

        $ARR_DATA[] = $arr_temp;
    }

    $count = count($ARR_DATA);
?>

JS:

<script>

<?php
foreach($ARR_DATA as $key=>$arr_temp)
{
?>
    var a = "<?php echo $arr_temp['a'];?>";
    var b = "<?php echo $arr_temp['b'];?>";
    var c = "<?php echo $arr_temp['c'];?>";
    var d = "<?php echo $arr_temp['d'];?>";
    var e = "<?php echo $arr_temp['e'];?>";
    var f = "<?php echo $arr_temp['f'];?>";
    var g = "<?php echo $arr_temp['g'];?>";
    var h = "<?php echo $arr_temp['h'];?>";
    var i = "<?php echo $arr_temp['i'];?>";
    var j = "<?php echo $arr_temp['j'];?>";

    var img = "image/"+j+".jpg";

    anArray[<?php echo $key;?>] = [ a, b, c, d, e, f, g, h, i, j];
<?php
}
?>
</script>

答案 1 :(得分:0)

试 在PHP代码

<?php mysql_connect('localhost','root',"");
    mysql_select_db('geo-image');
    $am=0 ;
    $sql="SELECT * FROM images WHERE satellite='".$_POST["satellite"]."'";
    $result=mysql_query($sql)or die(mysql_error());
    $count = mysql_num_rows($result);
    $images = array();
    for ($i=1; $i<=$count; $i++)
    {
    $rows = mysql_fetch_array($result);
    $images[] = $rows;
    }
    ?>

在js代码中

    <?php       
    foreach ($images as $image){
    ?>
        anArray[anArray.length] = [ "<?php echo  $image['xpos'];?>","<?php echo  $image['ypos'];?>", "<?php echo  $image['width'];?>", "<?php echo  $image['height'];?>", "<?php echo  $image['xoffset'];?>", "<?php echo  $image['yoffset'];?>", "<?php echo  $image['rotation'];?>", "<?php echo  $image['resolution'];?>","<?php echo  $image['name'];?>","image/<?php echo $image['url']?>.jpg"];
<?php 
        }
?>

答案 2 :(得分:-1)

在php和js之间进行通信最安全的方法是在我看来使用json。另外,我建议您使用pdo或mysqli而不是mysql来与数据库进行通信。