duanfuchi7236
duanfuchi7236
2016-12-14 11:51

一个数据Php / SQL的不同样式

已采纳

I'm trying to create a word cloud with PHP, CSS and SQL. The user can enter any word and it will be integrated into the word cloud.

The problem is that the word cloud works with CSS li class (the words are separated by text transformation, color, size etc ...).

Below is my attempt. The problem is everything is repeated and I don't know how I can put every word from my SQL database in the different class of <li> (see the screenshot).

<!DOCTYPE html>
<html>
<head><link rel="stylesheet" type="text/css" href="wordcloud.css">
<title>Nuage de mot</title>
</head>
<body>
<?php 
$bdd = new PDO('mysql:host=localhost;dbname=wordcloud', 'root', 'root');

$reponse = $bdd->query('SELECT * FROM form_element');
$donnees = $reponse->fetch();

?>
<ul class="word-cloud">
<?php while ($donnees = $reponse->fetch())
{
    ?>
    <li class="word-cloud__word  word-cloud__word--x-small"><?php echo $donnees['mot']?></li>
    <li class="word-cloud__word  word-cloud__word--small"><?php echo $donnees['mot']?></li>
    <li class="word-cloud__word  word-cloud__word--large"><?php echo $donnees['mot']?></li>
    <li class="word-cloud__word"><?php echo $donnees['mot']?></li>
<?php 
}
$reponse->closeCursor();
?>
</ul>
<div>
<a href="form.html"><input type="button" value="Ajouter un nouveau mot"></a>
</div>
</body>
</html>

enter image description here


This part is ok, when I put word in my form, they are in the cloud.

But now you were right, I have to integrate the notion of occurrences...

Words repeated xx times have to be bigger...

I have to change my database ?

I thought to create a column "repeated words" and create an if function in my form. But I don't know if it possible for the script to recognize, the word for example "hello", the number of times it is present in my colum "repeated word".

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dtcd27183 dtcd27183 5年前

    Here, just a idea how you can do it.

    $counter = 0;
    <?php while ($donnees = $reponse->fetch())
    {
    
    $counter++;
    
    if($counter == 1) { ?>
     <li class="word-cloud__word  word-cloud__word--x-small"><?php echo $donnees['mot']?></li>
    <?php }
    if($counter == 2) { ?>
     <li class="word-cloud__word  word-cloud__word--small"><?php echo $donnees['mot']?></li>
     <?php }
    if($counter == 3) { ?>
     <li class="word-cloud__word  word-cloud__word--large"><?php echo $donnees['mot']?></li>
    <?php }
    if($counter == 4) { ?>
    <li class="word-cloud__word"><?php echo $donnees['mot']?></li>
    <?php $counter = 0;
    }
     ?>
    

    May there are some syntax errors with the <?php ?> havent checked it. But I think you should know what I mean and you'll find the error by yourself if there are some. ;)

    点赞 评论 复制链接分享
  • doumanshan6314 doumanshan6314 5年前

    Suppose The Following Case :

    row < 10 then small font
    row > 10 and row < 20 then medium font
    row > 20 then large font
    

    Step 1:

    Count The Total Row From The Database Which You Want to Categorized. For Example :

    $response = $bdd->query('SELECT count(id) AS total_row FROM form_element');
    

    Now Suppose You Get 17 Records.

    So In PHP. Do It Like This ;

    Step 2 :

    if($donnees['total_row'] < 10 ){
        echo "<li class="word-cloud__word  word-cloud__word--x-small"><?php echo $donnees['mot']?></li>";    // Output Small Font
    }
    else if($donnees['total_row'] > 10 && $donnees['total_row'] < 10 ){
        echo " <li class="word-cloud__word  word-cloud__word--small"><?php echo $donnees['mot']?></li>";    // Output Medium Font
    }
    else if($donnees['total_row'] > 20 ){
        echo " <li class="word-cloud__word  word-cloud__word--large"><?php echo $donnees['mot']?></li>";    // Output Large Font
    }
    else{
        echo " <li class="word-cloud__word "><?php echo $donnees['mot']?></li>";    // Output Same Font
    }
    
    点赞 评论 复制链接分享