Jump to content

PaulRyan

Members
  • Posts

    876
  • Joined

  • Last visited

  • Days Won

    4

Posts posted by PaulRyan

  1. Turn of full error reporting:

     

    <?PHP
    
      error_reporting(E_ALL);
      ini_set("display_errors", 1);
    
    ?>

     

    Also, this line:

     

    if(!$_POST['username'] | !$_POST['pass']) {

     

    Should be:

    if(!$_POST['username'] || !$_POST['pass']) {
  2. Ohh right, well you need to create a new session variable something like "$_SESSION['numbers_guessed']" and then add each guessed number that is wrong to that variable. Look into array for that.

     

    Displaying amount of guesses I explained in my previous post, use the variable "$_SESSION['guesses']"

     

    I did the hard work, now have a little try to see what you can do, and pop back with any questions :)

  3. Was bored, so I gave this a little go.

     

    Try this:

     

    <?PHP
    
      session_start();
      #session_destroy();
     
      //### Current file name
      $filename = basename($_SERVER['PHP_SELF']);
     
      //### If play is set, reset all values
      if(isset($_GET['play'])) {
        unset($_SESSION['number']);  
        unset($_SESSION['guesses']);
        unset($_SESSION['guessed']);
      }
     
      //### Start the output
      $output = '';
     
      //### Check if the form is posted
      if($_SERVER['REQUEST_METHOD'] == 'POST') {
        
        //### Set the number that was guessed
        $numberGuessed = isset($_POST['numberGuessed']) ? (int)$_POST['numberGuessed'] : FALSE ;
        
        //### If no number is guessed, display error
        if(empty($numberGuessed)) {
          $output .= 'Please enter a number to guess. <br>';
          
        //### Validation passed  
        } else {    
            
          //### Increment guesses
          $_SESSION['guesses']++;
            
          //### Check to make sure they have guesses available
          if($_SESSION['guesses'] <= 7) {
              
            //### check if they guessed the secret number
            if($numberGuessed == $_SESSION['number']) {
              $_SESSION['guessed'] = TRUE;
              
            //### check if the number is lower than the secret number
            } else if($_SESSION['number'] < $numberGuessed) {
              $output .= 'Bad luck! The number is lower. <br>';
            
            //### Else the number is higher
            } else {
              $output .= 'Bad luck! The number is higher. <br>';
            }
            
          }
        }
      }
     
      //### Set the secret number and amount of guesses
      $_SESSION['number']  = isset($_SESSION['number']) ? $_SESSION['number'] : mt_rand(1,10) ;
      $_SESSION['guesses'] = isset($_SESSION['guesses']) ? min($_SESSION['guesses'], 7) : 0 ;
     
      //### Debug purposes
      echo '<pre>';
      print_r($_SESSION);
      echo '</pre>';
     
      //### Check if the secret number has been guessed
      if(isset($_SESSION['guessed'])) {
        $output .= 'Well done! You guessed the number. <br>';
        $output .= '<a href="'.$filename.'?play=true">Play again?</a>';  
        
      //### Check if they have used all of their guesses
      } else if($_SESSION['guesses'] >= 7) {
        $output .= 'You used up all your guesses. <br>';
        $output .= 'The secret number was: '. $_SESSION['number'] .'. <br>';
        $output .= '<a href="'.$filename.'?play=true">Play again?</a>';
        
      //### If both checks passed, show form
      } else {
        $output .= '<form method="POST" action="'. $filename .'">
                Your Guess: <input type="text" name="numberGuessed">
                <input type="submit" value="Submit Guess">
              </form>';
      }
     
      echo $output;
    ?>
  4. When outputting the data, use nl2br to convert line ending characters to new lines.

     

    Take this for example:

     

    <?PHP
    
      $string = "This is a string. \r\n This is on a new line.";
     
      echo 'Without nl2br() <br> "'. $string.'"';
     
      echo '<br>---------------------------<br>';
     
      echo 'With nl2br() <br> "'. nl2br($string) .'"';
    
    ?>
  5. I think I decoded what he means :geek:

     

     

    <?PHP
     
      if(!empty($row['skill3link']) && !empty($row['skill3'])) {
          //### Echo content here
      }
    
    ?>

    *Edit: Why do you keep jumping in and out of the PHP tags? That looks really messy, and is quite hard to follow.

  6. This is something I put together:

     

    <?PHP
    
      //### Start the session
      session_start();
     
      //### File name
      $filename = basename($_SERVER['PHP_SELF']);
     
      //### Assign $_GET variables if they exist
      $itemID    = isset($_GET['id']) ? (int)$_GET['id'] : FALSE ;
      $action    = isset($_GET['action']) ? $_GET['action'] : FALSE ;
      $subAction = isset($_GET['sub_action']) ? $_GET['sub_action'] : FALSE ;
    
      //### Add item to the items array, if it doesn't already exist
      if($action == 'add') {
        if(!isset($_SESSION['cart']['items'][$itemID])) {
          $_SESSION['cart']['items'][$itemID] = 1;
        }
        header('Location: '. $filename);
        exit;
      }
     
      //### Increase item count, if the item exists
      //### Decrease item count, if the item exists
      //### If the item count equals 1, remove the item from the item array
      if($action == 'update') {
        if($subAction == 'add') {
          if(isset($_SESSION['cart']['items'][$itemID])) {
            $_SESSION['cart']['items'][$itemID]++;
          }
        } else if($subAction == 'remove') {
          if(isset($_SESSION['cart']['items'][$itemID])) {
            if($_SESSION['cart']['items'][$itemID] > 1) {
              $_SESSION['cart']['items'][$itemID]--;    
            } else {
              header('Location: '. $filename .'?action=delete&sub_action=item&id='. $itemID);
              exit;
            }
          }
        }
        header('Location: '. $filename);
        exit;
      }  
     
      //### Delete item from item array, if it exists
      //### Delete all items from item array
      if($action == 'delete') {
        if($subAction == 'item') {
          if(isset($_SESSION['cart']['items'][$itemID])) {
            unset($_SESSION['cart']['items'][$itemID]);    
          }
        } else if($subAction == 'cart') {
          $_SESSION['cart']['items'] = array();   
        }
        header('Location: '. $filename);
        exit;
      }
     
      echo '<pre>';
      print_r($_SESSION);
      echo '</pre>';
     
      for($i=1; $i<=5; $i++) {
        echo '<a href="?action=add&id='.$i.'">Add Item '.$i.'</a> - ';
        echo '<a href="?action=update&sub_action=add&id='.$i.'">Update Item '.$i.'</a> - ';
        echo '<a href="?action=update&sub_action=remove&id='.$i.'">Remove Item '.$i.'</a> - ';
        echo '<a href="?action=delete&sub_action=item&id='.$i.'">Delete Item '.$i.'</a>';
        
        echo '<br>';
      }
     
      echo '<a href="?action=delete&sub_action=cart">Delete Cart</a>';
     
    ?>
  7. Both answers lacked a check if the results are not a multiple of 4.

     

    If you put 13 results, you get an open div round the last result, but not a closing one.

     

    Try this:

     

    <?PHP
     
      $num = 0;
     
      $output = '';
     
      foreach($db_res as $value) { $num++;
        //### Open div
        if($num%4==1) {
          $output .= '<div>';
        }
        
        //### Add value to output
        $output .= $value;
        
        //### Close div
        if($num%4 == 0) {
          $output .= '</div>';
        }
          
      }
     
      //### If total results is not multiple of 4, close the final div
      if($num%4 != 0) {
        $output .= '</div>';
      }
     
      //### Display output
      echo $output;
     
    ?>
  8. You shouldn't jump in and out of PHP tags like that, it makes for messy code.

     

    Try this:

     

    <?PHP
    
      if($highname1 == 'open') {
        $output = '<div class="module high open">
                     <img src="Icon_hi_slot.png" width="46" height="46">
                   </div>';
      } else if(strlen(trim($highname1))) {      
        $output = '<div class="module high inactive">
                   </div>';
      } else {      
        $output = '<div class="module high" data-typeid=$high1typeID data-name=$high1name>
                     <img src="http://image.website.com/Type/'. $high1typeID .'_64.png">
                   </div>';
      }
     
      echo $output;
     
    ?>
×
×
  • 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.