16. juli 2008 - 17:16Der er
9 kommentarer og 1 løsning
foreach i en WP plugin-funktion
Jeg forsøger at skrive en plugin til min weblog Wordpress.
og jeg er nu kommet så langt: (har undladt indledende kommentarer) 'function comments($no_comment,$exerpt_length) { global $wpdb;
// henter data vedd hjælp af en Wordpress funktion $rows= $wpdb->query("SELECT comment_ID, comment_post_ID, comment_author, comment_content, comment_approved FROM comments ORDER BY comment_ID DESC LIMIT ".$nocomment." WHERE comment_approved='1'");
// Vis data foreach ($rows as $row) { $tring='<a href="'.get_bloginfo ('url').'/?p='.$row[comment_post_ID].'#comment-'.$row[comment_ID].'">'.$row[comment_author].': </a>';
if ($exerpt=0) {$tring.=$row[comment_content];} else {$tring.=substr($row[comment_content],0,$exerpt).'...';} $tring.= '<br/>';
echo $tring; } } " og nu til mit problem, jeg får denne besked:
Warning: Invalid argument supplied for foreach() in ...
$rows= $wpdb->query("SELECT comment_ID, comment_post_ID, comment_author, comment_content, comment_approved FROM comments ORDER BY comment_ID DESC LIMIT ".$nocomment." WHERE comment_approved='1'");
til:
$rows= $wpdb->query("SELECT comment_ID, comment_post_ID, comment_author, comment_content, comment_approved FROM comments ORDER BY comment_ID DESC LIMIT ".$no_comment." WHERE comment_approved='1'");
Det er muligvis rigtigt at du har en default værdi, men det hjælper jo ikke på at du kalder variablen noget helt andet i SQL sætningen, end den gør i funktionens argument-liste :^)
Umiddelbart burde det være nok til at forklare den SQL fejl du får. Skal det forstås sådan at koden stadig fejler efter at du har rettet det? Med samme fejlbesked?
$rows= $wpdb->query("SELECT comment_ID, comment_post_ID, comment_author, comment_content, comment_approved FROM $wpdb->comments WHERE comment_approved='1' ORDER BY comment_ID DESC LIMIT".$no_comment);
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.