Jump to content

Strider64

Members
  • Posts

    470
  • Joined

  • Last visited

  • Days Won

    12

Posts posted by Strider64

  1. I wrote a game in Flash Actionscript 3 about three years ago and I'm in the progress of updating my php code from old mysql to PDO.  I have the scores inserting correctly from Actionscript to the Database Table. However, Deleting the lowest score doesn't seem to work, normally I can debug what is wrong, but the game runs in Flash. I think it's a logic error more than anything else. 

     

    I kind of want to dust off this game, so I can improve the game play and the graphics. 

     

    Here's is the short php code that transfer over from actionscript to the database.

    <?php 
    include("bouncyballdbs.php");
    
    $lowestHighScore = $_POST['lowestHighScore'];
    
    $id       		=  $_POST['id'];
    $todaydate     	=  date('Y-m-d H:i:s', strtotime($_POST['todaydate']));
    $playername	    =  $_POST['playername'];
    $playerscore    =  $_POST['playerscore'];
    
    $insertScore = 'INSERT INTO ' . DATABASE_TABLE . '( todaydate, playername, playerscore ) VALUES ( :todaydate, :playername, :playerscore )';
    $insertParams = array( ':todaydate' => $todaydate, ':playername' => $playername, ':playerscore' => $playerscore );
    
    $stmt = $pdo->prepare($insertScore);
    $stmt->execute($insertParams);
    
    $query = 'DELETE FROM ' . DATABASE_TABLE . ' WHERE playerscore < :lowestHighScore';
    
    $stmt = $pdo->prepare($query);
    $stmt->bindParam(':lowestHighScores',  $lowestHighScore, PDO::PARAM_INT);
    $stmt->execute();
    

    The $id is no longer use and I should probably just take that out of the script.  Thanks in advance, John

  2. The following might help you out:

     

    (Reading you're post again, I see you're pulling it from a database table, there are ways of doing that directly from MySQL also. I am sure some guru here will help you with that.)

     

    Though you can do it in PHP like this:

    <?php
    function number_of_days($date) {
    	$today = new DateTime();
    	$future = new DateTime($date);
    	
    	$difference = $future->diff($today, true);
    	echo "There are " . $difference->days . " days till the Detroit Tigers opening day!";
    }
    
    number_of_days("2015-04-06 13:08:00");
    
  3. Dependency interjection is pretty neat for the class(es) don't have to rely on another class(es). 

     

    Here's kind of a silly example:

    <?php
    class Customer {
      private $firstName;
      private $lastName;
      
      public function __construct($firstName, $lastName) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
      }
      
      public function getFirstName() {
        return $this->firstName;
      }
      
      public function getLastName() {
        return $this->lastName;
      }
    }
    
    class Store {
      private $customer;
      private $product;
      
      public function __construct($product, Customer $customer) {
        $this->customer = $customer;
        $this->product = $product;
      }
      
      public function getCustomer() {
        return $this->customer;
      }
      
      public function getProduct() {
        return $this->product;
      }
    }
    
    $sales = new Store("iPhone 6", new Customer("Bill", "Gates"));
    
    $customer = $sales->getCustomer();
    
    echo $customer->getFirstName() . ' ' . $customer->getLastName() . ' snuck into an Apple Store an bought an ' . $sales->getProduct() . "!<br>\n";
    echo '<br>';
    echo $sales->getCustomer()->getFirstName() . ' ' . $sales->getCustomer()->getLastName();
    
    $person = new Customer("Steve", "Jobs");
    
    $store = new Store("MicroSoft Windows", $person);
    
    //var_dump($store);
    echo '<br>';
    echo $store->getCustomer()->getFirstName() . ' ' . $store->getCustomer()->getLastName() . ' Product : ' . $store->getProduct() . "<br>\n"; 

    It's great for testing modules, I would guess it would also be great for a large project where you can have multiple people working on it. 

  4. If the $userdata variable is associative and you need to maintain the keys as they were, you can use the uasort() function 

    <?php
    $usersdata = array(
      12345 => array( 'username' => "Toby", 'email' => "toby@toby.com", 'permissions' => "1,2,3,4" ),
      12346 => array( 'username' => "Tom",  'email' => "tom@tom.com", 'permissions' => "5,6,7,8" ),
      12347 => array( 'username' => "Mable", 'email' => "mable@olddear.com", 'permissions' => "1,2,3,4" ),
      12348 => array( 'username' => "Twit",  'email' => "twit@twit.com", 'permissions' => "1,2,3,4,5,6,7" )
    );
    
    echo '<h2>Array as is</h2><pre>'. print_r($usersdata, 1) . '</pre>';
    
    // Sort by Username's Name:
    uasort($usersdata, function($x, $y) {
    	return strcasecmp($x['username'], $y['username']);
    });
    
    echo '<h2>Array sorted by username\'s name</h2><pre>' . print_r($usersdata, 1) . '</pre>';
    

    The function can stay the same, but the uasort() function will take into account and maintain the index association of your values. - I know it's being a little bit nit picky but the uasort is better for associative arrays. 

    • Like 1

  5. <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Apple Bavarian Torte</title>
    </head>

    <body>
    <h1>Apple Bavarian Torte (4 stars)</h1>
    <p>A classic European torte baked in a springform pan. Cream cheese, sliced almonds, and apples make this the perfect holiday treat (12 servings).</p>
    <h2>INGREDIENTS</h2>
    <ul>
    <li>1/2 cup butter</li>
    <li>1/3 cup white sugar</li>
    <li>1/4 teaspoon vanilla extract</li>
    <li>1 cup all-purpose flour</li>
    <li>1 (8 ounce) package cream cheese</li>
    <li>1/4 cup white sugar</li>
    <li>1 egg</li>
    <li>1/2 teaspoon vanilla extract</li>
    <li>6 apples - peeled, cored, and sliced</li>
    <li>1/3 cup white sugar</li>
    <li>1/2 teaspoon ground cinnamon</li>
    <li>1/4 cup sliced almonds</li>
    </ul>
    <h2>DIRECTIONS</h2>
    <ol>
    <li>Preheat oven to 450 degrees F (230 degrees C).</li>
    <li>Cream together butter, sugar, vanilla, and flour.</li>
    <li>Press crust mixture into the flat bottom of a 9-inch springform pan. Set aside.</li>
    <li>In a medium bowl, blend cream cheese and sugar. Beat in egg and vanilla. Pour cheese mixture over crust.</li>
    <li>Toss apples with sugar and cinnamon. Spread apple mixture over all.</li>
    <li>Bake for 10 minutes. Reduce heat to 400 degrees F (200 degrees C) and continue baking for 25 minutes.</li>
    <li>Sprinkle almonds over top of torte. Continue baking until lightly browned. Cool before removing from pan.</li>
    </ol>
    <h2>REVIEWS</h2>
    <h3>4 stars</h3>
    <p>I loved the buttery taste of the crust which complements the apples very nicely.</p>
    <p><cite>Reviewed on Sep. 22, 2014 by MMASON.</cite></p>
    <h3>2 stars</h3>
    <p>Nothing special. I like the crust, but there was a little too much of it for my taste, and I liked the filling but there was too little of it. I thought the crunchy apples combined with the sliced almonds detracted from the overall flavor.</p>
    <p><cite>Reviewed on Sep. 1, 2014 by GLENDACHEF.</cite></p>
    <h3>5 stars</h3>
    <p>Delicious!! I recommend microwaving the apples for 3 minutes before baking, to soften them. Great dessert - I'll be making it again for the holidays.</p>
    <p><cite>Reviewed on August 28, 2014 by BBABS.</cite></p>
    </body>
    </html>

     

  6. There's really nothing wrong in how you did it for it works and that is all that matters.  :happy-04:

     

     

    However, this is how I would do it:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Strict//EN"
    "html://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Temperature Conversion</title>
    <meta http-equiv="content-type"
    	content="text/html; charset=iso-8859-1" />
    </head>
    
    <body>
    <h1>Fahrenheit to Celsius Conversions</h1>
    <article>
    <?php
    	$fahrenheit = 0;
    	/* Convert Fahrenheit to Celsius */
    	function fahrenheitToCelsius($fahrenheit) { 
              return round( ($fahrenheit - 32) * 5/9, 1);	
    	}
    	do {//begining of loop
    	$celsius= fahrenheitToCelsius($fahrenheit);
    	echo "<p>$fahrenheit° F = $celsius° C.</p>";
    	++$fahrenheit;
    	}
    	while ($fahrenheit <= 100);//end of loop
    ?>
    </article>
    </body>
    </html>
    

    However, I sure everyone else here would do it a little bit differently. 

    • Like 1
  7. You sure it works?

    	do {//begining of loop
    	$convertCelsius = degreeConverter($degFahrenheit);
    	echo "<p>$degFahrenheit degrees Fahrenheit = $convertCelsius degrees Celsius.</p>";
    	++degFahrenheit; // Cough Cough....
    	}
    	while ($degFahrenheit <= 100);//end of loop
    
  8. You might want to consider using a form with individual time slots for the input, I have done something similar and can be seen by clicking on my signature link. Anyways, I have tried using contenteditable on tables in the past and it's a pain in the you-know-what. Loops can help you on in writing a form, I know this isn't exactly what you're talking about, but here is an example of what I'm talking about:

      <form class="appointmentBook" action="appointment.php" method="post">
        <fieldset class="appFieldSet">
          <legend class="appLegend"><?php echo $displayDate->format('F j, Y');  ?></legend>      
            <?php
            $counter = 0;
            foreach ( $data as $appBook) {
              $output = (object) $appBook;         
              echo '<input type="hidden" name="data[' . $counter . '][date]" value="' . $output->date . '">' . "\n";
              echo '<input type="hidden" name="data[' . $counter . '][time]" value="' . $output->time . '">' . "\n";
              echo '<input type="hidden" name="data[' . $counter . '][name]" value="' . $output->name . '">' . "\n";
              echo '<label class="appLabel">' . $output->time . '</label>' . "\n";
              echo '<input class="appText" type="text" name="data[' . $counter . '][info]" value="' . $output->info . '" ' . $readonly . '>' . "\n";
              $counter++;
            }
            /* Don't show the submit button if no one is logged in */
            if ($user) {      
              echo '<input type="submit" name="submit" value="Submit" class="appSubmitBtn">' . "\n";
            }
          ?>
        </fieldset>
      </form>
    

    You can style forms to look like you want to in CSS and you can even make them look like tables if you so want.

     

    HTH John 

  9. Here's a different way of doing it and it has the same results. 

     

    <?php
    $userChoice = array(-2, "Please choose", -1, "N/A");
    $year = date("Y");
    $dob = range($year-18, $year-74);
    ?>
    
    <select name="birthyear" class="date">
      <?php
        echo '<option value="' . $userChoice[0] . '">' . $userChoice[1] . '</option>' . "\n";
        echo '<option value="' . $userChoice[2] . '">' . $userChoice[3] . '</option>' . "\n";
        foreach ( $dob as $value ) {
          echo '<option value="'. $value . '">' . $value . '</option>' . "\n";
        }
      ?>
    </select>

     

  10. I remember a while back a company offering free products (samples) and what they did is took your credit card number (I am assuming they used a billing service) and charge a nominal fee like $5.00 or something like that. An when the product shipped the person was credited the money or so they said for I never tried it. The point being people will tend not to make multiple accounts if they might get socked with forking over money. The problem with this is two things, one you alienate some people and two some people won't bother signing-up. I fall in the the first category, I'm suspicious anytime a company asks for a CC#. 

  11. Close ->

    <?php
    class Cat {
      public $isAlive = true;
      public $numLegs = 4;
    		
      public function __construct($name) {
        $this->name = $name;
      }
      public function meow() {
        return "Meow meow";  
      } 
    }  
    $cat1 = new Cat('CodeCat');
    echo $cat1->meow();
    

  12. define('DATABASE_HOST', 'localhost'); // normally it's called localhost:
    define('DATABASE_NAME', 'notes_db');
    define('DATABASE_USERNAME', 'root'); // usually it's called root:
    define('DATABASE_PASSWORD', 'your password');

    $con = new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);

    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

     

  13. If your ISP is still using that version either tell them to upgrade and if they won't then I would recommend shopping for a new ISP that is using a current version at least PHP 5.4 (preferably 5.5 or 5.6). 

    • Like 1
  14. try $mail->isSendMail() instead of $mail->isSMTP() - I was having a problem yesterday with my remote server and this fix it. So it might be the problem?  An like stated ports 465 or ports 587 need to be commented out $mail->Port = 587. 

  15. Hi everyone. I'm completely new to PHP. I've been trying to teach myself to code with it and learn as much as I can for the past few days because it'll definitely come in handy. So far, everything I've learned is just from articles and videos.

     

    Anyway, let's skip the boring stuff.

     

    For literally the whole day today, I've been trying to figure out what the heck is wrong with a script I'm working with. I didn't code it myself. I'm pretty much just modifying it to fit my needs from the generic "template" it comes in.

    When I first started out I found it easier to write my own script than try to use someone else's script (Trust me I found out the hard way), for I found out that there is a lot of bad scripts on the Internet, scripts that use obsolete techniques or scripts that are way above by level of understanding (as a beginner). My opinion if you do use a script be it a library of some kind. From quickly glancing over you issue(s) I see that it deals with uploading images and maybe editing them. If you do an internet search I know you will find some pretty good php image libraries (I know I have used some of them myself). I think that would be better than trying to modifying someone's script to fit your need and I think you would find understanding PHP better. Just my opinion. 

     

    You might even consider using a PHP framework that too would probably be better than using someone's script. 

  16. /* Get the path and filename that you are currently on. */
    $phpSelf = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_URL);
    $path_parts = pathinfo($phpSelf);
    $basename = $path_parts['basename'];
    

    This is one way of making PHP_SELF safe and to utilize it all you would do something like this:

    <form class="formStyle" action="<?php echo $basename; ?>" method="post">
    

    However, if you really just want to be safe then just do this:

    <form class="formStyle" action="register.php" method="post">
    
  17. There also a lot of neat things you can do with a classes, such as this for example:

     

    Let's say you have a class called Member.php

    <?php
    
    # Member class - Store user info and functions to access/control the flow of data.
    
    class Member {
    
        // The member attributes (variables) containing required and optional information.
        // The attributes must correspond to the database table columns:
    
        private $id = NULL;
        private $userType = NULL; // Required 
        private $username = NULL; // Required 
        private $email = NULL; // Required   
        private $pass = NULL; // Required
        private $fullName = NULL;
        private $validation_code = NULL;
        private $address = NULL;
        private $city = NULL;
        private $state = NULL;
        private $zipCode = NULL;
    
        // Method returns the user ID:
        public function getId() {
            return $this->id;
        }
    
        // Grab the user's username:
        public function getUsername() {
            return $this->username;
        }
    
        // Grab the user's full name:
        public function getFullName() {
            return $this->fullName;
        }
    
        // Grab the password:
        public function getPass() {
            return $this->pass;
        }
    
        public function getUserType() {
            return $this->userType;
        }
    
        // Clear the password once user is logged in:
        public function clearPass() {
            $this->pass = NULL;
        }
    
        public function getEmail() {
            return $this->email;
        }
    
        // Method returns a Boolean if the user is an administrator:
        public function isAdmin() {
            return ($this->userType == 'author');
        }
    
        public function isSysop() {
            return ($this->userType == 'sysop');
        }
    
        public function isNewUser() {
            return ($this->userType == 'public');
        }
    
        // Method returns a Boolean indicating if the user is an administrator
        // or if the user is the original author of the provided page:
        public function canEditPage(Page $page) {
            return (($this->isAdmin() || ($this->id == $page->getCreatorId())));
        }
    
        // Method returns a Boolean indicating if the user is an administrator or an author:
        public function canCreatePage() {
            return ($this->isAdmin() || $this->isSysop());
        }
    
    }
    

    The you can simply do the following to put the pull a corresponding record from a database table:

        // Check against the database:
        $query = 'SELECT id, userType, username, email, pass, fullName, address, city, state, zipCode FROM users WHERE username=:username';
        $stmt = $pdo->prepare($query);
        $stmt->execute(array(':username' => $_POST['username']));
        /* The following fetches the class Member and creates an instance $stored_user_data */
        $stmt->setFetchMode(PDO::FETCH_CLASS, 'Member');
        $stored_user_data = $stmt->fetch();
    

    Then for example you can check the user's input against the database table password like this:

        // Verify Stored Hashed Password against input:
        if ($stored_user_data) {
            $result = password_verify($_POST['pass'], $stored_user_data->getPass());
        }
    

    However, you got to learn how to crawl before you walk, so following php_bad_boy's post would probably be the prudent thing to do. ;D

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.