首页 > 解决方案 > 会话变量不与嵌入式 PHP 共享

问题描述

我有一个带有 .php 扩展名的 hmtl 文件,因此我可以访问 php 会话变量...

<?php
session_start();
$test_user_id = $_SESSION['my_user_id'];
?>

<!DOCTYPE html>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet'>
<style>
body { background-color: black; font-family: 'Montserrat';font-size: 15px; font-weight: Bold; direction: rtl}
.bar:hover { fill: brown; }
.axis--x path { display: none; }
.NOTlabel {font-family:'Montserrat'; font-size: 15px; font-weight: Bold}   
div.tooltip {position: absolute;text-align: center;width: 75px;height: 28px;padding: 2px;font: 12px sans-serif;background: lightsteelblue;border: 0px;border-radius: 8px;pointer-events: none;}    
text {direction: ltr;}    
</style>
<body>

<?php echo $test_user_id; ?>  

<script src="https://d3js.org/d3.v4.min.js"></script>
<script type="text/javascript" src="charts_v0.12.php"></script>

...正如您在上述代码的末尾看到的那样,以下嵌入式 php...

<?php echo $test_user_id; ?>  

测试该变量是否有效。

但是,您还将看到上述代码的最后一行...

<script type="text/javascript" src="charts_v0.12.php"></script>

...具有以下代码...

var data = [
    <?php

//session_start();
//$test_user_id = $_SESSION['my_user_id'];



//date_default_timezone_set('America/New_York');   
$dbconn = pg_connect("host=localhost dbname=postgres user=postgres password=kevin234")
    or die('Could not connect: ' . pg_last_error());


$query = pg_query_params($dbconn, 'select time::date, mode() WITHIN GROUP (ORDER BY watts) AS modal_value, date_part(\'week\', time), count(secs) / 60, round(avg(bpm) ), round(avg(cadence)), round(mode() WITHIN GROUP (ORDER BY watts) / round(avg(bpm)), 2) as ratio from "Workout" where work_interval_flag = 1 and user_id = $1 group by time::date, date_part(\'week\', time) having round(avg(watts)) > 0 order by time::date', array(2));

//$query = pg_query_params($dbconn, 'select time::date, mode() WITHIN GROUP (ORDER BY watts) AS modal_value, date_part(\'week\', time), count(secs) / 60, round(avg(bpm) ), round(avg(cadence)), round(mode() WITHIN GROUP (ORDER BY watts) / round(avg(bpm)), 2) as ratio from "Workout" where work_interval_flag = 1 and user_id = $1 group by time::date, date_part(\'week\', time) having round(avg(watts)) > 0 order by time::date', array($test_user_id));

.
.
.

...这行得通,但是注释掉的最后一行不起作用,大概是因为 $test_user_id 存在问题,这是最后两行之间的唯一区别。

想法?

标签: javascriptphphtml

解决方案


不确定您想要什么var data = [(我假设您正在某处获取和回显行数据),但您无法在以下内容之前输出session_start()

<?php

session_start();
$test_user_id = $_SESSION['my_user_id'];

$dbconn = pg_connect("host=localhost dbname=postgres user=postgres password=kevin234")
    or die('Could not connect: ' . pg_last_error());

$query = pg_query_params($dbconn, 'select time::date, mode() WITHIN GROUP (ORDER BY watts) AS modal_value, date_part(\'week\', time), count(secs) / 60, round(avg(bpm) ), round(avg(cadence)), round(mode() WITHIN GROUP (ORDER BY watts) / round(avg(bpm)), 2) as ratio from "Workout" where work_interval_flag = 1 and user_id = $1 group by time::date, date_part(\'week\', time) having round(avg(watts)) > 0 order by time::date', array($test_user_id));

?>

var data = [

<?php
//fetch and echo row data?
?>

];

推荐阅读