繁体   English   中英

从SQL查询中用PHP变量回显JS函数

[英]echo JS functions with PHP variables from SQL query

我有存储在SQL表中的函数,需要在JS内部使用。 问题是,我还需要在这些函数中使用PHP变量。

下列:

<?php
  $container='11';
  $title='Header';
  $function_text =
  <<<EOT
    $(function() {
      $('#container$container').parent('div').find('h3').html('$title');
    });
  EOT;
  echo $function_text;
?>

正确返回:

$(function() {
  $('#container11').parent('div').find('h3').html('Header');
});

但是这个:

<?php
  $ID=1;
  $container='11';
  $title='Header';
  $article = $cls -> Query(sprintf('SELECT * from graphs WHERE ID="%s"', $ID));
  $function_text = $article[0]['function'];
  echo $function_text;
?>

精确打印SQL字段的内容,而不识别变量:

$(function() {
  $('#container$container').parent('div').find('h3').html('$title');
});

我如何获得要注入回显文本的变量?

  1. 更改存储在数据库中的数据,以便它使用格式占位符而不是对变量的引用:

     $(function() { $('#container%s').parent('div').find('h3').html('%s'); }); 
  2. 使用sprintf()

     $function_text = sprintf($article[0]['function'], $container, $title); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM