PHPの素人なので見苦しいコードかもしれませんがよろしくお願い致します。
PHPを使ってMySQLにアクセスして出力させるプログラムを書いているのですが
その際に<li class="0count_li">(2)</li>
<li class="1count_li">(5)</li>
のように出力させてからCSSで別々に表示のさせ方を変更出来るようにしたいのですが
(CSSは別のファイルのstyle.cssに入力しても直接同じファイルに入力しても結果は
同じでした。)
CSSを
li.1count_li {color: red;}
や.1count_li {color: red;}
としても効果がありません。
どうしたら別々にコントロール出来るでしょうか?
ご教授何卒お願いします。
<html>
<head>
<meta charset="utf-8" />
<title>不動産サイト</title>
<link rel="stylesheet" type="text/css" href="../common/style.css"/>
<style type="text/css">
li.1count_li {
color: red;
}
</style>
<!--フォーム完全クリア-->
<script>
function resetValue(f){
for (var i=0;i<f.length;i++){
if(f[i].type=="text") f[i].value="";
if(f[i].type=="select-one") f[i].selectedIndex=0;
if(f[i].type=="checkbox") f[i].checked=false;
}
}
</script>
<!--フォーム完全クリアEND-->
</head>
<body>
<div id="main">
<form name="search_form" action="list2.php" method="post" >
<input type="hidden" name="cmd" value="search" />
<table>
<tr>
<th>マンション名</th>
<td><input type="text" name="hotel_name" value="<?php print( htmlspecialchars
( $_POST["hotel_name"] ,ENT_QUOTES ) ) ?>" size="20"></td>
</tr>
<tr>
<th>価格帯</th>
<td>
<input type="text" name="price_min" value="<?php print( htmlspecialchars
( $_POST["price_min"] ,ENT_QUOTES ) ) ?>" size="8"> -
<input type="text" name="price_max" value="<?php print( htmlspecialchars
( $_POST["price_max"] ,ENT_QUOTES ) ) ?>" size="8"><br />
</td>
</tr>
<tr>
<th>住所</th>
<td><input type="text" name="address" value="<?php print( htmlspecialchars
( $_POST["address"] ,ENT_QUOTES ) ) ?>" size="20"></td>
</tr>
<tr>
<th>名古屋市</th>
<td>
<?php
$dsn = 'mysql:dbname=xxxx;host=xxxxx';
$user = 'xxxx';
$password = 'xxxxxx';
$dbh = new PDO($dsn, $user, $password);
$dbh->query('SET NAMES utf-8');
$sql = "SELECT hotels.area_id,area.ward, Count(id) AS cnt
FROM hotels
INNER JOIN area ON hotels.area_id = area.area_id
GROUP BY hotels.area_id";
$condition = array();
$statement = $pdo->prepare( $sql );
$statement->execute( $condition );
$results = $statement->fetchAll();
$i = 0;
foreach((array)$results as $result ){
if(($i >= 0)){
echo '<li class="'.$i.'count_li">' . '('.$result['cnt'].')' . '</li>';
}
$i++;
}
?>
//////////以下は質問以外の部分、回答していただく方に全体像がわかりやすいかもしれないので表記します。
以下は外部ファイルの別のSQL文で動きます。
<input type="checkbox" name="area_id[]" value="1" <?php if (isset($_POST['area_id']) and in_array('1',$_POST['area_id'])) print 'checked'; ?>/>
中村区
<input type="checkbox" name="area_id[]" value="2" <?php if (isset($_POST['area_id']) and in_array('2',$_POST['area_id'])) print 'checked'; ?>/>
西区
//省略
</tr>
</table>
<input type="submit" value="検索" class="Btn-gray button">
<input type="button" value="リセット" class="Btn-reset button" onClick="resetValue(this.form)">
</form>
<br>
<?php
//hotel_name,price_min,price_max,addressの値があれば<h1>検索結果</h1>表示////////////
if(!empty($_POST["hotel_name"]) or !empty($_POST["price_min"]) or
!empty( $_POST["price_max"] ) or !empty( $_POST["address"] ) or !empty( $_POST["area_id"] )){
echo "<h1>検索結果</h1>";
}
//hotel_name,price_min,price_max,addressの値があれば<h1>検索結果</h1>表示////////////
if(!empty($_POST["hotel_name"]) or !empty($_POST["price_min"]) or !empty( $_POST["price_max"] ) or !empty( $_POST["address"] ) or !empty( $_POST["area_id"] )){
echo "<table border=\"1\" class=\"table_result\">";
/* <tr>
<th></th>
<th>物件名</th>
<th>住所</th>
<th>宿泊料金</th>
</tr>";*/
}
?>
<?php
foreach((array)$results as $result ){
?>
<tr>
<td><img src="<?php echo($PHOTODIR)."/" ?><?php if (!empty( htmlspecialchars($result["photofilename"], ENT_QUOTES )) ) { print $result["photofilename"] ;}
else { print "noimage.jpg" ;}?>" /></td>
<td><p class="h_name"><?php print( htmlspecialchars( $result["hotel_name"], ENT_QUOTES ) ); ?></p><br>
<p><?php print( htmlspecialchars( $result["pref"], ENT_QUOTES ) ); ?>
<?php print( htmlspecialchars( $result["address"], ENT_QUOTES ) ); ?><br>
\<?php print( htmlspecialchars( number_format( $result["price"] ), ENT_QUOTES ) ); ?></p>
</td>
<td></td>
<td></td>
</tr>
<?php
}
?>
<?php
//hotel_name,price_min,price_max,addressの値があれば</table>表示////////////
if(!empty($_POST["hotel_name"]) or !empty($_POST["price_min"]) or !empty( $_POST["price_max"] ) or !empty( $_POST["address"] ) or !empty( $_POST["area_id"] )){
echo "</table>";
}
?>
</div>
</body>
</html>
コード
余分な部分が多いと思いますが以下CSS全体です
style.css
/* CSS Document */
body{
background-color:#F9F9F9;
font-size:80%;
color:#333;
padding:30px 0 0 30px;
}
div,p,ul,li,dl,dt,dd,
table,tr,th,td,form,
h1,h2,h3,h4,h5,h6{
padding:0;
margin:0;
}
h1{
font-size: 130%;
margin:15px 0;
padding: 2px 0 2px 3px;
border-left:#008000 5px solid;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
input,textarea,select,option {
border:#999 1px solid;
}
table{
border-top:#93C400 1px solid;
border-right:#93C400 1px solid;
border-left:#93C400 1px solid;
}
th,td{
padding: 5px 10px;
border:#93C400 1px solid;
}
th{
background:#E9F3CC;
font-weight:normal;
}
td{
background-color: #F8FBEC;
}
table.table_result p {
line-height: 1.8em;
}
p.h_name {
margin-top: -35px;
}
ul li{
list-style:none;
}
a img{
border:#CCC 1px solid;
}
.red{
color:#C00;
}
.blue{
color:#30F;
}
ul.check_ul li {
float: left;
}
.green{
color:#030;
}
span.space1 {
margin-left: 13px;
}
span.space2 {
margin-left: 12px;
}
span.space3 {
margin-left: 13px;
}
span.space4 {
margin-left: 2px;
}
input.button{
text-align:center;
font-size:90%;
width:50px;
margin:10px auto 0;
}
.Btn-gray {
color: #333;
border: 1px solid #AAA;
cursor: pointer;
background: #EEE;
padding:3px 12px;
}
.Btn-reset.button {
color: #333;
border: 1px solid #AAA;
cursor: pointer;
background: #EEE;
padding: 3px;
}
.Btn-gray:hover {
color: #666;
background: #FFF;
}
#S01 #main{
width:400px;
}
#S01 form{
text-align:center;
}
#S01 table{
width:100%;
text-align:left;
}
#S01 table th{
}
#S01 table td{
width:65%;
}
#S01-03 #main{
width:500px;
}
#S01 p{
padding:0px;
margin:20px;
}
#S01-03 table{
width:90%;
text-align:left;
margin:20px;
}
#S02{
width:500px;
}
#S02 .view dt{
margin-top: -10px;
position:relative;
bottom:-140px;
background-color:#FFF;
opacity: 0.6;
filter: alpha(opacity=60);
font-weight:bold;
font-size:90%;
padding: 5px;
width:490px;
text-align:left;
z-index:1;
}
#S02 .view dd{
width:500px;
position:absolute;
margin-top: -10px;
}
#S02 .view .text{
position:relative;
top:150px;
padding: 0 5px;
}
#S02 .view .text p{
padding: 0 0 10px;
}
#S02-10 #main{
width:500px;
}
#S04-04 #main{
width:500px;
}
#S04-04 p{
padding:0px;
margin:20px;
}
li.1count_li {
color: red !important;
}
li.1count_li {color: red !important;}
コード
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+2
クラス名が「1count_li」と数字で始まっているのが原因です。
それと、○番目の要素にスタイルを適用するという用途なら
擬似クラスもおすすめです。
li:first-child {color: red;} /* liの1つ目を赤文字に */
li:nth-child(2) {color: blue;} /* liの2つ目を青文字に */
li:nth-child(2n) {font-weight: bold;} /* 偶数行を太字に */
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
0
他のHTMLやCSS全体がわからないのですが、!important
を指定してみてはいかがでしょうか。
li.1count_li {color: red !important;}
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 89.99%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正、ベストアンサー選択の依頼
te2ji
2016/04/03 21:49
質問は**「CSSが <li class="1count_li">(5)</li> に適用されない」**で良いですか?
タイトルと内容が合致していないため、修正された方がよいかと。
CSSが適用されない原因はPHPと関係がないと思われますので、出力されたHTMLソースを提示する必要があります。HTMLソースを提示して下さい。