Jump to content

Recommended Posts

Hey guys,

 

I got a very amateuristic question, but I never worked with PHP before, but I do have to integrate it in my prototype this time so I guess there's no other choice. I downloaded a php comment system script from the internet to integrate into my website which is all working properly (I'm just running it locally without mysql, so no worries about that it's just a prototype). However there's a function that I want to add. In my index.php I have an image which source I want to make variable. I know I have to do this in a way such as:

 

<?php $imageNumber = 1?>

<img src="<?php echo $imageNumber ?>.png">

 

So there's a bunch of .png's stored in my folder from 1.png to 10.png. Then I get that I have to add a function that will give the $imageNumber ++. This has to be done every time a comment is validated and get's posted. The part where I'm getting stuck on is how to save the information of the $imageNumber change so that it keeps stacking up, instead of immediately falling back to 1 again every time a comment is posted. I have no idea if this is a lot of work (don't think so). Is there anyone who can lend me a hand?

 

Here are the two scripts for the comment part:

 

 

<?php
date_default_timezone_set('UTC');


class Persistence {
  
  private $data = array();
  
  function __construct() {
    session_start();
    
    if( isset($_SESSION['blog_comments']) == true ){
      $this->data = $_SESSION['blog_comments'];
    }
  }
  
  /**
   * Get all comments for the given post.
   */
  function get_comments($comment_post_ID) {
    $comments = array();
    if( isset($this->data[$comment_post_ID]) == true ) {
      $comments = $this->data[$comment_post_ID];
    }
    return $comments;
  }
  
  /**
   * Get all comments.
   */
  function get_all_comments() {
    return $this->data;
  }


  
  /**
   * Store the comment.
   */
  function add_comment($vars) {
    
    $added = false;
    
    $comment_post_ID = $vars['comment_post_ID'];
    $input = array(
     'comment_author' => $vars['comment_author'],
     
     'comment' => $vars['comment'],
     'comment_post_ID' => $comment_post_ID,
     'date' => date('r'));
    
    if($this->validate_input($input) == true) {
      if( isset($this->data[$comment_post_ID]) == false ) {
        $this->data[$comment_post_ID] = array();
      }
      
      $input['id'] = uniqid('comment_');
      
      $this->data[$comment_post_ID][] = $input;
      
      $this->sync();
      
      $added = $input;
    }
    return $added;
  }


  
  function delete_all() {
    $this->data = array();
    $this->sync();
  }
  
  private function sync() {
    $_SESSION['blog_comments'] = $this->data;    
  }
  
  /**
   * TODO: much more validation and sanitization. Use a library.
   */  
  private function validate_input($input) {
    
    $input['comment_author'] = substr($input['comment_author'], 0, 70);
    if($this->check_string($input['comment_author']) == false) {
      return false;
    }
    $input['comment_author'] = htmlentities($input['comment_author']);


    $input['comment'] = substr($input['comment'], 0, 300);
    if($this->check_string($input['comment'], 5) == false) {
      return false;
    }
    $input['comment'] = htmlentities($input['comment']);


    $input['comment_post_ID'] = filter_var($input['comment_post_ID'], FILTER_VALIDATE_INT);  
    if (filter_var($input['comment_post_ID'], FILTER_VALIDATE_INT) == false) {
      return false;
    }


    return true;
  }
  
  private function check_string($string, $min_size = 1) {
    return strlen(trim($string)) >= $min_size;
  }
}


?>
 
And the Second:
 
<?php
require('Persistence.php');


$db = new Persistence();
$added = $db->add_comment($_POST);






if($added) {
  header( 'Location: index.php' );  
   
}
else {
  header( 'Location: index.php?error=Your comment was not posted due to errors in your form submission' );
}
?>






Edited by AngeS
Link to comment
https://forums.phpfreaks.com/topic/278763-save-variable-change/
Share on other sites

Index.php =

 

 

<?php
require('Persistence.php');
$comment_post_ID = 1;
$db = new Persistence();
$comments = $db->get_comments($comment_post_ID);
$has_comments = (count($comments) > 0);
$imageNumber = 0;
?>


<!DOCTYPE html>
<html lang="en-US">
<head>
<title>MEMORIAL J...
   </title>
<meta charset="utf-8" />


<link rel="stylesheet" href="css/main.css" type="text/css" />




</head>


<body id="index" class="home">


<header id="banner" class="body">
<h1>IN LOVING MEMORY OF <br> ... </br></h1>
        <h1> <img src="images/blabla.png"></h1>
        


</header>
    
    <div class="contentbody">






<section id="comments" class="body">
    
    <div id="respond">


      <h3>Share Your Memories</h3>


      <form action="post_comment.php" method="post" id="commentform">


        <label for="comment_author" class="required"><h4>NAME</h4></label>
        <input type="text" name="comment_author" id="comment_author" value="" tabindex="1" required="required">
        




        <label for="comment" class="required"><h4>MEMORY</h4></label>
        <textarea name="comment" id="comment" rows="10" tabindex="4"  required="required"></textarea>


        <input type="hidden" name="comment_post_ID" value="<?php echo($comment_post_ID); ?>" id="comment_post_ID" />
        <input name="submit" type="submit" value="SHARE" />
       
        
      </form>
      
    </div>


 <header>
<h2>Memories</h2>
</header>


    <ol id="posts-list" class="hfeed<?php echo($has_comments?' has-comments':''); ?>">
      <li class="no-comments"><h6>Be the first to share a memory</h6></li>
      <?php
        foreach ($comments as &$comment) {
          ?>
          <li><article id="comment_<?php echo($comment['id']); ?>" class="hentry"> 
     
     <abbr class="published" title="<?php echo($comment['date']); ?>">
     <h5><?php echo( date('d F Y', strtotime($comment['date']) ) ); ?></h5>
     </abbr>


     <address class="vcard author">
     <h5> By <a href="*"><?php echo($comment['comment_author']); ?></a></h5></address>
     </footer>


     <div class="entry-content">
     <h6><?php echo($comment['comment']); ?></h6>
     </div>
     </article></li>
          <?php
        }
      ?>
</ol>






</section>




</div>




</section>


</section>
    </div>
    
         <footer>         
                <div class="container">
                    <div>
                        <img src="images/logo_footer.png" alt="">
                                                <div class="privacy_text">© 2013 | <a href="index-5.html">Privacy Policy</a></div>


                        <!-- {%FOOTER_LINK} -->
                    </div>
                </div>
     </footer>
   
    


</body>


</html>

Ah yes I'm sorry the blabla.png is the image that's meant to change, i just inserted a random name but should be 1.png and change to 2.png asf. I'm thinking this for the sequence of images:

 

 

  1. $imageNumber = $imageNumber++;
  2. if ($imageNumber>10) $imageNumber=1;
So after hitting 10.png it has to go back to 1.png.
Edited by AngeS

So if there are 123 comments it shows 3.png? Technically speaking it's ((123 - 1) % 10) + 1 = 3, which seems like overkill but with 120 comments it's ((120 - 1) % 10) + 1 = 10.

 

$comments = $db->get_comments($comment_post_ID);
You have the list of comments there and

$has_comments = (count($comments) > 0);
have already count()ed them to see if there are comments. Use that same count to figure out which image to show.

 

$image = ($has_comments ? (((count($comments) - 1) % 10) + 1) . ".png" : "default image or something if there aren't comments");
And there's your filename.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.