简体   繁体   中英

Trying to binding the combobox with mysql and PHP

I have a problem with combobox filling with data.

I have a table with columns:

  • usrIndex
  • usrNickname
  • usrFullname
  • usrEmail

We are binding the data include joins with other table (like hrh...) and we are caluculating user heart rate and their calories like that, by using following code:

$totalPoints = 0;
$totalDuration = 0;
$totalCalories = 0;
$noOfParticipants = 0;
$includedUsers = 0;
$participants = "";

Main Program

$query = "SELECT DISTINCT usrIndex,usrNickname";
$query .= " FROM hrheader hrh";
$query .= " INNER JOIN `user` usr ON hrh.hrhUsrIndex=usr.usrIndex";
$query .= " WHERE usrSIndex=$sIndex";
$query .= " AND hrhStart>'" . $start . "' AND hrhStart<'" . date("Y-m-d", strtotime($end . " +1 days")) . "'";
$query .= " ORDER BY usrNickname";

$gresult = mysql_query($query);
if ($gresult) 
{
    $noOfParticipants = mysql_num_rows($gresult);
    $participants = "<table style='text-align:center;font-size:18px;width:980px;margin-   top:16px;color:#231f20;'>";
    $participants .= "<tr>";
    $participants .= "<th>Nickname</th><th>Time (Hours &amp; Mins)</th><th>Average Effort</th><th>Calories</th><th><img src='../images/mepslogo.png' /></th>";
    $participants .= "</tr>";

The below code and diagram helps for clearly understanding the question:

while ($grow = mysql_fetch_array($gresult)) 
{
    $query = "SELECT ROUND(AVG(hr/hrhMaxHR)*100) AS AverageEffort,ROUND(AVG(hr)) AS AverageHeartRate,ROUND(MAX(hr)) AS MaxHeartRate,";
    $query .= " COUNT(DISTINCT hrhIndex) AS NumberOfMoves";
    $query .= " FROM `hrheader` hrh INNER JOIN hr ON hrh.hrhIndex=hr.hrHrhIndex";
    $query .= " WHERE hrhUsrIndex=" . $grow['usrIndex'] . " AND hr>0";
    $query .= " AND hrReceivedTime>'" . $start . "' AND hrReceivedTime<'" . date("Y-m-d", strtotime($end . " +1 days")) . "'";

    $result = mysql_query($query);
    $pAverageEffort = 0;
    $pAverageHeartRate = 0;
    $pMaxHeartRate = 0;
    $pNumberOfMoves = 0;

    if ($result)
    {
        $row = mysql_fetch_array($result);
        if ($row) 
        {
            $pAverageEffort = $row['AverageEffort'] . "%";
            $pAverageHeartRate = $row['AverageHeartRate'];
            $pMaxHeartRate = $row['MaxHeartRate'];
            $pNumberOfMoves = $row['NumberOfMoves'];
        }
    }

    $query = "SELECT hr,hrReceivedTime,hrhMaxHR,hrhWeight,FLOOR(DATEDIFF(hrReceivedTime,usrDOB)/365) AS Age,usrGender";
    $query .= " FROM hrheader hrh INNER JOIN hr ON hrh.hrhIndex=hr.hrHrhIndex";
    $query .= " INNER JOIN `user` usr ON hrh.hrhUsrIndex=usr.usrIndex";
    $query .= " WHERE hrhUsrIndex=" . $grow['usrIndex'] . " AND hr>0";
    $query .= " AND hrReceivedTime>'" . $start . "' AND hrReceivedTime<'" . date("Y-m-d", strtotime($end . " +1 days")) . "'";
    $query .= " ORDER BY hrReceivedTime";
    //echo $query;
    $result = mysql_query($query);
    $lastReceived = false;

    if ($result) 
    {
        $t = 0;
        $pDuration = 0;

        $zone1 = 0;
        $zone2 = 0;
        $zone3 = 0;
        $zone4 = 0;
        $zone5 = 0;
        $count = 0;
        $pCalories = 0;

        while ($row = mysql_fetch_array($result))
        {
            if ($lastReceived)
            {
                $t = TimeDifference($lastReceived, $row['hrReceivedTime']);
                if ($t < 5)
                {
                    if ($row['hr'] > 0) 
                    {
                        if ($row['hr'] > $row['hrhMaxHR'] * 0.9) 
                        {
                            $zone5 += $t;
                        }
                        elseif ($row['hr'] > $row['hrhMaxHR'] * 0.8)
                        {
                            $zone4 += $t;
                        }
                        elseif ($row['hr'] > $row['hrhMaxHR'] * 0.7)
                        {
                            $zone3 += $t;
                        }
                        elseif ($row['hr'] > $row['hrhMaxHR'] * 0.6)
                        {
                            $zone2 += $t;
                        }
                        else
                        {
                            $zone1 += $t;
                        }
                        $count++;
                        $pDuration += $t;
                        $pCalories += calculateCalPerMin($row['hr'], $row['hrhWeight'], $row['Age'], $row['usrGender']) * $t;
                    }
                }
            }
            $lastReceived = $row['hrReceivedTime'];
        }
        $zone1 = floor($zone1);
        $zone2 = floor($zone2);
        $zone3 = floor($zone3);
        $zone4 = floor($zone4);
        $zone5 = floor($zone5);

        $pPoints = 0;
        $pPoints+=calculatePoints(1, $zone1);
        $pPoints+=calculatePoints(2, $zone2);
        $pPoints+=calculatePoints(3, $zone3);
        $pPoints+=calculatePoints(4, $zone4);
        $pPoints+=calculatePoints(5, $zone5);

        $totalCalories+=$pCalories;
        $totalPoints+=$pPoints;
        $totalDuration+=$pDuration;

        if (mysql_num_rows($result) > 0) 
        {
            mysql_data_seek($result, 0);
            $row = mysql_fetch_array($result);
            $include = 1;
            if (!empty($targetGroup)) 
            {
                if ($targetGroup != $row['usrGender'])
                {
                    $include = 0;
                }
            }
            if (!empty($targetNoOfSessions)) 
            {
                if ($targetNoOfSessions < $pNumberOfMoves) 
                {
                    $include = 0;
                }
            }
            if (!empty($targetDuration))
            {
                $x = explode(":", $targetDuration);
                $targetDuration = ($x[0] * 60) + $x[1];
                if ($targetDuration < $pDuration)
                {
                    $include = 0;
                }
            }
            if (!empty($targetAveEffort))
            {
                if ($targetAveEffort > floor($pAverageEffort))
                {
                    $include = 0;
                }
            }
            if (!empty($targetTimeInZones))
            {
                $targetTimeInZones = intval($targetTimeInZones);
                if (!empty($targetTimeInZones))
                {
                    switch ($targetTimeInZones)
                    {
                        case 1:
                            if (empty($zone1))
                                $include = 0;
                            break;
                        case 2:
                            if (empty($zone2))
                                $include = 0;
                            break;
                        case 3:
                            if (empty($zone3))
                                $include = 0;
                            break;
                        case 4:
                            if (empty($zone4))
                                $include = 0;
                            break;
                        case 5:
                            if (empty($zone5))
                                $include = 0;
                            break;
                    }
                }
            }
            if (!empty($targetCalories))
            {
                if ($targetCalories >= $pCalories)
                {
                    $include = 0;
                }
            }
            if (!empty($targetPoints))
            {
                $debug .= "$targetPoints - $pPoints --";
                if ($targetPoints >= $pPoints)
                {
                    $include = 0;
                }
            }

            if ($include == 1)
            {
                $includedUsers+=1;
            }
        }
        if ($include == 1)
        {
            $participants .= "<tr><td>" . $grow['usrNickname'] . "</td><td>" . FormatTime($pDuration) . "</td><td>$pAverageEffort</td><td>" . floor($pCalories) . "</td><td>$pPoints</td></tr>";
        }
    }
}
$participants .= "</table>";

and this is html code for Main program (here we are defining the html table):

<div class="txt" style="margin-right:10px;margin-top:16px;font-size:18px;width:462px;float:right">
    <div class="txtl"></div>
    <div class="txtr"></div>
    <span>% of group that achieved target</span>
    <span id="spanPercentAchieved" style="color:black;float:right">-%</span>
</div>
<div class="txt" style="margin-left:10px;margin-top:16px;font-size:18px;width:462px">
    <div class="txtl"></div>
    <div class="txtr"></div>
    <span>Number of users achieved target</span>
    <span id="spanNumberAchieved" style="color:black;float:right">-</span>
</div>
<span id="spanParticipants">
    <table style='text-align:center;font-size:18px;width:980px;margin-top:16px;color:#231f20;'>
        <tr>
            <th>Nickname</th>
            <th>No of Sessions</th>
            <th>Duration</th>
            <th>Average Effort</th>
            <th><img src='../images/mepslogo.png' /></th>
        </tr>
    </table>
</span>

I have indicated the query and binding the data to html table in "Main Program"

Its working fine with this code...... But my problem is

I want to set the combobox inside html table at nickname header in that combobox the options are like (Nickname, Full Name, Email) when the user select these options (full name, email) they will able to see the users email and full name those whose nickname is currently displayed..

I want like this:

在此处输入图像描述

If the user select the full name, full name will be displayed instead of Nickname and when the user select the email id the email id will be displayed instead of nickname of the users.

This combox selection will be set in table column header.

Would anyone please help on this?

Can anyone help on this one...this topic.

You looking for somthing like this? http://test.puggan.se/test/column_test.php

This is a plain javascript soulotion, tested in firefox, There probely an easier solution whit JQuery. if you tag you question whit or , you probely get a few more answer of people that know thous langueges

code in exemple linked above:

<html>
    <head>
        <title>select column test</title>
        <script>
            function select_column(column_name)
            {
                document.getElementById('column_style').textContent = '.col_hideable div.col_' + column_name + ' {display: block;}';
            }
        </script>
        <style id='column_style'>
            .col_hideable div.col_nick_name {display: block;}
        </style>
        <style>
            TH, TD {border: solid gray 1px;}
            .col_hideable div {display: none;}
        </style>
    </head>
    <body>
        <table id='the_table'>
            <thead>
                <tr>
                    <th>
                        <select id='column_selector' onchange='select_column(this.value)'>
                            <option value='nick_name'>Nickname</option>
                            <option value='full_name'>Full name</option>
                            <option value='email'>Email</option>
                        </select>
                    </th>
                    <th>junk</th>
                    <th>junk</th>
                    <th>junk</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class='col_hideable'>
                        <div class='col_nick_name col_hideable'>nickname 1</div>
                        <div class='col_full_name'>fullname 1</div>
                        <div class='col_email'>email 1</div>
                    </td>
                    <td> junk 1-2 </td>
                    <td> junk 1-3 </td>
                    <td> junk 1-4 </td>
                </tr>
                <tr>
                    <td class='col_hideable'>
                        <div class='col_nick_name'>nickname 2</div>
                        <div class='col_full_name'>fullname 2</div>
                        <div class='col_email'>email 2</div>
                    </td>
                    <td> junk 2-2 </td>
                    <td> junk 2-3 </td>
                    <td> junk 2-4 </td>
                </tr>
                <tr>
                    <td class='col_hideable'>
                        <div class='col_nick_name'>nickname 3</div>
                        <div class='col_full_name'>fullname 3</div>
                        <div class='col_email'>email 3</div>
                    </td>
                    <td> junk 3-2 </td>
                    <td> junk 3-3 </td>
                    <td> junk 3-4 </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

Added 2011-08-15 13:30

to implement this in your code, you need to add thous lines inside your head-tag

    <script>
        function select_column(column_name)
        {
            document.getElementById('column_style').textContent = '.col_hideable div.col_' + column_name + ' {display: block;}';
        }
    </script>
    <style id='column_style'>
        .col_hideable div.col_nick_name {display: block;}
    </style>
    <style>
        TH, TD {border: solid gray 1px;}
        .col_hideable div {display: none;}
    </style>

and replace the table-header code

$participants .= "<tr>";
$participants .= "<th>Nickname</th><th>Time (Hours &amp; Mins)</th><th>Average Effort</th><th>Calories</th><th><img src='../images/mepslogo.png' /></th>";
$participants .= "</tr>";

with

$participants .= "<tr>";
$participants .= "  <th>";
$participants .= "    <select id='column_selector' onchange='select_column(this.value)'>";
$participants .= "      <option value='nick_name'>Nickname</option>";
$participants .= "      <option value='full_name'>Full name</option>";
$participants .= "      <option value='email'>Email</option>";
$participants .= "    </select>";
$participants .= "  </th>";
$participants .= "  <th>Time (Hours &amp; Mins)</th>";
$participants .= "  <th>Average Effort</th>";
$participants .= "  <th>Calories</th>";
$participants .= "  <th><img src='../images/mepslogo.png' /></th>";
$participants .= "</tr>";

and replace the table-body code

if ($include == 1)
{
    $participants .= "<tr><td>" . $grow['usrNickname'] . "</td><td>" . FormatTime($pDuration) . "</td><td>$pAverageEffort</td><td>" . floor($pCalories) . "</td><td>$pPoints</td></tr>";
}

with

if ($include == 1)
{
    $participants .= "<tr>";
    $participants .= "  <td class='col_hideable'>";
    $participants .= "    <div class='col_nick_name'>" . $grow['usrNickname'] . "</div>";
    $participants .= "    <div class='col_full_name'>" . $grow['usrFullname'] . "</div>";
    $participants .= "    <div class='col_email'>" . $grow['usrEmail'] . "</div>";
    $participants .= "  </td>";
    $participants .= "  <td>" . FormatTime($pDuration) . "</td>";
    $participants .= "  <td>" . $pAverageEffort . "</td>";
    $participants .= "  <td>" . floor($pCalories) . "</td>";
    $participants .= "  <td>" . $pPoints . "</td>";
    $participants .= "</tr>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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