Page 1 sur 1

Stopper le count

Posté : 23 sept. 2013, 20:07
par Stegue
Bonjour,

Sur ma page de citations, j'en appelle 8 par page depuis une BDD.
J'essaie d'intégrer un système de vote par étoiles comme on trouve partout. La fonction array a été modifiée pour fonctionner avec les n° ID des citations.

Le problème:
Si je n'appelle qu'une citation par page, ça marche. Mais comme je les appelle par 8, j'ai un bug.
Plutôt que d'avoir:
Citation1
note1

Citation2
note2

Etc...
J'ai:
Citation1
note1

Citation2
note1
note2

Citation3
note1
note2
note3

etc...
Je pense que c'est lié aux fonctions d'incrémentation qui servaient avec array (1,2,3)

Voici un bout de code:

Code : Tout sélectionner

<?php for($i=0;$i<count($ids);$i++) { $rating_tableName = 'ratings'; $id=$ids[$i]; $q="SELECT ratings.total_votes, ratings.total_value FROM $rating_tableName WHERE ratings.id=$id"; $r=mysql_query($q); if(!$r) echo mysql_error(); while($row=mysql_fetch_array($r)) { $v=$row['total_votes']; $tv=$row['total_value']; $rat=$tv/$v; } $j=$i+1; $id=$ids[$i]; echo'<div class="product"> Rate Item '.$j.' <div id="rating_'.$id.'" class="ratings">'; for($k=1;$k<6;$k++){ if($rat+0.5>$k)$class="star_".$k." ratings_stars ratings_vote"; else $class="star_".$k." ratings_stars ratings_blank"; echo '<div class="'.$class.'"></div>'; } echo' <div class="total_votes"><p class="voted"> Rating: <strong>'.@number_format($rat).'</strong>/5 ('.$v. ' vote(s) cast) </div> </div></div>';} ?>
$ids correspond au n° ID de la citation.

Merci pour votre aide.

Re: Stopper le count

Posté : 23 sept. 2013, 21:33
par xTG
Ce code est appelé après chaque citation ?
Car si tel est le cas tu n'as pas besoin de la première boucle for.

Re: Stopper le count

Posté : 24 sept. 2013, 12:56
par Stegue
Oui, après chaque citation.
Et pour enlever la boucle proprement ? Il suffit de la supprimer avec les { ?

Re: Stopper le count

Posté : 24 sept. 2013, 13:02
par xTG
Oui mais il faut aussi remplacer la variable $i par son équivalent (information que tu dois avoir en amont du code).

Re: Stopper le count

Posté : 24 sept. 2013, 16:20
par orenx22
Bonjour,

Savoure !!

PHP et HTML a mettre dans la même page.
<?php

// -- LIEN BASE DE DONNEES -----------------------------------------------------

$dsn = "mysql:dbname=mydb;host=127.0.0.1;port=3306" ;

$bdd = new PDO( $dsn, 'root', null );

// -- REQUETE SQL --------------------------------------------------------------

/*

Table SQL citation :

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`citation`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`citation` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `auteur` VARCHAR(45) NULL ,
  `valeur` TEXT NULL ,
  `vote` INT NULL DEFAULT 0 ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;

USE `mydb` ;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

 */

$sql = "SELECT * FROM citation ORDER BY vote ASC" ;

$req = $bdd-> prepare( $sql );

$req-> execute();

$result = $req-> fetchAll( PDO::FETCH_OBJ );

// -- CODE HTML ----------------------------------------------------------------

$step = 8 ;
$i = 0 ;
$j = 0 ;
$k = 0 ;

$btn = ceil( count( $result ) / $step ) ;

?>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Citations</title>
</head>
<body>
    
<script>
function voir( Id ) 
{
    document.getElementById( 'frame-0' ).style.display = "" ;
    
    for( i = 0 ; i < <?php echo $btn ?> ; i++ )
    {
       document.getElementById( "frame-" + i ).style.display = "none" ;
    }
    
    document.getElementById( "frame-" + Id ).style.display = "" ;
}
</script>

<div id="contenu">
<?php if( !empty( $result ) ): ?>

 
<ul>Page :
<?php for( $k == 0 ; $k < $btn ; ++$k ): ?>
<li style="display: inline;"><a href="javascript:voir('<?php echo $k ?>');"><?php echo $k ?></a></li>
<?php endfor ?>
</ul>
    
<div id="frame-<?php echo $i ?>">
<?php foreach( $result as $citation ): ?>
    
<div id="citation">
<div id="auteur"><?php echo $citation-> auteur ?></div>
<div id="valeur"><?php echo $citation-> valeur ?></div>
<div id="vote"><?php echo $citation-> vote ?></div>
</div>

<?php ++$j ?>
<?php if( $j > $step ): ?>
</div>

<div id="frame-<?php ++$i ; echo $i ?>" style="display:none;">
<?php $j = 0 ; ?>
<?php endif ?>
<?php endforeach ?>
<?php else: ?>
<span>Aucune citation enregistrée</span>
<?php endif ?>
</div>
</div>
    
</body>
</html>

reste plus qu'à mettre en place un système de requête pour ajouter un vote à la citation. Par exemple récupérer une clé de la vairable $_GET pour récupérer l'id de la citation et d'ajouter +1 avec une requête sql genre :
UPDATE citation SET vote = vote + 1 WHERE id = ?
Ciao ++

Re: Stopper le count

Posté : 28 sept. 2013, 08:05
par Stegue
J'ai pu m'en sortir avec un peu de tout ça, merci.