Hoi allemaal,

Ik ben nieuw met PHP en ik zit met een probleem. Ik heb op dit moment een php script van internet dat zorgt voor een comment systeem voor mijn website. In de index.php heb ik een image verwerkt. Wat ik nu wil bereiken is dat bij elke comment die wordt toegevoegd de source van de image veranderd:

1.jpg, 2.jpg, 3.jpg etc.

Ik ben zover dat ik begrijp dat ik die image source moet plaatsen in de trant van:

<img src="<?php echo $imageNumber; ?>.jpg"/>

Logisch lijkt me nu dat ik in het php element dat gaat over het verifieren van de user gegevens en als deze "true" zijn de comment toevoegt ($added = $db->add_comment($_POST);), aan moet geven dat de waarde van $imageNumber ++ moet krijgen. Zo simpel blijkt het echter niet te zijn, alhoewel het mijns insziens juist wel erg makkelijk moet zijn. Ik kom er echter niet uit, leek die ik ben. Iemand wellicht een helpende hand?
Hoi,

een php script duurt hooguit enkele seconden, als de pagina wordt opgevraagd bij de server dan wordt deze door php even in elkaar gezet. je kunt dan een teller nemen in je script: $imageNumber++; maar je zult die waarde wel moeten opslaan en de volgende keer moeten inlezen omdat deze (geheugen)variabele $imageNumber weer weg is zodra het script beƫindigd wordt.

dat opslaan kan in een bestand of in de database, ik hoop je hiermee een beetje op weg geholpen te hebben.

Frank.
Je geeft ons te weinig informatie om te kunnen helpen.

Toon eens die $db->add_comment functie.
Als ik het goed begrijp steekt dat een nieuwe comment in de DB.

Nu; die image veranderen zal (waarschijnlijk) moeten gebeuren bij het lezen van de database. Niet bij het INSERT'en.

Toon ook eens hoe je die comments leest
(iets van while(row = mysql_fetch_assoc($res)) {...} )
Hier even de hele php file die gaat over de comments:

<?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;
}
}

?>



Toevoeging op 03/06/2013 15:55:22:

Het weergave stuk in mijn index (heb het geheel even tussen <?php?> gezet voor de weergave:

<?php <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>
?>

En dan als laatste het script voor de toevoeging van een comment:

<?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' );
}
?>

Reageren