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

Problem: Array starts from 0 (zero) and DB AutoIncrement not

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


Joined: 01 May 2003
Posts: 18

PostPosted: Fri May 02, 2003 11:16 am    Post subject: Problem: Array starts from 0 (zero) and DB AutoIncrement not Reply with quote

I'm trying to make a new site with Php/MySQL(through Pear:DB)/Smarty, and it works (with caching per articles, etc).

But the only problem I got is that the MySQL Auto_Increment starts from 1, and array's starts from 0 (zero).

I read the data from DB with the following code:
Code:
      $sql = 'SELECT  asc_news.nid,
                  asc_speltak.name AS sidname,
                  asc_news.title,
                  asc_news.text
            FROM asc_news,
                   asc_speltak
            WHERE asc_news.speltakid = asc_speltak.sid';

      $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
      if (DB::isError($results)) {
         die ($results->getMessage());
      }
               $tmpl->assign('results', $results);


And I use it in a template like this:
Code:
   <table width="100%" style="border: 1px solid #00CC00;" border="0" cellpadding="0" cellspacing="2" bgcolor="#CCCCCC">
      <tr>
         <td height="26" bgcolor="#00CC00" style="font-size: 14px;"><font color="#FFFFFF"><strong>Nieuws </strong></font></td>
      </tr>
   {* Toon nieuwstitels met voor iedere speltak zijn eigen achtergrondkleur *}
   {section name='newslist' loop=$results}
   {strip}
      <tr bgcolor={if $results[newslist].name == 'esta'}
            "#00CC00"
         {elseif $results[newslist].name == 'scout'}
            "#FFB76F"
         {elseif $results[newslist].name == 'explo'}
            "#FF0000"
         {else}
            "#99CC99"
         {/if}>{/strip}
         <td><span style="font-size: 7pt;">22 Apr 12:10</span>
               <a href="nieuws.php?nid={$results[newslist].nid}" title="Bekijk het volledige bericht via nieuws.php">{$results[newslist].title|truncate:30}</a>
         </td>
   {/section}

   {$site_name}
   </table>


When I call the page as following: http://domain.ext/nieuws.php?nid=5
It should get the news message with nid (newsID) nr.5, but it get's the message with nid=4 (because of the array form).

What is the best way to fix this?
Should I just use the nid value minus 1 everywhere, or is there a way to start the array counting also from 1?

Thanks
Back to top
View user's profile Send private message
eadz
Smarty Regular


Joined: 30 Apr 2003
Posts: 61
Location: Auckland, New Zealand

PostPosted: Fri May 02, 2003 11:23 am    Post subject: Reply with quote

there is a start param to section :

Code:

{section name=n loop=$items start=1}

...


{/section}

http://smarty.php.net/manual/en/language.function.section.php
Back to top
View user's profile Send private message Visit poster's website
tdm
Smarty Rookie


Joined: 01 May 2003
Posts: 18

PostPosted: Fri May 02, 2003 11:42 am    Post subject: Reply with quote

eadz wrote:
there is a start param to section :

Code:

{section name=n loop=$items start=1}

...


{/section}

http://smarty.php.net/manual/en/language.function.section.php


That is not exactly what I meant, the template recieves this array from the database:
Code:
Array (2)
0 => Array (4)
  nid => 1
  sidname => esta
  title => Een esta bericht!
  text => Nieuws voor de esta's op de FrontPage!!
1 => Array (4)
  nid => 2
  sidname => explo
  title => Explo NEWS
  text => ff wat Explo Nieuws op de FrontPage

When I use:
Code:
{section name=n loop=$items start=1}
...
{/section}

It will start from the array with the index nr.1.
The array still starts from 0 (zero).
I need the array to start from 1.

Or anything else if there's a better solution?

Thanks
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri May 02, 2003 5:53 pm    Post subject: Reply with quote

yes, when you assign the ID to Smarty, do it like:

$smarty->assign('id', $id+1);

or increment it within Smarty (not a great idea) using the {math} plugin

or something similar Wink

good luck
Back to top
View user's profile Send private message
tdm
Smarty Rookie


Joined: 01 May 2003
Posts: 18

PostPosted: Fri May 02, 2003 8:31 pm    Post subject: Reply with quote

Thanks, it works Very Happy

I'm getting to love Smarty Laughing
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 -> General 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