如何使用php基于条件复制数组

时间:2016-03-02 02:31:23

标签: php arrays

我有一个数组,从数据库中选择staff_id并在表格中随机显示它们。

但是我想基于staff_level复制数组,这样初级员工就会被复制,而高级员工则不会重复。

这是为了让初级员工出现两倍于高级职员。这是代码:

foreach ($invigilator_array_indexes as $array_index) {
            if ($row['level'] == 1){
                $invi_array = $invigilators[$array_index]['staffid_pdo'].', ';
                $output .=$invi_array;
                $output .=$invi_array.concat();
            }else {
                $invi_array = $invigilators[$array_index]['staffid_pdo'].', ';
                $output .=$invi_array;
            }

    }

请注意:

foreach ($invigilator_array_indexes as $array_index) {
                $invi_array = $invigilators[$array_index]['staffid_pdo'].', ';
                $output .=$invi_array;
    }

最初以随机顺序输出staff_id

欢迎所有帮助,我们将不胜感激。感谢您的期待

完整代码:

<?php
$page = isset($_GET['page']) ? $_GET['page'] : 1;


$records_per_page = 50;

$from_record_num = ($records_per_page * $page) - $records_per_page;

include_once 'includes/config.php';
include_once 'includes/data.inc_course.php';
include 'includes/shuffle2.php';


$database = new Config();
$db = $database->getConnection();

$product = new Data($db);

$stmt = $product->readAll($page, $from_record_num, $records_per_page);
$num = $stmt->rowCount();

//range declaration
$range = $_POST['range'];

?>



<?php include('includes/header.php'); 



$output1 = "

    <body>
        <div id='wrapper'>";

        echo $output1;

       include('includes/nav.php');

$output2 = "
       <!-- Page Content -->
        <div id='page-wrapper'>
            <div class='container-fluid'>
                <div class='row'>
                    <div class='col-lg-12'>
                        <h1 class='page-header'>Timetable Details</h1>
                    </div>
                    <!-- /.col-lg-12 -->

                    <!-- niyicode -->
                <div class='col-lg-12'>
                    <p>
                        <a class='btn btn-primary' href='courses/add.php' role='button'>Add Course</a>
                        <a class='btn btn-primary fleft' href='staff/add.php' role='button'>Add Staff</a>
                    </p>";
        echo $output2;

        if($num>0){
            if($range>0){


            $query1 = "SELECT staffid_pdo,level FROM staff_list WHERE avail_pdo = 1 ORDER BY id_pdo ASC ";



            $invi_stmt = $db->prepare($query1);
            $invi_stmt->execute();
            $invigilators = $invi_stmt->fetchAll(PDO::FETCH_ASSOC);

            //generate an array containing the range of keys in the invigilators resultset we got above
            $invigilators_array_range = range(0, (count($invigilators) - 1));

$output =  "           
        <table class= 'table table-bordered table-hover table-striped js-serial' id='printTable'>
            <caption>Timetable with number of students</caption>
            <thead>
                <tr class='success'>
                    <th style='width: 4%'>#</th>
                    <th>No. Students</th>
                    <th>Code</th>
                    <th>Date</th>
                    <th>Start Time</th>
                    <th>End Time</th>
                    <th>Venue</th>
                    <th>Duration</th>
                    <th>Invigilator</th>
                    <!-- <th>Edit/Del</th> -->
                </tr>
            </thead>
        <tbody>

";
        // echo $output;


while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    extract($row);


    $invigilators_needed = round(($row['snb_pdo'] / $range), 0, PHP_ROUND_HALF_EVEN);
    print_r($invigilators_needed);
    $date_active = $row['dat_pdo'];

    $output .= ' <tr>' ;
    $output .= '' ;
    $output .= '   <td>'.$row['snb_pdo'].'</td>' ;
    $output .= '   <td>'.$row['coscd_pdo'].'</td>' ;
    $output .= '   <td>'.$date_active.'</td>' ;
    $output .= '   <td>'.$row['starttim_pdo'].'</td>' ;
    $output .= '   <td>'.$row['endtim_pdo'].'</td>' ;
    $output .= '   <td>'.$row['ven_pdo'].'</td>' ;
    $output .= '   <td>'.$row['dur_pdo'].'</td>' ;
    $output .= '   <td>';

    $invigilator_array_indexes = array_rand($invigilators_array_range, $invigilators_needed);

    if (count($invigilator_array_indexes)) {
        foreach ($invigilator_array_indexes as $array_index) {
                if ($row['level'] == 1){
                    $invi_array = $invigilators[$array_index]['staffid_pdo'].', ';
                    $output .=$invi_array;
                    $output .=$invi_array.clone();
                }else {
                    $invi_array = $invigilators[$array_index]['staffid_pdo'].', ';
                    $output .=$invi_array;
                }

                print_r($invi_array);

        }

    } else $output .= "
        <div class='alert alert-danger' role='alert'>
          <button type='button' class='close' data-dismiss='alert' aria-label='Close'></button>
           Not enough <strong>invigilators!</strong>
        </div>
        ";
    $output .= '</td>' ;
    // Add more here if appropriate .... ;
    $output .= ' </tr>' ;
?>
                    <?php
// }
}
$output .='      </tbody>
    </table>    
';
$results->free();
mysqli_close($mysqli);
echo $output;


$page_dom = "index.php";
include_once 'includes/pagination.inc_course.php';
?>

<center><button onclick="printData();" class="btn btn-primary hideprint"> Print </button></center>
<br/>

<?php
//range ending
    } else {
        ?>
                    <div class="alert alert-warning alert-dismissible" role="alert">
                      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                      <strong>Warning!</strong> No valid range entered. Please enter a valid range <a href="staff/range.php"> here! </a>
                    </div>
                    <?php
                    }
                    ?>
<?php
//if num ending
}
else{
?>
                    <div class="alert alert-warning alert-dismissible" role="alert">
                      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                      <strong>Warning!</strong> No Data. Please add data to view timetable
                    </div>
                    <?php
                    }
                    ?>
<?php   
$output5 = "                       </div>
                </div>
                <!-- /.row -->
            </div>
            <!-- /.container-fluid -->
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->
    ";
        echo $output5;


?>
<!-- echo $output;                     -->
    <?php include('includes/footer.php'); ?>

0 个答案:

没有答案