About to do one large update converting my site from pure html to php and have a few questions.

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by chrisrog, Dec 13, 2013.

  1. chrisrog

    chrisrog Member

    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    6
    1. What functions and setup to you recommend for an admin login feature and user login feature?
    2. Trying to figure out how to connect my java applets and other web applications to my DB but unsure how to do this on x10hosting.
    3. We all know my CSS and Design Skills are horrid so any suggestions on a good setup to the page an its CSS to make it look nice?
    4. Working on a php Gallery that is connected to a DB. So far it is working fine locally but not sure how to change it up to work on my live site here. (Will upload code when able to show it)
    5. What would you all recommend putting into php classes? Right now I got a general idea for things like my gallery and DB connection but any ideas on what else.
    6. Going to be working with the MySQL DB a lot here and forgot some of what we need to be using in the PDO statement that connects to it. Host being the main one? (I have a connection class working just fine at home but not sure if it'll work here when targets are changed)
    I got other questions but these should do for now at least. Also I'll be uploading some code files for a few things to see what I need to fix up in them if anyone wants to help on that matter. :) The main likely trouble makers would be the gallery i'm working on and my DBConnection class.

    Here's the DBConnection.php file.
    PHP:
    <?php
    //$connection = createconnection('localhost','namecatch','root','');
    //$connection = createconnection('localhost','cluboops','root','');
    //some connections I'm using for other things at home.
    function createconnection($host$dbname$user$pass) {
        try {
            
    $connectiontodb = new DBConnection("mysql:host=$host;dbname=$dbname""$user""$pass"); 
            return 
    $connectiontodb;
        } catch (
    Exception $ex) {
            
    $error_msg "Unknown Error in dbConnection.php! Report this! \n\n" $except->getMessage();
            include 
    'errorDisplayer.php';
            exit();
        }
    }

    /**
    * Description of DBConnection
    *
    * @[USER=182410]Author[/USER] crogers
    */
    class DBConnection {

        private 
    $dsn
        private 
    $username;
        private 
    $password
        private 
    $db

        function 
    __construct($dsninput$usernameinput$passwordinput) {
            
    $this->dsn $dsninput;
            
    $this->username $usernameinput;
            
    $this->password $passwordinput;
            try {
                
    $this->db = new PDO($this->dsn$this->username$this->password);
            } catch (
    PDOException $excep) {
                echo 
    "Something broke and I got to fix it :( : " $excep->getMessage();
                exit();
            }
        }
        function 
    __destruct() {
            
    $this->dsn null;
            
    $this->username null;
            
    $this->password null;
            
    $this->db null;
        }

        public function 
    getDB() {
            return 
    $this->db;
        }
        function 
    selectFromAny($fromtargets$selecttargets) {
            
    $querystatement "SELECT $selecttargets FROM $fromtargets";
            
    $tableinfo $this->db->prepare($querystatement); /* $connection->getDB() */
            
    $tableinfo->setFetchMode(PDO::FETCH_ASSOC);
            
    $tableinfo->execute();

            return 
    $tableinfo;
        }
        function 
    selectAny($fromtargets$selecttargets$orderbytargets) {
            
    $querystatement "SELECT $selecttargets FROM $fromtargets ORDER BY $orderbytargets";
            
    $tableinfo $this->db->prepare($querystatement); 
            
    $tableinfo->setFetchMode(PDO::FETCH_ASSOC);
            
    $tableinfo->execute();

            return 
    $tableinfo;
        }
        function 
    selectAnyWhere($fromtargets$selecttargets$wherecoltargets) {
            
    $querystatement "SELECT $selecttargets FROM $fromtargets WHERE $wherecoltargets";
            
    $tableinfo $this->db->prepare($querystatement);
            
    $tableinfo->setFetchMode(PDO::FETCH_ASSOC);
            
    $tableinfo->execute();
            return 
    $tableinfo;
        }
        function 
    selectAnyWhereOrder($fromtargets$selecttargets$wherecoltargets$orderbytargets) {
            
    $querystatement "SELECT $selecttargets FROM $fromtargets WHERE $wherecoltargets ORDER BY $orderbytargets";
            
    $tableinfo $this->db->prepare($querystatement);
            
    $tableinfo->setFetchMode(PDO::FETCH_ASSOC);
            
    $tableinfo->execute();

            return 
    $tableinfo;
        }
        function 
    updateOn($updatedtablename$arrayofcolnames$arrayofcoldata$wheretargets) {
            
    $querystatement "UPDATE $updatedtablename SET";
            
    $colcount1 count($arrayofcolnames);
            
    $setlist "";
            for (
    $row 0$row $colcount1$row++) {
                if (!
    is_numeric($arrayofcoldata[$row])) {
                    if (
    $row $colcount1 1) {
                        
    $setlist .= $arrayofcolnames[$row] = '$arrayofcoldata[$row]', ";
                    } else {
                        
    $setlist .= $arrayofcolnames[$row] = '$arrayofcoldata[$row]' ";
                    }
                } else {
                    if (
    $row $colcount1 1) {
                        
    $setlist .= $arrayofcolnames[$row] = $arrayofcoldata[$row], ";
                    } else {
                        
    $setlist .= $arrayofcolnames[$row] = $arrayofcoldata[$row] ";
                    }
                }
            }
            
    $querystatement .= $setlist "WHERE $wheretargets";
            
    $tableinfo $this->db->prepare($querystatement);
            
    #$tableinfo->setFetchMode(PDO::FETCH_ASSOC);
            
    $tableinfo->execute();
            return 
    $tableinfo;
        }
        function 
    deleteOn($targettable$wheretargets) {
            
    $querystatement "DELETE FROM $targettable WHERE $wheretargets";
            echo 
    $querystatement;
            
    $tableinfo $this->db->prepare($querystatement);
            
    #$tableinfo->setFetchMode(PDO::FETCH_ASSOC);
            
    $tableinfo->execute();
            return 
    $tableinfo;
        }
        function 
    insertItems($tablename$stringofcolnames$arrayofdatainput) {
            
    $colcount1 count($arrayofdatainput);
            
    $setlist "";
            for (
    $row 0$row $colcount1$row++) {
                if (!
    is_numeric($arrayofdatainput[$row])) {
                    if (
    $row $colcount1 1) {
                        
    $setlist .= "'$arrayofdatainput[$row]', ";
                    } else {
                        
    $setlist .= "'$arrayofdatainput[$row]' ";
                    }
                } else {
                    if (
    $row $colcount1 1) {
                        
    $setlist .= "$arrayofdatainput[$row], ";
                    } else {
                        
    $setlist .= "$arrayofdatainput[$row] ";
                    }
                }
            }
            echo 
    "<br />";
            
    $querystatement "INSERT INTO $tablename($stringofcolnames) VALUES($setlist)";
            echo 
    $querystatement;
            
    $tableinfo $this->db->prepare($querystatement);
            
    #$tableinfo->setFetchMode(PDO::FETCH_ASSOC);
            
    $tableinfo->execute();

            return 
    $tableinfo;
        }
    }
    Even with CIT classes I still need to work on my code formatting and a few other issues. XD
     
    Last edited: Dec 13, 2013
  2. chrisrog

    chrisrog Member

    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    6
    and here's the GalleryClass.php file
    PHP:
    <?php
    require_once 'DBConnection.php';
    class 
    MediaGallery {
        private 
    $mediaType$mediaCatagory$mediapageslot$listFsize$listTsize$listCsize;
        private 
    $filearraylist$categorylist$typelist;
        private 
    $connectionmedia;
        public function 
    __construct(DBConnection $connection) {
            
    $this->connectionmedia $connection;
            if (isset(
    $_GET['listSlot'])) {
                
    $this->mediapageslot $_GET['listSlot'];
            } else {
                
    $this->mediapageslot 0;
            }
            if (isset(
    $_GET['listType'])) {
                
    $this->mediaType $_GET['listType'];
            } else {
                
    $this->mediaType 1;
            }


            if (isset(
    $_GET['listCate'])) {
                
    $this->mediaCatagory $_GET['listCate'];
            } else {
                
    $this->mediaCatagory 1;
                
    $this->mediaType 1;
                
    $this->mediapageslot 0;
            }
            
    $this->filearraylist $this->LoadFilesFromDB();
            
    $this->categorylist $this->LoadCategoriesFromDB();
            
    $this->typelist $this->LoadDirectoriesFromDB();
            
    $this->listFsize $this->LoadFilesCount();
            
    $this->listTsize $this->LoadDirectoriesCount();
            
    $this->listCsize $this->LoadCategoriesCount();
        }

        public function 
    LoadFilesFromDB() {
            
    $filelist $this->connectionmedia->selectAnyWhere('mediafilelist''*'" MediaCatagory = $this->mediaCatagory AND MediaType = $this->mediaType");
            
    $filearray = array();
            foreach (
    $filelist as $filename) {
                if (
    $filename['IsOnSite'] < 1) {
                    
    array_push($filearray$filename['FilePath']);
                } else {
                    
    array_push($filearray$filename['SitePath']);
                }
            }


            return 
    $filearray;
        }

        public function 
    LoadFilesCount() {
            
    $number count($this->filearraylist);
            return 
    $number;
        }

        public function 
    LoadCategoriesFromDB() {
            
    $filelist $this->connectionmedia->selectAnyWhere('mediacatagorylist''*'"MediaCatagoryID = $this->mediaCatagory");
            return 
    $filelist;
        }

        public function 
    LoadCategoriesCount() {
            
    $filelist $this->connectionmedia->selectAny('mediacatagorylist''COUNT(*) AS TypeCount'"MediaCatagoryID");
            
    $number 0;
            foreach (
    $filelist as $num) {
                
    $number $num['TypeCount'];
            }
            return 
    $number;
        }

        public function 
    LoadDirectoriesFromDB() {
            
    $filelist $this->connectionmedia->selectAnyWhere('mediatypelist''*'"MediaTypeID = $this->mediaType");
            return 
    $filelist;
        }

        public function 
    LoadDirectoriesCount() {
            
    $filelist $this->connectionmedia->selectAny('mediatypelist''COUNT(*) AS TypeCount'"MediaTypeID");
            
    $number 0;
            foreach (
    $filelist as $num) {
                
    $number $num['TypeCount'];
            }
            return 
    $number;
        }

        public function 
    ShowSingleFile() {
            
    $mainstring "";
            
    $tables $this->filearraylist;
            if (
    count($tables) > 0) {
                
    $mainstring .= $tables[$this->mediapageslot];
                return 
    $mainstring;
            } else {
                return 
    "No Files to Display in this Category!";
            }
        }

        public function 
    ShowSingleCate() {
            
    $mainstring "";
            
    $tables $this->LoadCategoriesFromDB();
            foreach (
    $tables as $row) {
                
    $mainstring .= $row['MediaCatagoryName'];
            }
            return 
    $mainstring;
        }

        public function 
    ShowSingleType() {
            
    $mainstring "";
            
    $tables $this->LoadDirectoriesFromDB();
            foreach (
    $tables as $row) {
                
    $mainstring .= $row['MediaTypeName'];
            }
            return 
    $mainstring;
        }

        public function 
    BuildCategorySelection() {
            
    $mainstring "<div>";
            
    $decreasing $this->combineE($this->mediaCatagory$this->listCsize, (-1));
            
    $mainstring .= "<a href=\"?listSlot=0&listType=1&listCate=$decreasing\" name=\"BackACategory\" class=\"Back\">Back A Category</a>";
            
    $mainstring .= "<h1>".$this->ShowSingleCate()."</h1>";
            
    $increasing $this->combineE($this->mediaCatagory$this->listCsize1);
            
    $mainstring .= "<a href=\"?listSlot=0&listType=1&listCate=$increasing\" name=\"ForwardACategory\" class=\"Forward\">Forward A Category</a>";
            return 
    $mainstring "</div>";
        }

        public function 
    BuildTypeSelection() {
            
    $mainstring "<div>";
            
    $decreasing $this->combineE($this->mediaType$this->listTsize, (-1));
            
    $mainstring .= "<a href=\"?listSlot=0&listType=$decreasing&listCate=$this->mediaCatagory\" name=\"BackAType\" class=\"Back\">Back A Type</a>";
            
    $mainstring .= "<h1>".$this->ShowSingleType()."</h1>";
            
    $increasing $this->combineE($this->mediaType$this->listTsize1);
            
    $mainstring .= "<a href=\"?listSlot=0&listType=$increasing&listCate=$this->mediaCatagory\" name=\"ForwardAType\" class=\"Forward\">Forward A Type</a>";
            return 
    $mainstring "</div>";
        }
        public function 
    BuildGallery() {
            
    $mainstring "";
            
    $mainstring .= "<div>";
            
    $mainstring .= $this->BuildCategorySelection();
            
    $mainstring .= $this->BuildTypeSelection();
            
    $mainstring .= "<div class=\"clearer\"></div>";
            
    $decreasing $this->combineE2($this->mediapageslot$this->listFsize, (-1));
            
    $mainstring .= "<a href=\"?listSlot=$decreasing&listType=$this->mediaType&listCate=$this->mediaCatagory\" name=\"BackAFile\" class=\"Back\">Back A File</a>";
            
    $mainstring .= "<div id=\"GalleryDisplayer\" class=\"GalleryDisplayer\">";
            
    $dircatname $this->ShowSingleType() . "/";
            
    $fullfilepath $dircatname $this->ShowSingleFile();
            if (
    stristr($fullfilepath".") !== FALSE) {
                switch (
    $dircatname) {
                    case 
    "Picture/":
                        
    $mainstring .= "<image src=\"" $fullfilepath "\" />";
                        break;
                    case 
    "Video/":
                        
    $mainstring .= "<video controls>";
                        
    $mainstring .= "<source src=\"" $fullfilepath "\">"//type=\"audio/mpeg\"
                        
    $mainstring .= "<embed src=\"" $fullfilepath "\">"//height="50" width="100"
                        
    $mainstring .= "<object data=\"" $fullfilepath "\" ></object>";
                        
    $mainstring .= "</video>";
                        
    $mainstring .= "<a href=\"" $fullfilepath "\">Click if Video Not Playing</a>";
                        break;
                    case 
    "Sound/":
                        
    $mainstring .= "<audio controls>";
                        
    $mainstring .= "<source src=\"" $fullfilepath "\">";
                        
    $mainstring .= "<embed src=\"" $fullfilepath "\">"
                        
    $mainstring .= "<object data=\"" $fullfilepath "\" ></object>"
                        
    $mainstring .= "</audio>";
                        
    $mainstring .= "<a href=\"" $fullfilepath "\">Click If Sound Not Playing</a>";
                        break;
                    case 
    "Applications/":
                        
    $mainstring .= "<p>No Applications Yet. :(</p>";
                        break;
                    default:
                        
    $mainstring .= "<p>Unknown File Directory or Type! " $dircatname "</p>";
                        break;
                }
            } else {
                
    $mainstring .= "<p>" $this->ShowSingleFile() . "</p>";
            }
            
    $mainstring .= "</div>";
            
    $increasing $this->combineE2($this->mediapageslot$this->listFsize1);
            
    $mainstring .= "<a href=\"?listSlot=$increasing&listType=$this->mediaType&listCate=$this->mediaCatagory\" name=\"ForwardAFile\" class=\"Forward\">Forward A File</a>";
            
    $mainstring .= "</div>";
            return 
    $mainstring;
        }

        public function 
    combineE($anumber$asize$changeamount) {
            if (
    is_numeric($anumber) && is_numeric($asize) && is_numeric($changeamount)) {
                
    $holder = ($anumber $changeamount);
                if (
    $holder <= $asize && $holder >= 1) {
                    return 
    $holder;
                } else if (
    $holder >= $asize) {
                    return 
    1;
                } else if (
    $holder 1) {
                    return (
    $asize 1);
                } else {
                    return 
    1;
                }
            }
            return 
    1;
        }

        public function 
    combineE2($anumber$asize$changeamount) {
            if (
    is_numeric($anumber) && is_numeric($asize) && is_numeric($changeamount)) {
                
    $holder = ($anumber $changeamount);
                if (
    $holder $asize && $holder >= 0) {
                    return 
    $holder;
                } else if (
    $holder $asize) {
                    return 
    1;
                } else if (
    $holder 0) {
                    return (
    $asize 1);
                } else {
                    return 
    0;
                }
            }
            return 
    0;
        }

    }

    ?>
    lol silly thing wouldn't let me post both in same post.
     
  3. chrisrog

    chrisrog Member

    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    6
    :confused: So many views but no help or ideas? :rofl:
     
  4. essellar

    essellar Community Advocate Community Support

    Messages:
    3,295
    Likes Received:
    227
    Trophy Points:
    63
    Well, you did ask for rather a lot, and in just about every imaginable skill area of full-stack web development from UX/UI (the front-end "surface" code) to the database. But (very) briefly:

    1. There is no excuse for not using the PHP 5.5 password functions on *nix servers running PHP 5.5 or CentOS/RHEL servers running PHP 5.4 (like x10Hosting's servers). I've posted the relevant information here before.

    2. Databases can only be used via localhost (no remote connection possible) on Free Hosting, so you would need to create PHP-based API "pages" that your front-end code (applets, mobile apps, Ajax/JavaScript) can talk to.

    3. If you suck at front-end design, you have two choices: outsource (quick, but not necessarily cheap), or spend a lot of time and effort educating yourself (A List Apart, Smashing Magazine and CSS Tricks are some of the best online resources).

    4. (Sorry, can't really be of help here. Parkinsonism has done bad things to my brain, and there's too much code in that class for me to be able to keep it straight these days. I can't see any obvious problems or gotchas, but that doesn't mean much.)

    5. I genuinely hate the idea that everything should be in a class; that's a thinking pattern that Java imposed on us, and as often as not it just leads to unnecessarily complicated code. If it feels like a "thing" it should probably be an object; if it feels like an "activity" or a "value", it probably shouldn't be. There are exceptions, of course, but those are good guidelines. Static classes with static methods are a very poor substitute for namespacing or simply properly-organized code.

    6. See #2. "localhost" is all you can use on Free Hosting.
     

Share This Page