我认为我的代码可以更容易,但我不确定。如果你有的话,请看看并告诉我一些替代方案。这段代码我用来显示有关电影的信息
$sql='SELECT DISTINCT id,title,img,description,adder,added,
GROUP_CONCAT(DISTINCT cid,"-",caty ) AS caty,
GROUP_CONCAT(DISTINCT oid,"-",obs,"-",face,"-",rola,"-",typ) AS obs
FROM film
LEFT JOIN f_o ON f_o.f_id = film.id
LEFT JOIN obs ON f_o.o_id = obs.oid
WHERE film.id ='.$fid;
$wynik=mysql_fetch_assoc(mysql_query($sql));
if(isset($wynik['id'])){
echo '<pre>';
print_r($wynik);
echo '</pre>';
////
$array = explode(',', $wynik['obs']);
$r=array();//director - 0
$s=array();//Screenwriter - 1
$ak=array();//actors - 2
$akn=array();//actors 2 plan - 3
$np=array();//From Idea By - 4
$p=array();//producers - 5
$m=array();//music - 6
foreach ($array as $item)
{
$a = explode('-', $item);
if( $a[4] == 0 )
{
$r[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
}
elseif($a[4] == 1 )
{
$s[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
}
elseif($a[4] == 2 )
{
$ak[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
}
elseif($a[4] == 3 )
{
$akn[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
}
elseif($a[4] == 4 )
{
$np[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
}
elseif($a[4] == 5 )
{
$p[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
}
elseif($a[4] == 6 )
{
$m[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
}
}
function dzielperson($data){
$i = 0;
$ile=count($data);
while ($i < $ile) {
$a = explode(",", $data[$i]);
$caty='<a href="/person/'.dolink($a[1]).'-'.$a[0].'" class="link1">'.$a[1].'</a>'.($i==($ile-1) ? '':', ');
$i++;
}
return $caty;
}
echo '<br>Title: '.$wynik[title];
echo '<br>Desription: '.$wynik[description];
echo '<br>directors: '.dzielperson($r);
echo '<br>screenwriters: '.dzielperson($s);
echo '<br>actors: '.dzielperson($ak);
echo '<br>actors 2 plan: '.dzielperson($akn);
echo '<br>From Idea By '.dzielperson($np);
echo '<br>Producers: '.dzielperson($p);
echo '<br>Music: '.dzielperson($m);
}
这是mysql输出:
Array
(
[id] => 1
[title] => Pirates
[img] => /images/1page_img1.jpg
[description] =>
[adder] => baambaam
[added] => 1349387322
[obs] => 1-aktor1-foto.jpg-shrek-3,2-aktor2-foto2.jpg-batman-0,3-aktor3-f1.png-Pirat-1,4-aktorzyna4-f2.png-Pirat 3-1
)
表格结构:
film:id,title img,description,adder,added
obs:oid,obs,face,rola,typ
f_o:f_id,o_id
在栏目栏中我有演员,导演......的名字。
这不是完整的代码,但我希望你明白
答案 0 :(得分:2)
$sql='SELECT DISTINCT id,title,img,description,adder,added
FROM film
WHERE film.id ='.$fid;
$wynik=mysql_fetch_assoc(mysql_query($sql));
if(isset($wynik['id'])){
// you should use constants instead of just if(type==_some_meaningless_number_):
$personTypeMap = array(
'r' //director - 0
,'s' //Screenwriter - 1
,'ak' //actors - 2
,'akn' //actors 2 plan - 3
,'np' //From Idea By - 4
,'p' //producers - 5
,'m' //music - 6
);
// so above should be something like:
// define('PERSON_TYPE_DIRECTOR', 0);
// define...
// then you wouldn't need that array-map above as well as would be easier to understand who is what
// init all people subarrays:
$people = array_fill_keys($personTypeMap, array());
$sql='SELECT oid, obs, typ #add other fields if you actually use them
FROM f_o
INNER JOIN obs ON f_o.o_id = obs.oid
WHERE f_o.f_id ='.$fid;
$peopleResult = mysql_query($sql);
while ($person=mysql_fetch_object($peopleResult)) {
$people[$personTypeMap[$person->typ]][] = dzielperson($person);
}
function dzielperson($person){
return '<a href="/person/'.dolink($person->obs)."-{$person->oid}\" class=\"link1\">{$person->obs}</a>";
}
// join people in all categories through comma:
foreach ($people as &$category) {
$category = implode(', ', $category);
}
echo '<br>Title: '.$wynik['title'];
echo '<br>Desription: '.$wynik['description'];
echo '<br>directors: '.$people['r'];
echo '<br>screenwriters: '.$people['s'];
echo '<br>actors: '.$people['ak'];
echo '<br>actors 2 plan: '.$people['akn'];
echo '<br>From Idea By '.$people['np'];
echo '<br>Producers: '.$people['p'];
echo '<br>Music: '.$people['m'];
P.S。我正在修理你的代码有一个原因:你原来让我笑了10分钟不停:)谢谢。
P.P.S。我留下了一些原始的混乱,但把它作为一个机会来了解你的代码有什么问题,并试着自己简化。
P.P.P.S。是的,在这种情况下,多个查询比单个怪物收集单行中不相关的东西更好。
答案 1 :(得分:1)
从您的foreach开始的代码缩短版
$result = array(
array(),
array(),
array(),
array(),
array(),
array(),
array()
);
foreach ($array as $item)
{
$a = explode('-', $item);
$result[ $a[4] ][] = $a[0].','.$a[1].','.$a[2].','.$a[3];
}
function dzielperson($data){
$i = 0;
$ile=count($data);
while ($i < $ile) {
$a = explode(",", $data[$i]);
$caty='<a href="/person/'.dolink($a[1]).'-'.$a[0].'" class="link1">'.$a[1].'</a>'.($i==($ile-1) ? '':', ');
$i++;
}
return $caty;
}
echo '<br>Title: '.$wynik[title];
echo '<br>Desription: '.$wynik[description];
echo '<br>directors: '.dzielperson($result[0]);
echo '<br>screenwriters: '.dzielperson($result[1]);
echo '<br>actors: '.dzielperson($result[2]);
echo '<br>actors 2 plan: '.dzielperson($result[3]);
echo '<br>From Idea By '.dzielperson($result[4]);
echo '<br>Producers: '.dzielperson($result[5]);
echo '<br>Music: '.dzielperson($result[6]);
dzielperson函数根本没有修改过。
初始化$ result的简化语法:
$result = json_decode('[[],[],[],[],[],[],[]]');
如果您希望旧名称具有可读性:
$names = array_flip(array('r', 's', 'ak', 'akn', 'np', 'p', 'm'));
echo '<br>directors: '.dzielperson($result[$names['r']]);
echo '<br>screenwriters: '.dzielperson($result[$names['s']]);
echo '<br>actors: '.dzielperson($result[$names['ak']]);
echo '<br>actors 2 plan: '.dzielperson($result[$names['akn']]);
echo '<br>From Idea By '.dzielperson($result[$names['np']]);
echo '<br>Producers: '.dzielperson($result[$names['p']]);
echo '<br>Music: '.dzielperson($result[$names['m']]);