简体   繁体   中英

Viewing manual values in column wise and mysql values in row wise PHP

I have programmed for a seating arragement for Exam hall in PHP. The hall names are given manually by the user it should viewed in the table column wise where as register number from mysql database the values should be viewed in row wise logically both have to done in same time. How to achieve it

Hall Name -> Coloumn wise :: Register name -> Row wise

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Move from one select box to another</title>
        <style type="text/css">
            select {
                width: 200px;
                float: left;
            }
            .controls {
                width: 40px;
                float: left;
                margin: 10px;
            }
            .controls a {
                background-color: #222222;
                border-radius: 4px;
                border: 2px solid #000;
                color: #ffffff;
                padding: 2px;
                font-size: 14px;
                text-decoration: none;
                display: inline-block;
                text-align: center;
                margin: 5px;
                width: 20px;
            }
       </style>
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
       <script>
            function displayCount() {
                var count =  $('#from option').length;
                document.getElementById("countFrom").value=count;
                var count2 =  $('#to option').length;
                document.getElementById("countTo").value=count2;
            }

            function moveAll(from, to) {
                $('#'+from+' option').remove().appendTo('#'+to); 
                var count =  $('#from option').length;
                document.getElementById("countFrom").value=count;
                var count2 =  $('#to option').length;
                document.getElementById("countTo").value=count2;
            }

            function moveSelected(from, to) {
                $('#'+from+' option:selected').remove().appendTo('#'+to); 
                var count =  $('#from option').length;
                document.getElementById("countFrom").value=count;
                var count2 =  $('#to option').length;
                document.getElementById("countTo").value=count2;
            }

            function selectAll() {
                $("select option").attr("selected","selected");
                var count =  $('#from option').length;
                document.getElementById("countFrom").value=count;
                var count2 =  $('#to option').length;
                document.getElementById("countTo").value=count2;
            }

            function getValues() {
                var hallName="";
                var tex = $( "#to option:selected" ).text(); 
                var count3 =  $('#to option').length;

                var x=document.getElementById("to");
                for (var i = 0; i < x.options.length; i++) {
                    if(x.options[i].selected ==true){
                        // alert(x.options[i].value);
                        hallName += x.options[i].value+"-";
                    }
                }
                document.getElementById("hallName").value=hallName;
                alert(hallName);
            }
        </script>
    </head>
    <body>
        <form name="selection" method="post" onSubmit="return selectAll()">
        <input type="text" name="hallName" id="hallName" onclick="show();">
        <input type="submit" name="my_form_submit_button" value="Second Click"/>
        <body onload="displayCount();"> 
            <select multiple size="10" id="from">
                <option value="hall 1">hall 1</option>
                <option value="hall 2">hall 2</option>
                <option value="hall 3">hall 3</option>
                <option value="hall 4">hall 4</option>
                <option value="hall 5">hall 5</option>
                <option value="hall 6">hall 6</option>
                <option value="hall 7">hall 7</option>
                <option value="hall 8">hall 8</option>
                <option value="hall 9">hall 9</option>
                <option value="hall 10">hall 10</option>
            </select>
            <div class="controls"> 
                <a href="javascript:moveAll('from', 'to');" onclick="return ccfromto();"  id="get">&gt;&gt;</a> 
                <a href="javascript:moveSelected('from', 'to');" onclick="return ccfromto();" id="get">&gt;</a> 
                <a href="javascript:moveSelected('to', 'from');" onclick="return ccfromto();" id="get">&lt;</a> 
                <a href="javascript:moveAll('to', 'from');" onclick="return ccfromto();" id="get">&lt;&lt;</a>
            </div>
            <select multiple id="to" size="10" name="topics[]"></select>
            <br></br>
            <br></br>
            <br></br>
            <br></br>
            <br></br>
            <br></br>
            <input type="text" name="countFrom" id="countFrom">
            <input type="text" name="countTo" id="countTo">
            <br>
            <input type="button" name="but" value="after moving first   click" id="but" onclick="return getValues();">
            </br>
        </form> 
        <?php
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "cms";

            $conn = new mysqli($servername, $username, $password, $dbname);

            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }

            $sql='select  register_number from stu_master ';
            $result = $conn->query($sql);
            $count = 0;

            echo "<table>";
            echo "<tr>";
            $name=$_POST["hallName"];
            $arr1=explode("-",$name);
            $arr_length = count($arr1);
            $countRow=0;
            for ($i = 0; $i < $arr_length-1; $i++) {
                for ($j = 1; $j <= 5; $j++) {
                    if($countRow == 1) {
                        echo "</tr><br><tr>";
                        $countRow = 0;
                    }  
                    echo "<td>".$arr1[$i]." "."Row"." ".$j."</td><br>";

                    while ($show = mysqli_fetch_array($result)) { 
                        if($count == 5) {
                            echo "</tr><br><tr>";
                            $count = 0;
                        }
                        echo "<td>".$show[register_number]."</td>"; 
                        $count++; 
                    }
                    $countRow++;
                }
            } 
            echo "</tr>";
        ?>
        </table>
    </body>
</html>

Sample Data 数据库样本数据

Current Output 电流输出

Required Output 所需输出

Somehow i managed and brought the required output. I will post php part alone so it will be useful for someone's

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cms";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql='select  register_number from stu_master';
$result = $conn->query($sql);
$count = 0;
echo "<table>";
echo "<tr>";
$name=$_POST["hallName"];
$arr1=explode("-",$name);
$arr_length = count($arr1);


 $countRow=0;

 $show=array();
   while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
     $show[]= $row[register_number];
   }
$size=count($show);

echo $size;

$var=0;

 for ($i = 0; $i < $arr_length-1; $i++)
{
for ($j = 1; $j <= 5; $j++) 
{

  if($countRow == 1)
    {
      echo "</tr><br><tr>";
      $countRow = 0;
    }

     echo "<td>".$arr1[$i]." "."Row"." ".$j."</td><br>";


       echo "<td>".$show[$var+0]."</td>";
       echo "<td>".$show[$var+1]."</td>";
       echo "<td>".$show[$var+2]."</td>";
       echo "<td>".$show[$var+3]."</td>";
       echo "<td>".$show[$var+4]."</td>";

 $countRow++;
 $var=$var+5;
}

} 
echo "</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