Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

foreach und datenabfrage

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
cyryx
Smarty n00b


Joined: 17 Dec 2014
Posts: 1

PostPosted: Wed Dec 17, 2014 12:17 pm    Post subject: foreach und datenabfrage Reply with quote

hallo

ich habe hier ein kleine problem bei einer simplen sache . ich möchte einfach die die id aus der datenbank ausgegeben haben, jedoch bekomme ich dann eine meldung. das ganze sieht wie folgt aus

PHP
Code:

$smarty = new Smarty;
$db = @new mysqli('localhost', 'root', '', 'ow');
if (mysqli_connect_errno()) {
    die ('Konnte keine Verbindung zur Datenbank aufbauen: '.mysqli_connect_error().'('.mysqli_connect_errno().')');
}

$result = mysqli_query($db ,"SELECT id FROM ow_base_user where id=1");

$smarty->assign('user',($result));


HTML
Code:

{foreach from=$user item=test  }
  {$test}
{/foreach}



Fehlermeldung
Code:

OW Debug - Notice
Message:   Undefined index: user
File:   G:\xampp\htdocs\fsite\ow_smarty\template_c\9cb77358ab46c489538399b621864c6f96cfb577.file.moderation_index.html.php
Line:   29
OW Debug - Notice
Message:   Trying to get property of non-object
File:   G:\xampp\htdocs\fsite\ow_smarty\template_c\9cb77358ab46c489538399b621864c6f96cfb577.file.moderation_index.html.php
Line:   29


wo liegt nun mein fehler ?
vielen dank schonmal im vorraus.

edit:

hier ist noch 9cb77358ab46c489538399b621864c6f96cfb577.file.moderation_index.html.php ab line 28 - 31

Code:

28 <?php  $_smarty_tpl->tpl_vars['wert'] = new Smarty_Variable; $_smarty_tpl->tpl_vars['wert']->_loop = false;
29  $_from = $_smarty_tpl->tpl_vars['werte']->value; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array');}
30 foreach ($_from as $_smarty_tpl->tpl_vars['wert']->key => $_smarty_tpl->tpl_vars['wert']->value){
31 $_smarty_tpl->tpl_vars['wert']->_loop = true;
?>
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Fri Dec 19, 2014 8:53 am    Post subject: Reply with quote

Hey,

mal abgesehen davon, dass du dir die ID vom Benutzer mit der ID=1 holen willst, solltest du ggf. mal statt

Code:
$smarty = new Smarty;


eine instanz davon machen:

Code:
$smarty = new Smarty();


Außerdem reicht nicht nur ein "einfaches" mysqli_query(). Du musst es dann noch "holen" mit z. B.

Code:
mysqli_fetch_object($result);


oder

Code:
mysqli_fetch_row($result);



Du kannst ja auch mal schauen, ob du z. B. im PHP-Code das richtige zurück bekommst.

z. B.:

Code:

...
$result = mysqli_query($db ,"SELECT id FROM ow_base_user where id=1");

var_dump($result);
$smarty->assign('user',($result));
...


PS: Vergiss nicht - falls nicht schon getan - die verbindung wieder zu schließen...
Back to top
View user's profile Send private message Visit poster's website
Evergreen123
Smarty n00b


Joined: 16 May 2012
Posts: 4

PostPosted: Sun Mar 08, 2015 5:53 pm    Post subject: Reply with quote

Hallo, ich habe genau das selbe Problem.

Ich hole mir über eine Abfrage 8 Datensätze aus der DB, die auch richtig ankommen.

Über mysqli_fetch_assoc erstelle ich mir ein Array. Laut phpmyadmin und print_r() erhalte ich auch die gewünschten 8 Datensätze.

Gebe ich diese aber im Template aus (foreach) bekomme ich dreimal die Fehlermeldung "Notice: Undefined index: TITLE in ..."

Mit $smarty->debugging = true; befinden sich aber 11 Elemente im Array, 7 werden aber nur angezeigt. Wenn ich das richtig verstehe:

Code:
meinarray => Array (11)
          0 => Array (4)
            TITLE => "..."
            SUBTITLE => "..."
            PID => "..."
            DATE => "..."
          1 => Array (4)
            TITLE => "..."
            SUBTITLE => "..."
            PID => "..."
            DATE => "..."

usw.

Leider finde ich den Fehler nicht ... Sad


edit: Fehler gefunden. War ein simpler Fehler beim Aufbau des Arrays ...
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP