Php head-scratcher


  • I survived the hour long Uno hand

     So I've been puzzling over this for a few hours today, and off and on as I worked on other areas yesterday. The following code is matching exactly one listing per directory per sort view, with new characters being matched in place of old (yesterday Jennifer showed up under first-name sort H-M, today it's Jill, who was added about ten minutes ago). DB name redacted because doing silly censorship makes me feel safer:

     

        $cxn = mysqli_connect($host,$user,$password,"DBname") or die ("Could not connect to database");
        $query = "SELECT FName, LName, charID FROM Chars ";
        switch (sort){
            case 1:
                $query .= "ORDER BY FName";
                break;
            case 2:
                $query .= "ORDER BY LName";
                break;
        }
        $result = mysqli_query($cxn,$query) or die ("Error:" . mysqli_error($cxn));
        $matches="0";
        while ($row = mysqli_fetch_assoc($result)){
            $found=false;
            $name = "";
            $row['FName'] = trim($row['FName']);
            $row['LName'] = trim($row['LName']);
            switch ($dir){
                case 1:
                    if ($sort==1 && ereg("^[A-Ga-g]",$row['FName'])) {
                        $name = $row['FName'] . " " . $row['LName'];
                         $found=true;
                    }
                    if ($sort==2 && ereg("^[A-Ga-g]",$row['LName'])) {
                        $name = $row['LName'] . ", " . $row['FName'];
                        $found=true;
                    }
                    break;
                case 2:
                    if ($sort==1 && ereg("^[H-Mh-m]",$row['FName'])) {
                        $name = $row['FName'] . " " . $row['LName'];
                        $found=true;
                    }
                    if ($sort==2 && ereg("^[H-Mh-m]",$row['LName'])) {
                        $name = $row['LName'] . ", " . $row['FName'];
                        $found=true;
                    }
                    break;
                case 3:
                    if ($sort==1 && ereg("^[N-Sn-s]",$row['FName'])) {
                        $name = $row['FName'] . " " . $row['LName'];
                        $found=true;
                    }
                    if ($sort==2 && ereg("^[N-Sn-s]",$row['LName'])) {
                        $name = $row['LName'] . ", " . $row['FName'];
                        $found=true;
                    }
                    break;
                case 4:
                    if ($sort==1 && ereg("^[T-Zt-z]",$row['FName'])) {
                        $name = $row['FName'] . " " . $row['LName'];
                        $found=true;
                    }
                    if ($sort==2 && ereg("^[T-Zt-z]",$row['LName'])) {
                        $name = $row['LName'] . ", " . $row['FName'];
                        $found=true;
                    }
                    break;

            }
            if ($found) {
                $template->setCurrentBlock("LISTING");
                $template->setVariable("NAME", $name);
                $template->setVariable("ID", $row['charID']);
                $template->parseCurrentBlock;
                $matches++;
            }
        } //end of while

     

     

    Any thoughts?


  • I survived the hour long Uno hand

    Nevermind. Hours and hours later, I figured it out. I'd forgotten a layer of nesting - listing was in a second file that was being added, so it was nested; parsing the parent block fixed the issue. 



  •  switch (sort){

     

    Did you copypaste?

    Because there's a $ missing there.


  • I survived the hour long Uno hand

     Yeah, saw that too, once I got the listing working and noticed they were out of order >.< thanks



  •  cheers. :)


Log in to reply