CSS没有在PHP页面中工作

I can't show my CSS into PHP pages:

I have this PHP page where i wrote my CSS (stile.php):

<?php
    header("Content-type: text/css");
    $url = "http://sitename.com/files/greenbutton.png";
    $colore = "#000000";
?>
a
{ background: <?echo $url;?>; 
color: <?echo $colore;?>;   }

This is my PHP page:

<html>
<head>
<title>Pagina dei Risultati</title>
<link rel="stylesheet" type="text/css" href="http://sitename.com/stile.php"/>
</head>


<title>Pagina dei Risultati</title>
<link rel="stylesheet" type="text/css" href="http://sitename.com/stile.php"/>


<?php
$db = mysql_connect('localhost','','') or die ('Impossibile connettersi');
mysql_select_db('my_palmiano', $db) or die(mysql_error($db));
$cognome=$_POST['cognome'];
$nome=$_POST['nome'];

if(empty($cognome) & empty($nome)) { ?>
<p align="center">Si deve inserire un Nome od un Cognome.</p>
<p align="center"><a href="http://sitename.com/cercaform.html">Torna alla pagina di Ricerca.</a></p>
<?php
exit();}


$sql = "SELECT * FROM Defunti WHERE cognome LIKE '%" . $cognome .  "%' AND nome LIKE '%" . $nome .  "%' ORDER BY cognome";
$result =mysql_query($sql, $db) or die (mysql_error($db));
$contatore= mysql_num_rows($result);
?>
<div style="text-align: center;">
<?php echo '<h2>'.'Risultati Trovati: '. $contatore.'</h2>';?>
<table border="1" bordercolor="black" cellpadding="2" cellspacing="2" style="width: 100%; margin-left: auto; margin-right: auto; text-align: center;">
<tr>
    <th>Cognome</th>
    <th>Nome</th>
    <th>Nascita</th>
    <th>Decesso</th>
    <th>Et&agrave</th>
    <th>Cimitero di</th>
    <th>Settore</th>
    <th>Numero</th>
    <th>Mappa</th>
    <th>Note</th>


</tr>
<?php

function calcola_eta($ggnascita,$mmnascita,$aaaanascita,$ggdecesso,$mmdecesso,$aaaadecesso){
    if ($ggnascita==0 || $mmnascita==0 || $aaaanascita==0 || $ggdecesso==0 || $mmdecesso==0 || $aaaadecesso==0){
        return 'ND';
    }
    if ($aaaanascita==$aaaadecesso ){
       return '-';}

     if ($aaaanascita<$aaaadecesso & $mmnascita < $mmdecesso){
       return (( $aaaadecesso - $aaaanascita) - 1);}

    if ($aaaanascita<$aaaadecesso & $mmnascita > $mmdecesso){
       return $aaaadecesso - $aaaanascita;
                    }
 if ($aaaanascita<$aaaadecesso & $mmnascita==$mmdecesso){
                        if ($ggnascita >= $ggdecesso) {
           return $aaaadecesso - $aaaanascita;
        } 
        if ($aaaanascita<$aaaadecesso & $mmnascita==$mmdecesso){
                        if ($ggnascita < $ggdecesso) {
                        return (( $aaaadecesso - $aaaanascita) - 1);}
          else { return 0;}
        }
    }}
while($row=mysql_fetch_array($result)){
extract($row);


echo '<tr>';
echo '<td>' . $cognome . '</td>';
echo '<td>' .  $nome. '</td>';
echo '<td>' . $ggnascita . '/'. $mmnascita . '/'. $aaaanascita . '</td>';
echo '<td>' . $ggdecesso . '/'. $mmdecesso . '/'. $aaaadecesso . '</td>';
echo '<td>' . calcola_eta($ggnascita, $mmnascita, $aaaanascita, $ggdecesso, $mmdecesso, $aaaadecesso). '</td>';
echo '<td>' . $localita . '</td>';
echo '<td>' . $settore . '</td>';
echo '<td>' . $numero . '</td>';
echo '<td>' . '<a href="http://palmiano.altervista.org/files/'. $settore . '.pdf'.'". target="_blank">' . 'visualizza'. '</a>'. '</td>';
echo '<td>' . $note . '</td>';
}
?>

</table>
</div>
</html>

Not minding for a moment about all those ifs and query injections (gonna correct later), why doesn't it show what i want to? Link to page stile.php is correct, i am afraid that is not the right position for <link rel="stylesheet" type="text/css" href="http://sitename.com/stile.php"/> or that html is too messed up....

3个回答

Expanding on to the Short Tags mistake, which is really not one (because it works in places where short tags are enabled), your CSS has a bigger syntax mistake:

a { background: <?php echo $url;?>; color: <?php echo $colore;?>; }

The above code render it as:

a { background: http://sitename.com/files/greenbutton.png; color: #000000; }

The above is a wrong value for background. You need to use:

a { background: url('http://sitename.com/files/greenbutton.png'); color: #000000; }

So, for that, change your PHP code to:

a { background: url('<?php echo $url;?>'); color: <?php echo $colore;?>; }

Hope it helps.

Unless you've php short tags on, you cannot use <?. try this:

<?php
    header("Content-type: text/css");
    $url = "http://sitename.com/files/greenbutton.png";
    $colore = "#000000";
?>
a
  { background: <?php=$url;?>; 
color: <?php=$colore;?>;   }

You can't use PHP short_tags unless you enabled it:

PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).

From PHP 5.5 onwards, The tag <?= is always available regardless of the short_open_tag ini setting.

a
{ background: <?echo $url;?>; 
color: <?echo $colore;?>;   }

Use the full PHP tags instead:

a
{ background: <?php echo $url;?>; 
color: <?php echo $colore;?>;   }
doubaisui2526
doubaisui2526 你和我的答案有什么区别?
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐