2011年3月7日月曜日

3/7覚書

1時間目
二次元配列の肝がキタ━━゚+.ヽ(≧▽≦)ノ.+゚━━ ッ ! ! !
kuku.php

require( "./libs/Smarty.class.php" );
$smarty = new Smarty();

for ($i = 1; $i < 10; $i++) {
 for ($j = 1; $j < 10; $j++) {
  //二次元配列の生成
  $num[$i][] = $i*$j;
  }
}

$smarty -> assign( "num", $num );
$smarty -> display("kuku.tpl");

kuku.tpl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8" />
</head>
<body>

<table border=1>
<caption>九九の段・二次元配列</caption>
 <tr>
 {foreach from = $num item = n}
  {foreach from = $n item = c}
  <td height="25" width="25" align="center">
  {$c}
  </td>
  {/foreach}
 </tr>
 {/foreach}
</table>
</body>
</html>
$num[$i][]にすることで二次元配列となる。
[$i]
☆1~9までの数字が入る
[]
☆入れ子のforが回るたびに$i*$jの値が追加されていく
$num[$i][1(1*1)][2][3][4][5].....
[$i][2(2*1)][4][6][8][10].....
[$i][3(3*1)][6][9][12][15].....



2時間目
二次元配列を元にBBSのreplyも出すよー。

方法は二つ。
whileを入れ子にする。
→この方法はやぎーに聞いてくだしあ;
whileを入れ子にしない。
おいらはこっち。
【必須】commentのidとreplyのidを紐付ける。

index.php
require( "./libs/MySmarty.class.php" );

//mysql に接続する
require_once './db_connect.php';

// クエリを送信する
$sql = "SELECT * FROM comment where delete_flg = 0 order by created_at desc";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
 $data[] = $row;
}

//クエリを送信する(返信テーブル)
$sql = "SELECT * FROM reply where delete_flg=0 order by created_at desc";
$result1 = mysql_query($sql);

while ($row2 = mysql_fetch_array($result1)) {
 $repdata[] = $row2;
}

$smarty = new MySmarty();
$smarty -> assign( "index_list", $data );
$smarty -> assign( "reply_list", $repdata );
$smarty -> display( "index.tpl" );

//print_r($data);
//print_r($repdata);

index.tpl
<!-- 投稿内容 -->
{{foreach from = $index_list item =num}}
 <div style="border:1px solid #ddd;margin-bottom:10px;padding:5px;">
 {{if $num.url neq NULL}}
 <a href="{{$num.url}}">{{$num.nickname}}</a> さんの投稿
 {{else}}{{$num.nickname}}さんの投稿
 {{/if}}
  
 {{if $num.mail1 neq NULL}}
 <a href="mailto:{{$num.mail1}}">メール</a>
 {{/if}}
   <a href="./delete.php?comment_id={{$num.id}}">削除</a>
 <br>
 {{$num.content}}
 <br>
 {{if $num.photo neq NULL}}
 <IMG SRC="./img/{{$num.photo}}" width=200>
 {{/if}}
 <br>
 <br>
 {{if $num.photo2 neq NULL}}
 <IMG SRC="./img2/{{$num.photo2}}" width=200>
 {{/if}}
 <br>
 {{$num.created_at}}
 <br>
 <a href="reply.php?comment_id={{$num.id}}">返信する</a>
 </div>

 <!-- 返信内容 -->
  {{foreach from = $reply_list item =num2}}
   {{if $num2.comment_id eq $num.id}}
   {{*commentのidとreplyのidを紐付ける。*}}
 <div style="border:1px solid #ddd;margin-bottom:10px;padding:5px;
  background-color:#f3f3f3;margin-left:40px;">
 {{if $num2.url neq NULL}}
 <a href="{{$num2.url}}">{{$num2.nickname}}</a> さんの投稿
 {{else}}{{$num2.nickname}}さんの投稿
 {{/if}}
  
 {{if $num2.reply_mail neq NULL}}
 <a href="mailto:{{$num2.reply_mail}}">メール</a>
 {{/if}}
   <a href="./reply_delete.php?comment_id={{$num2.id}}">削除</a>
 <br>
 {{$num2.content}}
 <br>
 {{if $num2.reply_photo neq NULL}}
 <IMG SRC="./reply_photo/{{$num2.reply_photo}}" width=200>
 {{/if}}
 <br>
 <br>
 {{if $num2.reply_photo2 neq NULL}}
 <IMG SRC="./reply_photo2/{{$num2.reply_photo2}}" width=200>
 {{/if}}

 <br>
 {{$num2.created_at}}
 </div>
   {{/if}}
  {{/foreach}}
{{/foreach}}

3時間目
残りも片付けるよー。

4時間目
if文を使わない方式。こっちのほうがデータ処理が早い。

4次元配列
//SELECT文を発行し、commentテーブルからレコードを取得
$sql = "SELECT * FROM comment WHERE delete_flag = 0 ORDER BY created_at DESC";
$result = mysql_query($sql);

while ($comment = mysql_fetch_array($result)) {
 $row = array();
 //配列変数$rowの要素[comment]に配列$commnetが入るので、この時点で$rowは二次元配列
 $row['comment'] = $comment;
    //SELECT文を発行し、replyテーブルからレコードを取得
    $sql = "SELECT * FROM reply WHERE delete_flag = 0 AND comment_id = 
'".$comment["id"]."'ORDER BY created_at DESC";
    $result1 = mysql_query($sql);

    if($result1) {
        while ($reply = mysql_fetch_array($result1)) {
         //配列変数$rowの要素[reply]の下にもう一つ配列を作り、
         そこに配列$replyが入るので、この時点で$rowは3次元配列
            $row['reply'][] = $reply;
        }
    }
 //配列変数$rowsに3次元配列$rowを格納するので、$rowsは4次元配列
    $rows[] = $row;
}

# 多重配列の表示確認
/*
print"<pre>";
print_r($rows);
print"</pre>";
*/

$smarty = new Smarty;
$smarty->assign('rows', $rows);
$smarty->display('index.tpl');

index3.は3次元だそうな。
デザイナーの負担にならないように構築することで、Smarty本来の機能を発揮できる。

階層イメージ
$rows━━
    ┗$row
      ┣comment
      ┣reply━
          ┣0
          ┣1
$smarty.session.~でsession使えるよ。

マジでくわっちょおわり~。

" ゚☆,。・:*:・゚★o(´▽`*)/♪Thanks♪\(*´▽`)o゚★,。・:*:・☆゚ "
" ゚☆,。・:*:・゚★o(´▽`*)/♪Thanks♪\(*´▽`)o゚★,。・:*:・☆゚ "
" ゚☆,。・:*:・゚★o(´▽`*)/♪Thanks♪\(*´▽`)o゚★,。・:*:・☆゚ "