【问题标题】:PHP Function and array Syntax IssuePHP函数和数组语法问题
【发布时间】:2013-07-26 08:41:16
【问题描述】:

我真的在为一些事情苦苦挣扎,想知道是否有人可以抽出一些时间来看看这个代码块。

原来的行是这样的: $home_collectionsx=get_home_page_promoted_collections();

这会带回所有提升到主页的项目并在主页上显示它们。然而,我只是想使用相同的代码和数组函数提取 1 个项目,为此目的,id 为 5,所以我认为添加 =array(5)(array (5)) 会起作用 - 但它没有。

我希望它是一些简单的东西,或者是我遗漏或写得不正确的东西。

<?php 
if(!hook("EditorsPick")):
/* ------------ Collections promoted to the home page ------------------- */
$home_collectionsx=get_home_page_promoted_collections (array(5));
foreach ($home_collectionsx as $home_collectionx)
{
?>
<div class="EditorsPick">
<div class="HomePanel"><div class="HomePanelINtopEditors">
<div class="HomePanelINtopHeader">Editors Pick</div>
<div class="HomePanelINtopText">This is the editors pick of Asset Space...</div>
<div class="EditorsPicImage"><div style="padding-top:<?php echo floor((155-$home_collectionx["thumb_height"])/2) ?>px; margin-top: -24px; margin-bottom: -15px;">
<a href="<?php echo $baseurl_short?>pages/search.php?search=!collection<?php echo   $home_collectionx["ref"] ?>" onClick="return CentralSpaceLoad(this,true);"><img     class="ImageBorder" src="<?php echo   get_resource_path($home_collectionx["home_page_image"],false,"thm",false) ?>" width="<?php echo $home_collectionx["thumb_width"] ?>" height="<?php echo $home_collectionx["thumb_height"] ?>" /></div>
</div></div>
</div>
</div>
</div>
<?php
}
endif; # end hook homefeaturedcol
?>

这是上面代码连接到的数据库本身的函数……

function get_home_page_promoted_collections()
{
return sql_query("select   collection.ref,collection.home_page_publish,collection.home_page_text,collection.home_page_image,resource.thumb_height,resource.thumb_width from collection left outer join resource on collection.home_page_image=resource.ref where collection.public=1 and collection.home_page_publish=1 order by collection.ref desc");
}

任何帮助将不胜感激:-)

非常感谢 丰富

【问题讨论】:

  • array() 不是函数(尽管语法看起来很像)。而且你不能神奇地将参数传递给该函数并期望它们做某事——它不是为此而设计的。

标签: php arrays function syntax


【解决方案1】:

该函数不带参数:get_home_page_promoted_collections()

你想要这样的东西:

$home_collectionsx=get_home_page_promoted_collections(5);

还有:

function get_home_page_promoted_collections($id=null)
{
    $filterClause = '';
    if(!is_null($id))
    {
        //to only return this id
        $filterClause = ' AND collection.ref = '.intval($id);
        //to get all but that id
        $filterClause = ' AND collection.ref != '.intval($id);
    }
    return sql_query("SELECT collection.ref,collection.home_page_publish,collection.home_page_text,collection.home_page_image,resource.thumb_height,resource.thumb_width FROM collection LEFT OUTER JOIN resource on collection.home_page_image=resource.ref WHERE collection.public=1 AND collection.home_page_publish=1".$filterClause." ORDER BY collection.ref DESC");
}

【讨论】:

  • 不幸的是(不接受参数)本身在 PHP 中不是问题,因为默认情况下所有函数都支持可变参数 - 请参阅 php.net/manual/en/function.func-get-args.php
  • 是的,但是如果函数不查找任何参数,那么无论您传递什么/多少个参数,它都不会执行任何操作
  • 感谢 Smokey,但我现在使用您的代码时遇到此错误:(数据库)行 N/A:“where 子句”中的未知列“collection.id”

    从collection.home_page_image=resource.ref 上的collection 左侧外部连接资源中选择collection.ref,collection.home_page_publish,collection.home_page_text,collection.home_page_image,resource.thumb_height,resource.thumb_width 其中collection.public=1 和collection.home_page_publish= 1 AND collection.id = 5 order by collection.ref desc
  • @user2621921 你提到了id 的使用——如果它不在集合表中,请将collection.id 更改为resource.id 或任何你的ID 5 所指的表
  • Ok brill this work collection.ref :-) 但是现在我想在这个函数中做相反的事情并排除 5 的 id: function get_home_page_promoted_collections() { return sql_query("select collection.ref, collection.home_page_publish,collection.home_page_text,collection.home_page_image,resource.thumb_height,resource.thumb_width from collection left external join resource on collection.home_page_image=resource.ref where collection.public=1 and collection.home_page_publish=1 order by collection.ref描述"); }
猜你喜欢
  • 2017-11-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 2010-10-18
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
相关资源
最近更新 更多