简体   繁体   English

聪明地分配给数组

[英]smarty assign to array

$result = mysql_query("SELECT blog_title,body FROM blog WHERE post_id='$id' LIMIT 1") or die (mysql_error());

while ($line = mysql_fetch_assoc($result)){
 $tasks[] = $line;
 $group = $tasks['blog_title'];
 $smarty->assign('view', $tasks);
 $smarty->assign('group', $group);
//here the error.i want to assign blog_title to title
 $smarty->assign('title', "Blog - $group");

newbie,need help.i want to assign blog_title to title any idea? 新手,需要帮助。我想为blog_title指定标题有什么想法吗?

You don't need $tasks . 您不需要$tasks This variable messes your script up due to the [] you're using. 由于使用的[]此变量使您的脚本混乱。 [] = works just like creating an array using array and applying array_push to it. [] =就像使用array创建一个数组并对其应用array_push一样。

Here's what you want to achieve: 这是您要实现的目标:

$result = mysql_query("SELECT blog_title,body FROM blog WHERE post_id='$id' LIMIT 1") or die (mysql_error());

while ($line = mysql_fetch_assoc($result)){
  $group = $line['blog_title'];
  $smarty->assign('view', $line);
  $smarty->assign('group', $group);
  $smarty->assign('title', "Blog - $group");
}

By the way: in the first line ( $result =... ), ensure that $id is properly escaped (see mysql_real_escape ). 顺便说一句:在第一行( $result =... )中,确保正确地转义了$id (请参见mysql_real_escape )。

You create a 2 dimensional array that's not supposed to be there. 您创建了一个不应该存在的二维数组。 Tasks looks like this now: 现在的任务如下所示:

<?php
tasks = array(
      0 => array(
        'blog_title' => 'something'
        'body' => 'something else'
      )
    )
?>

So you do $group = $tasks['blog_title'] it does nothing because $tasks only has a key 0, not 'blog_title'. 因此,您执行$ group = $ tasks ['blog_title']不会执行任何操作,因为$ tasks仅具有键0,而没有'blog_title'。 $group[0]['blog_title'] would work but just remove the tasks assignment altogether. $ group [0] ['blog_title']可以工作,但只需完全删除任务分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM