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

Undefined offset

 
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: Polish
View previous topic :: View next topic  
Author Message
Bajki
Smarty Rookie


Joined: 11 Jul 2006
Posts: 9

PostPosted: Tue Jul 11, 2006 9:17 pm    Post subject: Undefined offset Reply with quote

Witam
Mam dwa pliki :
art_search.php :
[php:1:66658b64d2]<?php

error_reporting( E_ALL );
ini_set( 'display_errors', 1 );

require 'libs/Smarty.class.php';
$template = new Smarty;

$template->compile_check = true;
$template->debugging = true;
mysql_connect ('localhost', 'root', 'krasnal') or die('Nie udalo polaczys sie z baza');
mysql_select_db('zin') or die('Nie udalo sie wybrac bazy');


$result = mysql_query('SELECT id, nazwa, data FROM artykuly') or die (mysql_error());
$tabl=mysql_fetch_assoc($result);

$template->assign('artykuly', $tabl);
$template->display('artykuly.tpl');


?>[/php:1:66658b64d2]

Oraz artykuly.tpl :
Code:
<table>
<tr>
<td>ID</td> <td>Nazwa</td> <td>Autor</td>
</tr>

{section name=co loop=$artykuly}
  <tr>
    <td><a href="view.php?id={$artykuly[co].id}">view<a></td>
    <td>{$artykuly[co].nazwa}</td>
    <td>{$artykuly[co].autor}</td>
  </tr>
{/section}

</table>


I teraz...kiedy chce uruchomic strone, pojawia mi sie takie oto cos Razz :
http://puszcza.roxtet.com/art_search.html

Czym to jest spowodowane ? Gdzie robie blad ?

Tak wyglada moja tabela "artykuly" :


Pozdrawiam
Back to top
View user's profile Send private message
SzubertX
Smarty Rookie


Joined: 27 Sep 2005
Posts: 8
Location: Poland

PostPosted: Thu Jul 13, 2006 11:24 pm    Post subject: Reply with quote

Witam!
Problem nie leży w Smarty - choć może pośrednio Wink Funkcja {section} działa na tablica wielowymiarowych - numerycznych. Dobrze że wstawiłeś kod php - bo od razu widać, że przetwarzasz zwracane dane z MySQLa do tablicy asocjacyjnej - a Smarty poszukują indeksu numerycznego. Jeśli chcesz korzystać z funkcji {section} musisz dostarczyć do Smarty tablicę numeryczną - więc wystarczy zmienić instrukcję php:
Code:
$tabl=mysql_fetch_assoc($result);

na:
Code:
$tabl=mysql_fetch_array($result);

i wszystko będzie ok Wink

Nazwa 'co' w funkcji {section} jest niczym innym jak iteracją od zera do liczby którą określa loop (jak count w php) więc zmienna $artykuły w {section} wygląda w rozumieniu php jak np.:
Code:
$artykuly[$i]['nazwa']


Pozdrawiam!
Back to top
View user's profile Send private message
Bajki
Smarty Rookie


Joined: 11 Jul 2006
Posts: 9

PostPosted: Fri Jul 14, 2006 8:32 am    Post subject: Reply with quote

Problem zalatwilem wczesniej (chociaz zapomnialem o tym napisac :0 sory Razz). Zapytalem oto na innym forum, gdzie napisali mi zebym zastapil to co mam takim kodem :
[php:1:062e8aae6b]<?php
mysql_connect(...);

$table = array();
$result = mysql_query( 'SELECT id, nazwa, autor FROM artykuly' ) or die ( mysql_error() );
while( $row = mysql_fetch_assoc( $result ) )
$table[] = $row;

$template->assign( 'artykuly' , $table );
?>[/php:1:062e8aae6b]

a w pliku tpl uzyc {foreach} :
Code:
<table>
<tr>
<td>ID</td> <td>Nazwa</td> <td>Autor</td>
</tr>
{foreach from=$artykuly key=key item=item}
<tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}">
<td><a href="view.php?id={$item.id}">view<a></td>
<td>{$item.nazwa}</td>
<td>{$item.autor}</td>
</tr>
{/foreach}
</table>


Link do tematu
Back to top
View user's profile Send private message
SzubertX
Smarty Rookie


Joined: 27 Sep 2005
Posts: 8
Location: Poland

PostPosted: Fri Jul 14, 2006 10:03 am    Post subject: Reply with quote

Owszem można i tak - myślałem po prostu, że chcesz koniecznie użyć {section} Smile Ja osobiście pobieram dane z bazy przez Peara i wygodniej mi jest uzywac {section}, wiec dlatego to opisałem Wink
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: Polish 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