1
 <div class="meta">
<?php echo  $submitted."pubblished in".$fenl; ?>
<a href="<?php echo base_path().'comp/'.$node->nid;?>">
<?php echo $comment_count;?>comment</a>
</div>

it there a better way to write the above code.

2,

function blog_gettag(){
    $terms = mysql_query('SELECT vid,name FROM term WHERE vid=2');
    $items = array(); 
    foreach ($terms as $term){
        $items[] = $term->name;
    }

    return $items;
}

could i delete $items = array(); this line. thank you.

zhuanzhou
  • 2,409
  • 8
  • 33
  • 51
  • 2
    I wouldn't recommend that. Let's say your `foreach()` loop doesn't get hit, and you delete that line. Now `$items` becomes undefined. – onteria_ May 29 '11 at 12:41
  • Also you if you're already using double quotes, then variables can be embedded inside: `echo "$submitted published in $fenl";` and `echo base_path()."comp/{$node->nid}";` – onteria_ May 29 '11 at 12:43
  • 1
    does this code really work? AFAIK You can't iterate result of `mysql_query`. – ahmet alp balkan May 29 '11 at 12:47
  • onteria_,you're right. i happened to see an error"the $items becomes undefined. why if doesn't reach to the foreach loop, it shows $items undefined,if i delete that line. thank you – zhuanzhou May 29 '11 at 13:29

4 Answers4

1
  1. Not really, unless you count the short-tags, but they are frowned on.

  2. Technically, it would be created automatically, but you should pre-declare the empty array, it should generate a warning if you did not.

NB. If you have two separate questions, they should be asked separately in future.

Community
  • 1
  • 1
Orbling
  • 20,413
  • 3
  • 53
  • 64
1

What about one line?

<?php
echo "<div class=\"meta\">\n{$submitted}published in$fenl\n<a href=\"" .
  base_path() . "comp/{$node->nid}\">\n$comment_count\ncomment</a>\n</div>";
?>

You could delete $items = array(); as long as $items isn't used before in your script (just don't return $items in case $terms isn't an array and the foreach loop gets called, as the situation seems to be at the moment). I believe you need to check mysql_query() examples at http://php.net/manual/en/function.mysql-query.php.

AndersTornkvist
  • 2,610
  • 20
  • 38
0

It's syntactically pretty okay. As far as I know, you could omit $items = array(); but I find it more readable if you just leave it as it is.

Btw.: Don't you need to use mysql_fetch_assoc for your iteration? mysql_query returns a resource.

reeaal
  • 347
  • 3
  • 14
0
echo "<div class=\"meta\">" . $submitted . " pubblished in " . $fenl . "<a href=\"" . base_path() . "comp/" . $node->nid . "\">" . $comment_count . " comment</a></div>";

You don't need to define array. But in this case ( foreach issue. Foreach loop requires an array as parameter to left side the "as". If you delete that line $items wouldn't be an array.It will be a null variable.So you will get a warning. ) it should be defined.

miqbal
  • 2,213
  • 3
  • 27
  • 35