0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

php 掲示板 ユーザー一覧画面の作成途中

Last updated at Posted at 2023-04-21

やっていること

・php学習のため掲示板を作成中
・現在登録しているユーザーを一覧を下記ソースコードで表示したいのですが、
 受け取る変数がわからず試行錯誤している状況です。
・foreach文が機能していない。
 →$usersの定義がうまくできていない

お気づきの点がございましたらコメントいただけますと幸いです。

<?php
// ユーザー一覧画面
require('./dbconnect.php');
//👇Warningを非表示にする
//error_reporting(0);

session_start();

if (isset($_SESSION['id']) && $_SESSION['time'] + 3600 > time()) {
	// ログインしている
	$_SESSION['time'] = time();

	$members = $db->prepare('SELECT * FROM members WHERE id=?');
	$members->execute(array($_SESSION['id']));
	$member = $members->fetch();
} else {
	// ログインしていない
	header('Location: login.php'); exit();
}
// 投稿を取得する
$page = $_GET['page'];
if ($page == '') {
	$page = 1; }
$page = max($page, 1);
// 最終ページを取得する
$counts = $db->query('SELECT COUNT(*) AS cnt FROM members');
$cnt = $counts->fetch();
$maxPage = ceil($cnt['cnt'] / 5);
$page = min($page, $maxPage);
$start = ($page - 1) * 5;
$start = max(0, $start);


//案1
for ($human=1; $human<=365; $human++) {
    echo $human;
}
$users = $db->prepare('SELECT m.* FROM members m WHERE m.id=?');
$users->bindParam(1, $human, pdo::PARAM_INT);
$users->execute();


// htmlspecialcharsのショートカット
function h($value) {
	return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
// 本文内のURLにリンクを設定
function makeLink($value) {
	return mb_ereg_replace("(https?)(://[[:alnum:]\+\$\;\?\.%,!#~*/:@&=_-]+)", '<a href="\1\2">\1\2</a>' , $value);
}
?>

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>ユーザー一覧</title>

	<link rel="stylesheet" href="./style.css" />
</head>
<body>
<div id="wrap">
<div id="head">
<h1>ユーザー一覧画面</h1>
</div>

<div id="content">

<div style="text-align: right"><a href="pic.php">掲示板へ</a></div>
<div style="text-align: right"><a href="logout.php">ログアウト</a></div>
<br>

<?php foreach ($users as $user): ?>

    <div class="msg">
    <!-- list.phpは特定のユーザー一覧へのリンク -->
    <a href="list.php?member_id=<?php echo h($user['id']); ?>">
    <img src="./member_picture/<?php echo h($user['picture']); ?>" width="48" height="48" alt="<?php echo h($user['name']); ?>" />
    </a>
    
    <p><span class="name"><a href="list.php?member_id=<?php echo h($user['id']); ?>"><?php echo h($user['name']); ?></a></span>

    </div>[
<?php endforeach; ?>

<ul class="paging">
    <?php if ($page > 1) { ?>
    <li><a href="user.php?page=<?php print($page - 1); ?>">前のページへ</a></li>
    <?php } else { ?>
    <li>前のページへ</li>
    <?php } ?>
    <?php if ($page < $maxPage) { ?>
    <li><a href="user.php?page=<?php print($page + 1); ?>">次のページへ</a></li>
    <?php } else { ?>
    <li>次のページへ</li>
    <?php } ?>
</ul>


</div>
</div>
</body>
</html>

試したこと

案1では、

・案1では、1から365までのidを順番に羅列すればいいのでは?と、
 $humanを定義したが失敗

//案1
for ($human=1; $human<=365; $human++) {
    echo $human;
}
$users = $db->prepare('SELECT m.* FROM members m WHERE m.id=?');
$users->bindParam(1, $human, pdo::PARAM_INT);
$users->execute();

2023-04-21 (1).png

2023-04-21.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?