Display hexadecimal color with php coming from Mysql

Asked

Viewed 109 times

-2

I would like to display on a page that I am developing a hexadecimal value that is stored in my database. I did some tests, but without success.

I tried to do something like this:

<?php echo "background-color:".$ProdCor->Cor;.""; ?>

The value in the bank field is like this:

#ffff00

I’m trying to show color in a page field.

The CSS that is related to the field is this:

.tovar_color_select {padding-bottom:19px;}
.tovar_color_select p {
    margin-bottom:13px;
    text-transform:uppercase;
    font-weight:900;
    font-size:12px;
    color:#333;
}
.tovar_color_select a {
    position:relative;
    display:inline-block;
    margin-right:10px;
    width:32px;
    height:22px;
}

.tovar_color_select a:before { content:''; position:Absolute; left:-4px; top:-4px; right:-4px; bottom:-4px; border:1px Solid #e9e9e9; Transition: all 0.1s Ease-in-out; -Webkit-Transition: all 0.1s Ease-in-out; } .tovar_color_select a:Hover:before, .tovar_color_select a.active:before { border:2px Solid #333; }

The complete tag is this:

<div class="tovar_color_select"> <p>SELECIONE A COR</p> <?php foreach($ResCor as $ProdCor ) { ?> <a href="javascript:void(0);" ><?php echo "background-color:".$ProdCor->Cor; ?></a> <?php } ?>

  • What about the rest of the field code? You’re using CSS inline or inserted it into the CSS file?

  • 2

    There is a syntax error, a ; in the middle of concatenation. Also, while not causing an error in the output, concatenate a string with "" in the end is meaningless in that context. It would be enough <?php echo "background-color:".$ProdCor->Cor; ?> which may be abbreviated to <?='background-color:'.$ProdCor->Cor?> in newer versions of PHP (in older versions, it depends on configuration)

  • Hello @Bacco, thanks for the tip, but it didn’t work, I edited the question to try to show what I need.

  • @adventistapr tested my response?

  • Hello @Thiago Magalhães, also did not work.

  • Following my answer code, change the background-color: <?= $ProdCor->Cor ?> for background-color: #333. It should print a square with the specified color. Checked if the color is being returned correctly?

Show 1 more comment

1 answer

1


Try it like this:

<?php foreach($ResCor as $ProdCor ) { ?>
    <a href="javascript:void(0);">
      <div style="background-color: <?= $ProdCor->Cor ?>; width: 100px; height: 100px;"></div>
    </a>
<?php } ?>

I created a div adding the css inline, where I define the color of background that comes through the PHP and fixed its dimensions. It is important to remember to define the dimensions because if you do not put any character inside, the div will not have a width and not one height, then there would be no room for the background can fill in the desired color.

  • Thank you so much for your great help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.