根据Mysql结果更改样式(即 - 返回的每个名称)

QUICK SHORT VERSION.

i want to color code the results of a mysql query for each name returned so that i can have individual colors for each message sender. this link gives an example of what im looking for.

How to create dynamic color list for charts

Im fairly New to Mysql and PHP and i am trying to do a message service / chat and i wish to have individual Class/style for each members message , so each sender has their own colored div/name/message.

So that a user can see clearly the individual messages per Sender.

Otherwise every message is the same color and this gets a little difficult to see which member sent the messages. So i am trying to code a way around this and have Different Styling for each members message..

The Problem i am having is adding a css class depending on what name has been returned from the query..

Can anybody help me Either with an Array assigned to individual colors for the individual users, Or any other methods..

Iv done the brute force method and i will show you the code, but what i really require is a sound work around so i can add different Css Classes to each Message senders Message...

Example... 3 senders - Melissa - Robert - Paul -- They each have a message box. But i need each box to be individually colored according to what names they have.

Example in an array - if ($name = $namereturned[$i]) { css=[$colr] }

The main problem i have found is not knowing what names will be returned, otherwise i could code a solution..

But as the names are returned randomly depending on who sent the message, i require a method that will assign a color class to each result based on what name is returned, then i can add the class according to the name. I hope this makes some sense...

Basically , if you where trying to assign a unique color coded cell for each message sender how would you do it.... Using an array...

    $db3= new PDO("mysql:host=localhost;dbname=messages", 'root', ''); // 1. set database with this instead of conect - or change conect to this

        $query3="SELECT * FROM `messagedatabase` WHERE `sendto` = ? AND `listID` = ? GROUP BY `messageaccountname` ORDER BY `messagedate`,`messageaccountname`";


        $stat3=$db3->prepare($query3);

        $stat3->execute(array("$accountname","$listID"));

        $senderCount=$stat3->rowCount();

        while($row = $stat3->fetch()){



            $col=$col+1;
                if($col > $senderCount  || $col>5){$col=1;}
            if ($col==1){$colr="#e91e63";}
            if ($col==2){$colr="#00d469";}
            if ($col==3){$colr="#4d90fe";}
            if ($col==4){$colr="deeppink";}
            if ($col==5){$colr="#ff9900";}


            $messagesender=$row['messageaccountname'];
            echo"<p class='fromdata' style='color:{$colr}'>{$messagesender}</p>";   

        }

i wish the result to be like this...

while($row = $stat3->fetch()){

     RESULT1(in red)      RESULT2(in blue)      RESULT3(in green)  

}

Then for the messages themselves...

 while($row = $stat4->fetch()){

     RESULT1 message(in red)      RESULT2 message (in blue)      RESULT3 message (in green)  

}

I simply require a method to do this efficiently . I would also like to assign a class or style to each result , but each class/style is unique for each message sender... so that all sender1's messages are in red - sender1's messages are in blue - sender1's messages are in green - and so on..

its a simple question, but a difficult problem.

But im sure a professional will be along eventually to enlighten us all ;)

if you wish to see the code for the output its below... It might help direction.

thank you all.

//test
        $view="All";

            $db3= new PDO("mysql:host=localhost;dbname=messages", 'root', ''); // 1. set database with this instead of conect - or change conect to this

            if ($view==="All"){ $query3="SELECT * FROM `messagedatabase` WHERE `sendto` = ? AND `listID` = ? ORDER BY `messagedate`,`messageaccountname` DESC";}
            else if ($view==="Group"){$query3="SELECT * FROM `messagedatabase` WHERE `sendto` = ? AND `listID` = ? GROUP BY `messageaccountname` ORDER BY `messagedate` DESC"; }


            $stat3=$db3->prepare($query3); 

            $stat3->execute(array("$accountname","$listID"));

            while($row = $stat3->fetch()){


            $messagesender=$row['messageaccountname'];//message sender 

            if ($lastnamed !== $messagesender){$colorchange=$colorchange+1;}

            $lastnamed=$messagesender;

            if ($colorchange>$senderCount || $colorchange>5 ){$colorchange=1;}
            if ($colorchange==1){$cols="#e91e63";}
            if ($colorchange==2){$cols="#00d469";}
            if ($colorchange==3){$cols="#4d90fe";}
            if ($colorchange==4){$cols="deeppink";}
            if ($colorchange==5){$cols="#ff9900";}
            if ($colorchange==6){$colr="#3f51b5";}
            if ($colorchange==7){$colr="#4bd6d2";}
            if ($colorchange==8){$colr="#bed64b";}
            if ($colorchange==9){$colr="#858484";}
            if ($colorchange==10){$colr="#ff8a07";}




            $messagedate=$row['messagedate'];
            $messagedate = date('M-d-Y @ H:i', strtotime($messagedate));
            $sellermessage=$row['sellermessage'];
            $buyermessage=$row['buyermessage'];
            $listID=$row['listID']; 
            $listingID3=$listID;
            $sendto=$row['sendto'];
            $sendto=$row['sendto'];
            $messageid=$row['id'];
            $readornot=$row['read'];



            if (empty($sellermessage)){ $unreadmessage=$buyermessage;} 
            else if (empty($buyermessage)){$unreadmessage=$sellermessage;}
            else 
            { $unreadmessage="Empty Message";}


        echo"
            <div id='kill{$messageid}' class='messagedatabox' style='border:2px solid {$cols}' "; if ($readornot=="no") { $unreadcount=$unreadcount+1; echo" style='opacity:1;' "; } echo" >    


            <div class='replybox , hidereplybox'  id='replybox{$messageid}' >
                <textarea class='replyboxtextarea' placeholder='Type A Quick Reply Here' contenteditable='true' title='Quick Reply' value='' ></textarea>
            <div class='sendquickreply' id='sendreply{$messageid}' onclick'sendreply({$messageid});>Send Reply</div>
                </div>



            <div class='replymessage' id='delete{$messageid}' data-variable-mid='{$messageid}' onclick='replymessage({$messageid});' title='Send A Quick Reply To {$messagesender}'> </div>

            <div class='messagesender'  id='' style=' border:2px solid {$cols}; border-bottom:0; color:{$cols}; text-shadow: 0px 1px 0px #858484;'><i class='fas fa-user' style='margin-right:4px;color:{$cols};'></i>{$messagesender}</div>
                        "; if ($readornot=="no") { echo " <div class='unreadalert'><i class='fas fa-exclamation-triangle' style='font-size:20px' ></i></div> ";} 
                  echo"  <div class='messagetitle' id=''>Item: </div><span class='titledata' >{$title}</span><span class='sellername' title='{$sendto}' style='border:2px solid {$cols}; border-bottom:0;' >To: You...</span>
                  ";
                  if ($readornot==="no")
                                        {

                                        echo " 
                                            <div class='waiting' title='{$sendto} Has Not Yet Viewed Your Message'></div>
                                        ";}else 
                                        if ($readornot==="yes")
                                        { echo " 
                                            <div class='seen'><i class='fas fa-check' style='margin-right:3px;'></i><span style='font-size:16px;'  title='You Have Viewed This Message'  >Seen</span></div>
                                        ";}


                    echo"
                         <span class='messagedate' ><span style='color:#00d469'>Sent </span><i class='fas fa-clock' style='margin-left:2px; margin-right:5px; color:#3F51B5;'></i>{$messagedate}</span>
                        <div class='messagetext' id=''>Message: </div><span class='messageinfo'>{$unreadmessage}</span>


                        "; if ($readornot=="no") { echo " <div class='gototext'><a href='vbayshowlisting.php?listID={$listID}' style='position:relative; margin-right:24px; top:2px; color:black;'>UNREAD MESSAGE</a></div> ";} 
                        else if ($readornot=="yes") { echo " <div class='gototext'><a href='vbayshowlisting.php?listID={$listID}'>Goto Message</a></div> ";}
                        echo "<div class='gotolistid' ><a href='vbayshowlisting.php?listID={$listID}' style='color:#00d469;'>{$messagesender} Was {$lastloggedin}</a></div>
            </div>
                            ";

            }//WHILE

You can use md5 to generated 6 character color code

$color = substr(md5($messagesender), 0, 6);

Your code will looks like

echo "<p class='fromdata' style='color:#{$color}'>{$messagesender}</p>";

There is a case if the messagesender names are same in this case you can reverse the duplicated name using function strrev