如何使用表单过滤mysql / php表数据

时间:2016-11-05 09:56:04

标签: php mysql sql loops

以下是我正在做的事情的背景:

  1. PHP正在查询mySql表,以在网站上显示NFL数据表。
  2. 我想过滤掉那些没有检查过球队的球员
    • 见下图,仅图片中属于BAL,CIN和DEN的玩家将在表格中显示。
  3. 但我一次只能展示1支队伍
    • 如果我在下拉列表中检查了BAL,CIN,DEN,那么我只会与其中一个团队玩家有一个数据表。
  4. 有人可以帮帮我吗?

    
    
    <?php /*
    Template Name: TestPHPFilter}
    */ ?>
    
    <?php get_header(); ?>
    
    <?php genesis_before_content_sidebar_wrap(); ?>
    <div id="content-sidebar-wrap">
    
    <?php genesis_before_content(); ?>
    	<div class='statsheader'>
    	<div class='statstitlelng'>
    	<h2><?php _e('DFS Trends'); ?></h2></div><div id="dpapos" class='statstitle'><h2>WRs</h2></div>
    	
    		<div class='statsfddk'>
    	</div>
    	
    	<div class='statsform'>
    	   				<form action="<?php bloginfo('url'); ?>" method="get">
    	   				<?php  
    	   					$args = array(
    						    
    						    'include' => '1822, 1825, 1827, 1829'					    
    						);
    						wp_dropdown_pages($args); ?>
       				 
    	   				<input type="submit" name="submit" value="view" />
    	  				 </form>
    					 <script type="text/javascript">
                                        jQuery(function($) {
    						                $('#teammultiselect').multiselect();
                                        });
                                    </script>
            <form action="<?php get_permalink('1839'); ?>" method="POST">
       
                <select id="teammultiselect" name="start" multiple="multiple">
                    <option value="">Team</option>
    						<option value="ARI" <?php if($start == 'ARI'){echo 'selected';}?>>ARI</option>
    						<option value="ATL" <?php if($start == 'ATL'){echo 'selected';}?>>ATL</option>
    						<option value="BAL" <?php if($start == 'BAL'){echo 'selected';}?>>BAL</option>
    						<option value="BUF" <?php if($start == 'BUF'){echo 'selected';}?>>BUF</option>
    						<option value="CAR" <?php if($start == 'CAR'){echo 'selected';}?>>CAR</option>
    						<option value="CHI" <?php if($start == 'CHI'){echo 'selected';}?>>CHI</option>
    						<option value="CIN" <?php if($start == 'CIN'){echo 'selected';}?>>CIN</option>
    						<option value="CLE" <?php if($start == 'CLE'){echo 'selected';}?>>CLE</option>
    						<option value="DAL" <?php if($start == 'DAL'){echo 'selected';}?>>DAL</option>
    						<option value="DEN" <?php if($start == 'DEN'){echo 'selected';}?>>DEN</option>
    						<option value="DET" <?php if($start == 'DET'){echo 'selected';}?>>DET</option>
    						<option value="GB" <?php if($start == 'GB'){echo 'selected';}?>>GB</option>
    						<option value="HOU" <?php if($start == 'HOU'){echo 'selected';}?>>HOU</option>
    						<option value="IND" <?php if($start == 'IND'){echo 'selected';}?>>IND</option>
    						<option value="JAX" <?php if($start == 'JAX'){echo 'selected';}?>>JAX</option>
    						<option value="KC" <?php if($start == 'KC'){echo 'selected';}?>>KC</option>
    						<option value="LA" <?php if($start == 'LA'){echo 'selected';}?>>LA</option>
    						<option value="MIA" <?php if($start == 'MIA'){echo 'selected';}?>>MIA</option>
    						<option value="MIN" <?php if($start == 'MIN'){echo 'selected';}?>>MIN</option>
    						<option value="NE" <?php if($start == 'NE'){echo 'selected';}?>>NE</option>
    						<option value="NO" <?php if($start == 'NO'){echo 'selected';}?>>NO</option>
    						<option value="NYG" <?php if($start == 'NYG'){echo 'selected';}?>>NYG</option>
    						<option value="NYJ" <?php if($start == 'NYJ'){echo 'selected';}?>>NYJ</option>
    						<option value="OAK" <?php if($start == 'OAK'){echo 'selected';}?>>OAK</option>
    						<option value="PHI" <?php if($start == 'PHI'){echo 'selected';}?>>PHI</option>
    						<option value="PIT" <?php if($start == 'PIT'){echo 'selected';}?>>PIT</option>
    						<option value="SD" <?php if($start == 'SD'){echo 'selected';}?>>SD</option>
    						<option value="SEA" <?php if($start == 'SEA'){echo 'selected';}?>>SEA</option>
    						<option value="SF" <?php if($start == 'SF'){echo 'selected';}?>>SF</option>
    						<option value="TB" <?php if($start == 'TB'){echo 'selected';}?>>TB</option>
    						<option value="TEN" <?php if($start == 'TEN'){echo 'selected';}?>>TEN</option>
    						<option value="WAS" <?php if($start == 'WAS'){echo 'selected';}?>>WAS</option>
                </select>
                
                <input type="submit" name="search" value="Find">
    					 </div></div>
    					 </form>
        
    <?php
    
    $dsn = 'mysql:host=localhost;dbname=press_10';
    $username = 'xxx';
    $password = 'yyy';
    
    try{
        
        $con = new PDO($dsn, $username, $password);
        $con->setAttribute(PO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
    } catch (Exception $ex) {
    
        echo 'Not Connected '.$ex->getMessage();
    }
    
    $tableContent = '';
    $start = '';
    $selectStmt = $con->prepare('SELECT * FROM WR_TeamAnalyzer');
    $selectStmt->execute();
    $rows = $selectStmt->fetchAll();
    
    foreach ($rows as $row)
    {
        $tableContent = $tableContent.'<tr>'.
                 '<td>' . $row['WR'] .'</td>'
    			.'<td>' . $row['Team'] . '</td>'
    			.'<td>' . $row['WK'] . '</td>'
    			.'<td>' . $row['Opp'] . '</td>'
    			.'<td>' . $row['T'] . '</td>'
    			.'<td>' . $row['Yds'] . '</td>'
    			.'<td>' . $row['TDs'] . '</td>'
    			.'<td>' . $row['Fpts'] . '</td>';
    }
    
    if(isset($_POST['search']))
    {
    $start = $_POST['start'];
    $tableContent = '';
    $selectStmt = $con->prepare('SELECT * FROM WR_TeamAnalyzer WHERE TEAM like :start');
    $selectStmt->execute(array(
            
             ':start'=>$start.'%'
        
    ));
    $rows = $selectStmt->fetchAll();
    
    foreach ($rows as $row)
    {
        $tableContent = $tableContent.'<tr>'.
                 '<td>' . $row['WR'] .'</td>'
    			.'<td>' . $row['Team'] . '</td>'
    			.'<td>' . $row['WK'] . '</td>'
    			.'<td>' . $row['Opp'] . '</td>'
    			.'<td>' . $row['T'] . '</td>'
    			.'<td>' . $row['Yds'] . '</td>'
    			.'<td>' . $row['TDs'] . '</td>'
    			.'<td>' . $row['Fpts'] . '</td>';
    }
        
    }
    
    ?>
    
      
                
               <div class='wpDataTablesWrapper'><table id='trend' class='wpDataTable dataTable'><thead><tr><th>WR</th><th>Tm</th><th>Wk</th><th>Opp</th><th>T</th><th>Yds</th><th>TDs</th><th>Fpts</th></tr></thead>
                    
                    <?php
                    
                    echo $tableContent;
                    
                    ?>
                    
                </table>
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:1)

我建议将选择标记名称更改为www.example.com/about/,以便您可以使用.htaccess在数组中获取这些值。

然后,据我了解你的问题,你想要显示未被选中的球队的球员,所以我们需要将查询结构化为:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

因此,为了与您的方法保持一致,我们将使用for:

创建一个查询字符串和参数数组
name="start[]"

然后运行:

$_POST['start']

这应该有用。

我还建议您阅读有关MVC架构的内容,它在开发Web应用程序时会有很大帮助。有许多教程和方法可以很好地解释这一点,this是一个很好的PHP设计。

来源:

multiple select