php - 当输入通过json变量传递时,mysql中的输出为0

标签 php mysql arrays json handlebars.js

我有一个疑问:

JSON 的数据呈现如下:

<script type="text/x-handlebars-template" id="waterfall-tpl"> {{#result}} <?php echo {{img_id}}; ?> {{/result}}

我用过的:

$tty= utf8_decode('217');   
  $tty = str_replace("'","","$tty");
  $tty1 = "'".$tty;
  $tty2 = $tty1."'";


$number = mysql_fetch_array(mysql_query("select count(user_id) from `comments` where    `img_id`=".$tty2));
  print_r($number);

在本例中,$tty2 解析为“217”,并且输出数组 $number 按预期解析,即:

Array ( [0] => 2 [count(user_id)] => 2 )   

但是,当我将 json 变量输入(使用 Handlebars.js)传递为:

     $tty= utf8_decode('{{img_id}}');   
     $tty = str_replace("'","","$tty");
     $tty1 = "'".$tty;
     $tty2 = $tty1."'";

 $number = mysql_fetch_array(mysql_query("select count(user_id) from `comments` where `img_id`=".$tty2));
 print_r($number);

在本例中,$tty2 解析为 '217' 并且输出数组 $number 解析为:

Array ( [0] => 0 [count(user_id)] => 0 )  

数组输出应该给我值 2,但给我的却是 0。

完整代码:

<script type="text/x-handlebars-template" id="waterfall-tpl">


        {{#result}}
        <div class="item">

            <a title ="<u>{{title}}</u></br>{{story}}" 

               class="fancybox" href="{{image}}" rel="gallery"><img src="{{image}}" width="{{width}}" height="{{height}}" alt = ""/> </a>       

            <span style="color:#868686;font-size:13px;"><b>{{title}}</b>&nbsp</span> |&nbsp <span style="color:#8A8A8A;font-size:12px" >Uploaded by: {{user_name}}</span> <hr />
            <!-- <span style="color:#8A8A8A;font-size:12px;line-height:120%;display:block;padding-top:7px;padding-bottom:7px;">{{story}} </span> -->

            <span class = "up">
                <a href="" class = "dup"><b>+53</b></a></br> 
                <a href="" class = "ddown"><b class = "td">-26</b></a>  
            </span>


            <div class="comment-wrapper">

                <div class="comment-insert">

                    <div class="comment-insert-container" 
                         style="position: relative;
                         margin: 0px;
                         border:1px solid #e1e1e1;
                         min-height: 66px;
                         width: auto;"> 
                        <textarea id="{{textarea_id}}" class="comment-insert-text" style = "border: 1px solid #e1e1e1"></textarea>
                    </div>



                    <div id="{{btn_id}}" style = "margin: 2px 2px 0 0;width:26px;height: 25px;"class="comment-post-btn-wrapper"  >
                        C
                    </div>

                </div>

                <div class="comments-list">

                    <ul class="comments-holder-ul " id = {{ul_id}}>
                        <input type = "text" id = "btn_id1" value = "{{btn_id}}" />


                                          <li class="comment-holder" id="_1">

                                          <div class="user-img">
                                                  <img src="images/Setting-icon.png" class="user-img-pic" /> 
                                          </div>

                                          <div class="comment-body">
                                                  <h3 class="username-field" >Fixed User</h3>

                                                  <div class="comment-text">
                                                  <?php echo "{{img_id}}" ?>

                                                  </div>
                                          </div>

                                          <div class="comment-buttons-holder">
                                                  <ul>
                                                          <li class="delete-btn">[x]</li>
                                                  </ul>
                                          </div>

                                          </li>


    <!-- To display already commented posts -->  



     <?php  

    $tty= utf8_decode('{{img_id}}');   
   $tty = str_replace("'","","$tty");
     $tty1 = "'".$tty;
     $tty2 = $tty1."'";

     include 'connect.php';

     $qur = "select count(user_id) from `comments` where `img_id`=".$tty2;
     print_r ($qur);


    $number = mysql_fetch_array(mysql_query($qur));
 print_r($number);


     for ($k = 1; $k <= $num_of_comments2; $k++){

            {

            $smthng = new stdClass();
        $smthng->comment_id = 24;
        $smthng->Userid = 1;
        $smthng->comment = "Hard coded comments";
        $smthng->Username = "Sagar_username"; 
        //$smthng->profile_img = "images/Setting-icon.png";


        $data =  json_encode($smthng);

$ul_id = "ul218";

      $a = '1';
      $a = $a.'2';
      $comm = "Commented";

    $t = '<li class="comment-holder" id="">';
    $t = $t.'<div class="user-img">';
  //  $t = $t.'<img src="' + data.profile_img + '" class="user-img-pic" />';
    $t = $t.'</div>';
    $t =  $t.'<div class="comment-body">';
    //$t = $t.'<h3 class="username-field" >' + data.Username + '</h3>';
    $t = $t.'<div class="comment-text">'.$comm.'</div>';
    $t = $t.'</div>';
    $t = $t.'<div class="comment-buttons-holder">';
    $t = $t.'<ul>';
    $t = $t.'<li class="delete-btn">[x]</li>';
    $t = $t.'</ul>';
    $t = $t.'</div>';
    $t = $t.'</li>';



      echo $t;

            }   
      }

      ?>












                    </ul>

                </div> 
            </div>






        </div>


        {{/result}}
    </script>

最佳答案

希望您知道 JS 在浏览器中执行,而 PHP 在 Web 服务器中执行。 <script type="text/x-handlebars-template" id="waterfall-tpl">...</script> 中的所有 PHP 代码将在到达浏览器之前执行,因此 $tty= utf8_decode('{{img_id}}');将无法工作,因为它没有数据。

还有{{img_id}}是一个 Handlebar 模板变量,只有在像这样解析时才会获取值

var source   = $("#waterfall-tpl").html();
var template = Handlebars.compile(source);

我希望您的代码中还有另一部分,您可以将值传递给 Handlebar 变量并解析它。像下面这样,

var raw_template = $('#waterfall-tpl').html();
var template = Handlebars.compile(raw_template);
var data = {result:[title: "Some thing here", story: "Some thing here", image: "Some thing here", img_id: "Some thing here" ... ], [title: "Some thing here", story: "Some thing here", image: "Some thing here", img_id: "Some thing here" ... ], .... };
var html = template(data);

我希望你必须更换Some thing here使用 PHP 中的适当数据使其正常工作。

关于php - 当输入通过json变量传递时,mysql中的输出为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22877553/

相关文章:

php - 关闭编译器执行中的PHP错误

php - 在php中的关联多维数组中插入键值对

php - 排序实体依赖

javascript - 根据数组长度重复 ReactDOM.render

java - 遍历 pig 中的数组

php - 按 id ASC 从数据库顺序中选择最后 3 行

php - 如何使用 Zend_Db 有效地读取大量行?

php - php验证中的解析错误

python - 在 django 中创建 json 数组

mysql查询在上个月和下个月之间进行选择