Jump to content

Strider64

Members
  • Posts

    470
  • Joined

  • Last visited

  • Days Won

    12

Posts posted by Strider64

  1. You may add an (inaccurate) Ajax pre-check as an extra usability feature, but the actual uniqueness check must be done by a database constraint. It cannot be implemented with normal queries.

    What I do is check one check at the database constraint and one at registration time with a validation check (no Ajax).

     

    Here's the method I use for the validation check:

      public function checkAvailability($username) {
        $db = Database::getInstance();
        $pdo = $db->getConnection();
    
        $this->query = "SELECT 1 FROM users WHERE username=:username";
    
        $this->query_params = [':username' => $username];
    
        $this->stmt = $pdo->prepare($this->query);
    
        $this->stmt->execute($this->query_params);
    
        $this->result = $this->stmt->fetch();
    
        if ($this->result) {
          return 'Username is invalid or not available, please re-enter!';
        }
      } 

    If the validation doesn't get it, when the user's data is submitted and by some chance there is another user with the same name then it will catch it as Jacques1 has already. The only bummer is the user will have to re-enter all his/her data, but I can't see that happening unless it's a really busy website (A good thing :happy-04: ) or a freak happening. 

  2. You should treat the sidebar as a navigational menu and the sidebar as a container: 

     

    For example:

    <div id="sidebar">
      <h3>Popular Articles</h3>
      <div id="sidebarNav">
        <ul>
           <li><a href="http://pickmysmoker.com/masterbuilt_propane.php/">Masterbuilt Propane Smoker Review</a></li>
           <li><a href="#">Continue on...</a></li>
           <!-- Continue on with nav menu -->
        </ul>
      </div><!--/ End of #sidebarNav -->
    </div><!--/ End of #sidebar -->

    I would also check into using clearfix (Do a Google Search to find good examples) and utilizing box-sizing: border-box; for your sidebar issues. Do it this way you will be able to really stylize your Sidebar Navigation. As a personal preference I prefer to have my sidebar navigation on the left rather than the right, but like I said it's a personal preference. 

  3. There is a good book called "PHP Objects, Patterns, and Practice" by Matt Zandstra (Fourth Edition) that is pretty good on OOP and more. I would consider doing iCRUD interface for it forces you to structure your OOP. 

    <?php
    /* The iCrud interface.
     * The interface identifies four methods:
     * - create()
     * - read()
     * - update()
     * - delete()
     */
    
    interface iCrud {
    
      public function create($data);
    
      public function read();
    
      public function update($data);
    
      public function delete($id=NULL);
      
    }
    
    

    Besides then you can swap your classes if you move on to a different project. I know books can be tedious, but the only true way I found how to learn the material is either via books or by taking a college course on the subject. Just my .02 cents.  ;D

  4. Simply save the path and name of file to a database table, along a text description of the image (which you would get from the user's input). Then when you can retrieve all the necessary information to do what you want it to do. That's the thought process that I would do, I  have done a picture gallery without the description portion, but it wouldn't be to hard to add that feature. For all I would have to do is add another column in my database structure. Anyways, throw in some JQuery (JavaScript) into the mix and you can really get some neat results. 

     

    This is an example of what I do after I process my file upload then I proceed to save the info to a database table:

    // Insert into the database using PDO and prepared statements:
    $query = 'INSERT INTO pictures (thumbnail, picName, subDirectory, user_name) VALUES (:thumbnail, :picName, :subDirectory, :userName)';
    $stmt = $pdo->prepare($query);
    $result = $stmt->execute(array(':thumbnail' => $thumbImage, ':picName' => $image, ':subDirectory' => $name, ':userName' => 'Strider64'));
    
    $resultDir = displayDir($pdo, $name); // Re-display the thumbnails:
    
  5. A variable doesn't have to be in a DOM element to read it from AJAX, you can simply read it in from php via Ajax or by pulling the data in indirectly from a DOM element (In you case it would probably be a form element input?) using a keyboard press (I believe Google's search engine does something like that). 

  6. All three scripts are different, which makes it very confusing and your question doesn't relate to the code provided (Well at least I don't think it does) ??? 

     

    You're missing the value attribute in you <option> tag:

    <form action="process.php" method="post">
      Game:
      <select name="game">
        <option>Select a Game</option>
        <option value="Counter-Strike 1.3">Counter-Strike 1.3</option>
        <option value="Counter-Striker: Source">Counter-Strike: Source</option>
        <option value="Counter-Strike: GO">Counter-Strike: GO</option>
        <option value="ArmA: 3">ArmA: 3</option>
        <option value="Call of Duty: 4">Call Of Duty: 4</option>
      </select>
      Slots
      <select name="game">
        <option>Desired Slots</option>
        <?php 
    		  for ( $x = 10; $x <= 30 ; $x++ ) {
    				echo '<option value="' . $x . '">' . $x . '</option>' . "\n";
    			}
    		?>
      </select>
      <input type="submit" name="submit" value="Submit" />
    </form>
    
  7. You skipped a class, a test no less, and you want someone here to write your answer for you.  SHAME ON YOU AND SHAME ON ANYONE HERE WHO GIVES YOU WHAT YOU ASK FOR.

    He skipped the question on the test, probably couldn't answer it. Probably just trying to see what the answer was that was on the test. 

  8. From a book I read by Larry Ullman that MVC isn't really a true "pattern"  (He said it, I didn't so I don't want people coming down on me  :happy-04: ), so I guess the Enterprise Pattern could be MVC though I haven't gotten that far in another book that I'm reading about OOP. But I'm sure some guru here will answer this question better.  ;D

  9. If you just want to check to see if it is a valid integer :

    <?php
    /* If you just want to check to see if it's a valid integer */
    if (isset($_POST['id']) &&  !filter_var($_POST['id'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
       echo "I'm not an integer<br>";
    } elseif (isset($_POST['id'])) {
       echo 'The id is ' . $_POST['id'] . '<br>';
    }
    
    ?>
    <form action="" method="post">
    Enter Number <input type="text" name="id" >
    <input type="submit" name="submit" value="Submit">
    </form>
    
    

    An maybe this will clear up the confusion : http://us.php.net/manual/en/intro.filter.php

    $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
    
  10. I am seriously trying to wrap my head around abstract classes, but the more I try to understand it the more I get confused.  :confused:

     

    I've even dug out a book by Larry Ullman to no avail.

     

    Here's the example in the book:

     

    Shape abstract class

    <?php # Script 6.1 - Shape.php
    /* This page defines the Shape abstract class.
     * The class contains no attributes.
     * The class contains to abstract methods.
     * - getArea()
     * - getPerimeter()
     */
     
     abstract class Shape {
    	 // No Attributes to declare.
    	 // No constructor or destructor defined here.
    	 
    	 // Method to calculate and return the area.
    	 abstract protected function getArea();
    	 // Method to calculate and return the perimeter.
    	 abstract protected function getPerimeter();
    	 
     } // End of Shape Class.
    

    Triangle class:

    <?php # Script 6.2 - Triangle.php
    /* This page defines the Triangle class.
     * The class contains two attributes:
     * - private $_sides (array)
     * - private $_perimeter (number)
     * The class contains three methods.
     * - __construct()
     * - getArea()
     * - getPerimeter()
     */
     
    class Triangle extends Shape {
    	
    	// Declare the attributes:
    	private $_sides = array();
    	private $_perimeter = NULL;
    	
    	// Constructor:
    	function __construct($s0 = 0, $s1 = 0, $s2 = 0) {
    	
    		// Store the values in the array:
    		$this->_sides[] = $s0;
    		$this->_sides[] = $s1;
    		$this->_sides[] = $s2;
    		
    		// Calculate the perimeter:
    		$this->_perimeter = array_sum($this->_sides);
    		
    	} // End of constructor.
    	
    	// Method to calculate and return the area:
    	public function getArea() {
    		
    		// Calculate and return the area:
    		return (SQRT(
    		($this->_perimeter/2) *
    		(($this->_perimeter/2) - $this->_sides[0]) *
    		(($this->_perimeter/2) - $this->_sides[1]) *
    		(($this->_perimeter/2) - $this->_sides[2])
    		));
    		
    	} // End of getArea() method.
    	
    	// Method to return the perimeter:
    	public function getPerimeter() {
    		return $this->_perimeter;
    	} // End of getPerimeter() method.
    	
    } // End of Triangle Class.
    

    and to execute in finding the area and perimeter of a triangle:

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Abstract Class</title>
    </head>
    
    <body>
    <?php # Script 6.3 - abstract.php
    // This page uses the Triangle class - (Script 6.2), which is derived from Shape (Script 6.1)
    
    // load the class definitions:
    require('Shape.php');
    require('Triangle.php');
    
    // Set the triangle's sides:
    $side1 = 5;
    $side2 = 10;
    $side3 = 13;
    
    // Print a little introduction:
    echo "<h2>With sides of $side1, $side2, and $side3...</h2>\n";
    
    // Create a new triangle:
    $t = new Triangle($side1, $side2, $side3);
    
    // Print the area.
    echo '<p>The area of the triangle is ' . $t->getArea() . '</p>';
    
    // Print the perimeter.
    echo '<p>The perimeter of the triangle is ' . $t->getPerimeter() . '</p>';
    
    // Delete the object:
    unset($t); 
    	
    ?>
    </body>
    </html>
    

    What is the point of having an abstract class when it runs fine without it being declared? For example I can take out extends Shape in the Triangle class and it will work just fine?  I don't know if If I will ever understand this concept of abstract and interface classes.... :shrug:

     

    Any help in this matter will be greatly appreciated. Thanks John 

     

    OK, I fooled around with it some more and took out the include('Shape.php') and it spat an out an error stating it wasn't found. So is an abstract class just forcing a person to adhere to the methods being declared in the abstract class? (I'm probably not explaining this right). 

  11. Personally I would call $return something else, for that is close to using a reserve word that's just me:

    // In your JQuery 
    $("#sub").click(function() {
      var name = $("#name").val();
      var town = $("#town").val();
            
      jQuery.ajax({
        type: "POST",
        url: "postScripts/addDetail.php",
       
        data: 'name='+name+'&town='+town,
        
        success: function(info) {
         var name = info.name,
             town = info.town;
    
         /* Continue with code */  
        }
      });
      return false
    }) 
    
    
  12. You were close. btw I don't know what you mean it won't open up in the browser for it should had even with an error(s).

    /* You had this */
    $message = "Well done! It took you '$_POST["counter"]' tries!";
    /* It should had been something like this */
    $message = 'Well done! It took you ' . $_POST["counter"] . ' tries!';
  13. The call method would know what the attribute(variable) is doing because you would have to set it.

     

    Here's a little script that I put together using all magic methods (I personally would probably never use all magic methods, but that is just me):

    <?php
    
    class Phone {
    	
    	private $number = array();
    	
    	
    	public function __set($name, $value) {
    		$this->number[$name] = $value;
    	}
    	
    	public function __get($name) {
    		if (array_key_exists($name, $this->number)) {
    			return $this->number[$name];
    		}
    		
    	}
    	
    	public function __call($method, $arg) {
    		if (array_key_exists($method, $this->number)) {
    			return "Thank You for calling " . $this->number[$method] . "<br>";
    		}
    	}
    	
    }
    
    $myNumber = new Phone;
    
    $myNumber->phoneNumber = '555-421-4685'; // Set the attribute:
    echo $myNumber->phoneNumber; // Get the attribute: 
    echo '<br>';
    echo $myNumber->phoneNumber(); // Call the method:
    echo '<br>';
    
    $myNumber->telephone = '555-555-1313'; // Set the attribute:
    echo $myNumber->telephone; // Get the attribute:
    echo '<br>';
    echo $myNumber->telephone(); // Call the method:
    
    
    • Like 1
  14. It an utilities file that I have on each page I have this

    // Check for a user in the session:
    $user = (isset($_SESSION["user"])) ? $_SESSION["user"] : NULL;
    

    Of course when you login you would put the user in session, then I simply do this where I have my login/logout (Logout is simply taking the user out of session)

    If ($user) {
    /* Logout Script */
    } else {
    /* Login Script */
    }
    
  15. OOPS   :shrug:

     

    Though I did create a script that grabs the timezone using PHP, Ajax and JQuery :

    <!DOCTYPE HTML>
    <html>
      <head>
        <meta charset="utf-8">
        <title>TimeZone</title>
      </head>
    
      <body>
        <p>Today's Date and Time is <span id="result"></span></p>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="jstz.min.js"></script>
        <script>
          $(document).ready(function() {
            var timezone = jstz.determine(),
                myTimezone = timezone.name(), // Grab my Timezone:
                params = {timezone: myTimezone}, // Create an object
    
            /* A serialized repesentation of an object, suitable for an Ajax Request */
            myData = jQuery.param(params);
    
            /* The Ajax Request */
            $.ajax({
              type: "post",
              url: "myDateScript.php",
              data: myData, // The data that is being sent to myDateScript.php
              success: function(info) {
                $('#result').html(info); // Display the result back when saved: 
              }
            }); // End of Ajax Request:				
          }); // End of Doc Ready:
        </script>
      </body>
    </html>
    

    The PHP

    <?php
    // Date & time format.
    $date_format = "m/d/Y";
    $time_format = "h:i A";
    
    // Set timezone.
    $timezone = $_POST['timezone']; // The Ajax Request $_POST['timezone']:
    //$timezone = America/New_York';
    
    
    // Get current datetime.
    $date = new DateTime();
    
    // Set timezone.
    $date->setTimezone(new DateTimeZone($timezone));
    
    // Echo current date and time... plus send the response back:
    echo $date->format($date_format . " " . $time_format); 
    

    Maybe the scripts can modify it to fit what he wants? 

  16. I was bored, it isn't perfect but it works and needs to be stylized: 

    <?php
    /* I would pull the form and table apart, by that I mean */
    /* don't try to do two things at once. I haven't done    */
    /* any number validation and I'm sure this needs to be   */
    /* modified for I don't know what exactly the initial    */
    /* HTML looked liked. 																	 */
    if ( isset($_POST['submit']) && $_POST['submit'] == "Generate" ) {
    	$numRows = $_POST['rows'];
    	$numCols = $_POST['columns'];
    	$operand = $_POST['operation'];
    }
    
    
    ?>
    <!DOCTYPE HTML>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Learning How to Multiply & Add</title>
      <style>
        table {
         border-collapse: collapse;
       }
       table, th, td {
         border: 1px solid #2e2e2e;
       }
     </style>
    </head>
    
    <body>
      <form action="" method="post">
        <label for="numRows" class="labelStyle">Number of Rows:</label>
        <input id="numRows" name="rows" value="">
        <label for="numCols" class="labelStyle">Number of Cols:</label>
        <select id="numCols" name="columns">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="4">4</option>
          <option value="8">8</option>
          <option value="16">16</option>
        </select>
        <br>
        <h3>Operation</h3>
        <input id="multiplication" type="radio" name="operation" value="multiplication" checked>
        <label for="multiplication">Multiplication</label>
        <input id="addition" type="radio" name="operation" value="addition">
        <label for="addition">Addition</label>
        <br>
        <input class="submitBtn" type="submit" name="submit" value="Generate">
      </form>
      <table class="tableStyle">
        <thead>
          <tr>
            <!-- Number of Colums and Rows are initial set to 10 -->
            <?php $columns = isset($numCols) ? $numCols : 10; ?>
            <th colspan="<?php echo $columns ?>"><?php echo $columns . " Columns"; ?></th>
          </tr>
          <tr>
            <?php
            for($x = 0; $x < $columns; $x++) {
             echo '<th>' . $x . '</th>' . "\n";
           }
           ?>
         </tr>
       </thead>
       <tbody>
         <?php
    
         $rows = isset($numRows) ? $numRows : 10;
         for($x = 0; $x < $rows; $x++) {
          echo "<tr>\n";
          for($y = 0; $y < $columns; $y++) {
           /* If operand isn't set yet, I set it to an addition default */
           if ( isset($operand) &&  ($operand == "multiplication")) {
            echo "<td>" . $x . " X " . $y . " = " . ($x * $y) . "</td>\n";
          } elseif ( isset($operand) && ($operand == "addition") ) {
            echo "<td>" . $x . " + " . $y . " = " . ($x + $y) . "</td>\n";
          } else {
            echo "<td>" . $x . " + " . $y . " = " . ($x + $y) . "</td>\n";
          }
        }
        echo "</tr>\n";
      }
      ?>
    </tbody>
    
    </table>
    </body>
    </html>
    
  17.  

    The PDO page also says:

     

    (emphasis added)

     

    The automatic quoting of the bound data is a side-affect of the prepared statement. And we need to clearly state that only the bound data is protected from SQL injection. Otherwise, we are giving a false sense of security; the same false sense of security that many PHP coders had when using magic_quotes - which has been removed from the language because of the false sense of security.

     

     

    DavidAM climbs off his soap-box

     

    Not trying to make it more confusing as it is, but isn't that exactly what the OP has, by that I mean the data ISN"T bound so the data must be protected some other way from SQL injection. I remember reading a book by Larry Ullman on PHP and in the example he gave he didn't use prepared statements for it was just a simple little query statement, but he made sure to secure it just the same. 

  18. If you're using jQuery this is I how I do it:

    $(function() {
     
      $(window).scroll(function() {
        // Use Console.log to figure out your 'Sticking' point.
        // console.log($(window).scrollTop())
        if ($(window).scrollTop() > 200) {
          $('.navigational-wrapper').addClass('navbar-fixed');
        }
        if ($(window).scrollTop() < 201) {
          $('.navigational-wrapper').removeClass('navbar-fixed');
        }
      });
    
    }); // END MAIN JQUERY FUNCTION
    
×
×
  • 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.