开发者

PHP: 2 switches don't interact well with each other

开发者 https://www.devze.com 2023-02-13 02:17 出处:网络
I can\'t make both switches work, it\'s either the one or the other. On the page list.php, there\'s a table which gets all members from \"T_Leden\". If a user clicks on any of the links, it sorts by i

I can't make both switches work, it's either the one or the other. On the page list.php, there's a table which gets all members from "T_Leden". If a user clicks on any of the links, it sorts by it (id, name, address, etc.)

It was sorting alright until I added a new switch to show only men/women/etc. Now, I can show only men and only women, but I can't sort anymore.

I'm clueless.

This is my SQL:

-- phpMyAdmin SQL Dump
-- version 3.3.9
-- http://www.phpmyadmin.net
--
-- Machine: localhost
-- Genereertijd: 23 Feb 2011 om 10:58
-- Serverversie: 5.5.8
-- PHP-Versie: 5.3.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `db_school`
--

-- --------------------------------------------------------

--
-- Tabelstructuur voor tabel `t_leden`
--

CREATE TABLE IF NOT EXISTS `t_leden` (
  `D_index` int(11) NOT NULL AUTO_INCREMENT,
  `D_Naam` varchar(255) NOT NULL,
  `D_Voornaam` varchar(255) NOT NULL,
  `D_Adres` varchar(255) NOT NULL,
  `D_Peter` varchar(255) NOT NULL,
  `D_Lid` varchar(255) NOT NULL,
  `D_Geslacht` enum('Man','Vrouw') NOT NULL,
  `D_Betaald` date NOT NULL,
  `D_Gdatum` date NOT NULL,
  `D_Zichtbaar` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`D_index`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

--
-- Gegevens worden uitgevoerd voor tabel `t_leden`
--

INSERT INTO `t_leden` (`D_index`, `D_Naam`, `D_Voornaam`, `D_Adres`, `D_Peter`, `D_Lid`, `D_Geslacht`, `D_Betaald`, `D_Gdatum`, `D_Zichtbaar`) VALUES
(2, 'Ed-Damgha', 'Shakira', 'Wilrijk', 'Vlad', 'Gewoon lid', 'Vrouw', '2011-02-15', '1992-05-19', 1),
(1, 'Polianskii', 'Vlad', 'Antwerpen', 'Tuplad', 'Bestuurslid', 'Man', '2011-02-01', '1990-08-04', 1),
(3, 'Sandru', 'Raoul', 'Hoboken', 'Kevin', 'Gewoon lid', 'Man', '2011-02-11', '1990-07-18', 1);

This is my PHP:

<?php include_once("config.inc.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Ledenlijst</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="generator" content="Geany 0.20" />
</head>

<body>
<h1 style="text-align: center">Ledenlijst</h1>
<br /><? echo $menu ?><br />
<p style="text-align:center">
<select name="sorteer_methode" onchange="location.href='list.php?toon='+this.options[this.selectedIndex].value"> 
 <option>------------</option>
 <option value="mannen">Mannen</option> 
 <option value="vrouwen">Vrouwen</option>
 <option value="bestuur">Bestuursleden</option>
</select></p>
<table align="center" border="1">
<tr>
<td><b><a href="list.php?sortby=index">ID</a></b></td>
<td><b><a href="list.php?sortby=naam">Naam</a></b></td>
<td><b><a href="list.php?sortby=voornaam">Voornaam</a></b></td>
<td><b><a href="list.php?sortby=adres">Adres</a></b></td>
<td><b><a href="list.php?sortby=peter">Peter</a></b></td>
<td><b><a href="list.php?sortby=lid">Soort lid</a></b></td>
<td><b><a href="list.php?sortby=betaald">Laatst betaald</a></b></td>
<td><b><a href="list.php?sortby=gdatum">Geboortedatum</a></b></td>
<td></td>
</tr>
<?php
    /* Een switch om sorteren makkelijk te maken */
switch(isset($_GET['sortby']) ? $_GET['sortby'] : (isset($_GET['toon']) ? $_GET['toon'] : null)) {
    case 'name':
        $result=mysql_query("SELECT D_index,D_Naam,D_Voornaam,D_Geslacht,D_Adres,D_Peter,D_Lid,D_Betaald,D_Gdatum
        FROM T_Leden
        WHERE D_Zichtbaar = '1'
        ORDER BY D_".$_GET['sortby']) or die(mysql_error());
        break;
    /* Een switch om enkel vrouwen, mannen of bestuursleden te laten zien */
    case 'mannen':
        $result=mysql_query("SELECT D_index,D_Naam,D_Voornaam,D_Geslacht开发者_开发百科,D_Adres,D_Peter,D_Lid,D_Betaald,D_Gdatum
        FROM T_Leden
        WHERE D_Geslacht = 'Man'
        AND D_Zichtbaar = '1'
        ORDER BY D_index") or die(mysql_error());
        break;
    case 'vrouwen':
        $result=mysql_query("SELECT D_index,D_Naam,D_Voornaam,D_Geslacht,D_Adres,D_Peter,D_Lid,D_Betaald,D_Gdatum
        FROM T_Leden
        WHERE D_Zichtbaar = '1'
        AND D_Geslacht = 'Vrouw'
        ORDER BY D_index") or die(mysql_error());
        break;
    case 'bestuur':
        $result=mysql_query("SELECT D_index,D_Naam,D_Voornaam,D_Geslacht,D_Adres,D_Peter,D_Lid,D_Betaald,D_Gdatum
        FROM T_Leden
        WHERE D_Zichtbaar = '1'
        AND D_Lid = 'Bestuurslid'
        ORDER BY D_index") or die(mysql_error());
        break;
    default:
        $result=mysql_query("SELECT D_index,D_Naam,D_Voornaam,D_Geslacht,D_Adres,D_Peter,D_Lid,D_Betaald,D_Gdatum
        FROM T_Leden
        WHERE D_Zichtbaar = '1'
        ORDER BY D_index") or die(mysql_error());
    }
    /* Een loop om leden te tonen */
while($row=mysql_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>".htmlspecialchars($row['D_index'])."</td>";
        echo "<td>".htmlspecialchars($row['D_Naam'])."</td>";
        echo "<td>".htmlspecialchars($row['D_Voornaam'])."</td>";
        echo "<td>".htmlspecialchars($row['D_Adres'])."</td>";
        echo "<td>".htmlspecialchars($row['D_Peter'])."</td>";
        echo "<td>".htmlspecialchars($row['D_Lid'])."</td>";
        echo "<td>".htmlspecialchars($row['D_Betaald'])."</td>";
        echo "<td>".htmlspecialchars($row['D_Gdatum'])."</td>";
        echo "<td><a href=del.php?del=".htmlspecialchars($row['D_index']).">Verwijder</a></td>";
        echo "</tr>";
        }
?>
</table>
</body>
</html>
<?php
mysql_close($connect);
?>


First mistake:
'naam'

<td><b><a href="list.php?sortby=naam">Naam</a></b></td>

vs. 'name'

switch(...) {
    case 'name':


check you logic in the ternary conditional (nested ones give unexpected results).

maybe have a look at refctoring to get rid of the switch all together - use prepared statements and bind values based on your required logic.(see PDO class in php.net)


Lots of mistakes. But here are some ideas:

  • On the HTML part, when the gender changes submit as well the current sort status in parameters (so for example hide the current sort status in an hidden).
  • same thing for the gender filter, do not forget to send it on the 'sorter by' url
  • maybe you could use some js code to catch actual values and rebuild the query parameters
  • On the PHP side, start to build the $sql query outside of the switch, and to finish and lauch it after the end of the witch. In the switch add conditions in a $orderby array. Now add a check on the gender status and if you need to add a condition add it on a $where. Just before building the final query explode the $where and $orderby in SQL conditions.
0

精彩评论

暂无评论...
验证码 换一张
取 消