douxian3170
2018-05-31 19:55
浏览 48
已采纳

会话变量不与嵌入式PHP共享

I have an hmtl file that I have named with a .php extension so I could access php session variable...

<?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>

... and as you can see toward the end of the above code the following embedded php...

<?php echo $test_user_id; ?>  

to test that variable works and it does.

However, you will also see the following last line of the above code...

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

...which has the following code...

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));

.
.
.

... which works, but the last line which is commented out does NOT work, presumably because there is a problem with $test_user_id which is the only difference between the last two lines.

Ideas?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douhuo0884 2018-05-31 20:15
    已采纳

    Not sure what you want in the var data = [ (I assume you are fetching and echo rows data somewhere) but you can't output before the 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?
    ?>
    
    ];
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题