Different style for one data Php/SQL

117 views Asked by At

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

There are 2 answers

3
Twinfriends On BEST ANSWER

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. ;)

1
Sachin Sanchaniya On

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
}