性格悪い人が優勝

初心者です。一度似た質問をさせていただきましたが、再度整理しましたので、質問に答えて頂けたら幸いです。
下記のm_magazine(DBのテーブル名)の中にそれぞれの月のmagazine_id(1、2、3…と番号順に登録してあります。)と月刊のurl、タイトルであるmagazine_titleなどのカラムを収納させてあります。
生成させた今月号の月刊ページには来月号のリンクと先月号のリンクを自動的に取得させたいのですが、下記のように書くと、最新号は次のようになってしまします。

次の月刊へ:2016年2月号 ○○○  前の月刊へ:2016年1月号 ○○○
次の月刊へ:2016年3月号 ○○○  前の月刊へ:2016年2月号 ○○○
次の月刊へ:2016年4月号 ○○○  前の月刊へ:2016年3月号 ○○○

つまり、最新号には過去の全部の「次の月刊へ」と「前の月刊へ」が全部出てしまいます。
次に新しい号は「次の月刊へ:2016年4月号 ○○○  前の月刊へ:2016年3月号 ○○○」は出ずに、あとは全部出てしまいます。最も古い号は逆に何も出ません。

どなたか教えて頂けませんでしょうか。よろしくお願い致します。

◆DB仕様

-- Table "m_magazine" DDL

CREATE TABLE `m_magazine` (
`magazine_id` int(11) NOT NULL COMMENT '月刊ID',
`url` varchar(256) DEFAULT NULL COMMENT 'URL',
` magazine_datetitle` varchar(256) DEFAULT NULL COMMENT '月刊年月タイトル',
`magazine_title` varchar(256) DEFAULT NULL COMMENT '月刊タイトル',
`is_deleted` char(1) NOT NULL COMMENT '削除フラグ(y, n)',
PRIMARY KEY (`magazine_id`),
KEY `is_deleted` (`is_deleted`),
KEY `create_staff_id` (`create_staff_id`),
KEY `modify_staff_id` (`modify_staff_id`),
CONSTRAINT `m_magazine_ibfk_1` FOREIGN KEY (`create_staff_id`) REFERENCES `m_staff` (`staff_id`),
CONSTRAINT `m_magazine_ibfk_2` FOREIGN KEY (`modify_staff_id`) REFERENCES `m_staff` (`staff_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='月刊管理マスタ';


◆生成に使っているPHPファイル

$currentid = $h['magazine_id'];
$sql2 = "select distinct url, magazine_datetitle, magazine_title from m_magazine where magazine_id = (select min(magazine_id) from m_magazine where magazine_id > '$currentid' order by magazine_id desc limit 1)";
$sql3 = "select distinct url, magazine_datetitle, magazine_title from m_magazine where magazine_id = (select max(magazine_id) from m_magazine where magazine_id < '$currentid' order by magazine_id desc limit 1)";
$ret2 = f_db_select($sql2, $h['url'], $h['magazine_datetitle'], $h['magazine_title']);
$ret3 = f_db_select($sql3, $h['url'], $h['magazine_datetitle'], $h['magazine_title']);
foreach ($ret2 as $h2) {
$nexturl = f_db_select($h2['url']);
$nextdatetitle = f_db_select($h2['magazine_datetitle']);
$nexttitle = f_db_select($h2['magazine_title']);
}
foreach ($ret3 as $h3) {
$prevurl = f_db_select($h3['url']);
$prevdatetitle = f_db_select($h3['magazine_datetitle']);
$prevtitle = f_db_select($h3['magazine_title']);

$wkbuff .= <<<EOF
<p>
次の月刊へ:<a href ="{$h2['url']}">{$h2['magazine_datetitle']} {$h2['magazine_title']}</a><br/>
前の月刊へ:<a href ="{$h3['url']}">{$h3['magazine_datetitle']} {$h3['magazine_title']}</a>
</p>
EOF;

A 回答 (1件)

一律で次や前を書くことはできないことは理解できてますか?


(例外処理が必要とかきましたよね?)

とりあえず、なんのCMSかわかりませんがビミョーな感じ
たとえばこんな風にidを抜き出してみては?

CREATE TABLE `m_magazine` (
`magazine_id` int(11) NOT NULL PRIMARY KEY,
`url` varchar(256) not null,
`magazine_title` varchar(256) not null,
`is_deleted` enum('y','n') not null default 'n'
);

insert into `m_magazine` values
(1,'url1','title1','n')
,(2,'url2','title2','n')
,(4,'url4','title4','n')
,(5,'url5','title5','y')
,(6,'url6','title6','n')
,(7,'url7','title7','n');

条件:
・idが必ずしも連番とは限らない
・削除フラグが'y'のモノは無視する

select
(select max(`magazine_id`) from `m_magazine` as t1 where t1.`magazine_id`<t0.`magazine_id` and t1.`is_deleted`='n') as pre_id
,t0.`magazine_id` as `this_id`
,(select min(`magazine_id`) from `m_magazine` as t2 where t2.`magazine_id`>t0.`magazine_id` and t2.`is_deleted`='n') as nxt_id
from `m_magazine` as t0
where t0.`is_deleted`='n';

これにurlとタイトルを拡張して
select `pre_id`,t4.`url` as `pre_url`,t4.`magazine_title` as `pre_title`
,`this_id`,`this_url`,`this_title`
,`nxt_id`,t5.`url` as `nxt_url`,t5.`magazine_title` as `nxt_title`
from (
select
(select max(`magazine_id`) from `m_magazine` as t1 where t1.`magazine_id`<t0.`magazine_id` and t1.`is_deleted`='n') as pre_id
,t0.`magazine_id` as `this_id`
,t0.`url` as `this_url`
,t0.`magazine_title` as `this_title`
,(select min(`magazine_id`) from `m_magazine` as t2 where t2.`magazine_id`>t0.`magazine_id` and t2.`is_deleted`='n') as nxt_id
from `m_magazine` as t0
where t0.`is_deleted`='n'
) as t3
left join `m_magazine` as t4 on `pre_id`=t4.`magazine_id`
left join `m_magazine` as t5 on `nxt_id`=t5.`magazine_id`

仮に自ページのidが4なら、t0のmagazine_idで絞って
select
`pre_id`
,t4.`url` as `pre_url`
,t4.`magazine_title` as `pre_title`
,`this_id`
,`this_url`
,`this_title`
,`nxt_id`
,t5.`url` as `nxt_url`
,t5.`magazine_title` as `nxt_title`
from (
select
(select max(`magazine_id`) from `m_magazine` as t1 where t1.`magazine_id`<t0.`magazine_id` and t1.`is_deleted`='n') as pre_id
,t0.`magazine_id` as `this_id`
,t0.`url` as `this_url`
,t0.`magazine_title` as `this_title`
,(select min(`magazine_id`) from `m_magazine` as t2 where t2.`magazine_id`>t0.`magazine_id` and t2.`is_deleted`='n') as nxt_id
from `m_magazine` as t0
where t0.`is_deleted`='n'
and t0.`magazine_id`=4
) as t3
left join `m_magazine` as t4 on `pre_id`=t4.`magazine_id`
left join `m_magazine` as t5 on `nxt_id`=t5.`magazine_id`
    • good
    • 0
この回答へのお礼

ご丁寧に回答して頂き、ありがとうございました。なんとかできました。ありがとうございます!

お礼日時:2016/04/20 16:41

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!