douzhanglun4482 2017-06-23 05:53
浏览 101

在Javascript中嵌入PHP

I need to embed PHP into Javascript so that when the user selects Countries it would display the result from the query alphabatically and if he selects Numbers then list based on numbers in descending.

Having researched, I have applied this (echo concept into my code) but doesn't seem to work.

I have the following query written in PHP that output staffs' country of birth( no of staff born in number of countries) in ascending order:

$querytest = "select x , COUNT( * )  from( select `staffbirthplace` as x from staffbirthdetails where staffemailid IN(SELECT staffemailid FROM staff where orgid='" . $orgId . "'  AND deptname='" . $deptName . "' AND teamname='" . $teamName . "') ) as temptable group by x order by count(*) ASC ";

Then, I have a dropdown in HTML:

<form>
    <label for="sortorder">Sort by:</label>
    <select id="sortByDropdown" onchange="sortBy(this);">
      <option value="alphabatically">Countries</option>
      <option value="numbers">Number</option>
    </select>
</form>

Furthermore, I have a Javascript function sortBy()

function sortBy(){
    var sortByDrpdownDiv =  document.getElementById('sortByDropdown');

if (sortByDrpdownDiv[sortByDrpdownDiv.selectedIndex].value == 'numbers'){
    alert("yo in if statement");
    <?php $querytest = "select x , COUNT( * ) from( select `staffbirthplace` as x from staffbirthdetails where staffemailid IN(SELECT staffemailid FROM staff where orgid='" . $orgId . "'  AND deptname='" . $deptName . "' AND teamname='" . $teamName . "') ) as temptable group by x order by count(*) DESC ";
    $result = mysql_query($querytest);

    while ($row = mysql_fetch_assoc($result)) {
            echo "<b>";
            echo $row['x'];
            echo ": </b>&nbsp;";
            echo $row['COUNT( * )'];
            echo "<br/>";
        }?>
    document.getElementById('staffbirthplaces').innerHTML = <?php echo $row?>;
    }
}

First I am going only for Numbersbecause the same logic will apply to the Countries. Any help will be appreciated

  • 写回答

1条回答 默认 最新

  • duanhuren5581 2017-07-19 04:25
    关注

    So, i finally did it! Used switch instead of IF statement. Below is the code:

       function sortByAlphabetsOrNumbers(obj){
    
        var selectedValue = obj.options[obj.selectedIndex].value
    switch(selectedValue)
    {
        case "numberOfStaff":
            document.getElementById('sortBy').innerHTML = 
            "<?php
                include 'connection.php';
    
                $staffNumbersDesc = "select x , COUNT( * )  from( select `staffbirthplace` as x from staffbirthdetails where staffemailid IN(SELECT staffemailid FROM staff where orgid='" . $orgId . "'  AND deptname='" . $deptName . "' AND teamname='" . $teamName . "') ) as temptable group by x order by count(*) DESC";
                $result = mysql_query($staffNumbersDesc);               
                while ($row = mysql_fetch_assoc($result)) 
                {
                    echo "<b>";
                    echo $row['x'];
                    echo ": </b>&nbsp;";
                    echo $row['COUNT( * )'];
                    echo "<br/>";
                    } 
            ?>";
            document.getElementById('birthCountriesAlphabaticalOrder').style.display = "none";
            break;
    
        case "countries":
            document.getElementById('sortBy').innerHTML = 
            "<?php
                include 'connection.php';
    
                $alphabaticalOrder = "select x , COUNT( * )  from( select `staffbirthplace` as x from staffbirthdetails where staffemailid IN(SELECT staffemailid FROM staff where orgid='" . $orgId . "'  AND deptname='" . $deptName . "' AND teamname='" . $teamName . "') ) as temptable group by x";
                $result = mysql_query($alphabaticalOrder);              
                while ($row = mysql_fetch_assoc($result)) 
                {
                    echo "<b>";
                    echo $row['x'];
                    echo ": </b>&nbsp;";
                    echo $row['COUNT( * )'];
                    echo "<br/>";
                    } 
            ?>";
            document.getElementById('birthCountriesAlphabaticalOrder').style.display = "none";
            break;
    }
    };
    

    Hope it helps someone

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度