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

Nested array data is getting overwritten

 
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
mhdn2
Smarty Rookie


Joined: 12 Aug 2015
Posts: 11

PostPosted: Wed Aug 12, 2015 11:01 pm    Post subject: Nested array data is getting overwritten Reply with quote

Hey there. I'm trying to nest foreach loops, but my data for the inner loop is getting overwritten. So no matter how many inner loop iterations I have, the data for each iteration is printed the same on the HTML page, even if the data in PHP is different.

Any suggestions on how I can fix my code? Some kind of append() function instead of assign() function?

Code:
// outer loop
$races_t = array();
$query = mysql_query("SELECT DISTINCT race_id FROM shifts WHERE company_id = $company_id ORDER BY race_id ASC;");
while ( $races = mysql_fetch_array($query) ) {

   // inner loop
   $shifts_t = array();
   $query2 = mysql_query("SELECT * FROM shifts WHERE race_id = $race_id ORDER BY shift_date ASC;");
   while ( $shifts = mysql_fetch_array($query2) ) {
      $shifts_t[] = $shifts;
   }
   $smarty->assign('shifts_t', $shifts_t);

   $races_t[] = $races;
}
$smarty->assign('races_t', $races_t);

$smarty->display('report.html');


Code:
{foreach item=race from=$races_t}
{foreach item=shift from=$shifts_t}
{/foreach}
{/foreach}


Thanks in advance.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Aug 13, 2015 2:48 am    Post subject: Reply with quote

mhdn2 wrote:
Hey there. I'm trying to nest foreach loops, but my data for the inner loop is getting overwritten. So no matter how many inner loop iterations I have, the data for each iteration is printed the same on the HTML page, even if the data in PHP is different.

Any suggestions on how I can fix my code? Some kind of append() function instead of assign() function?

Code:
// outer loop
$races_t = array();
$query = mysql_query("SELECT DISTINCT race_id FROM shifts WHERE company_id = $company_id ORDER BY race_id ASC;");
while ( $races = mysql_fetch_array($query) ) {

   // inner loop
   $shifts_t = array();
   $query2 = mysql_query("SELECT * FROM shifts WHERE race_id = $race_id ORDER BY shift_date ASC;");
   while ( $shifts = mysql_fetch_array($query2) ) {
      $shifts_t[] = $shifts;
   }
   $smarty->assign('shifts_t', $shifts_t);

   $races_t[] = $races;
}
$smarty->assign('races_t', $races_t);

$smarty->display('report.html');


You don't know any better ways to get data, than fetch_array? >.< seriously?

Code:
$_rc = $_pdo->prepare("SELECT DISTINCT race_id FROM shifts WHERE company_id = ? ORDER BY race_id ASC");
$_rc->execute(array($company_id));
$smarty->assign('races_t', $_rc->fetchAll(PDO::FETCH_ASSOC));


Also, your inner loop is just bogus.
Rewrite your SQL to return all the data you need at once.

Quote:
Code:
{foreach item=race from=$races_t}
{foreach item=shift from=$shifts_t}
{/foreach}
{/foreach}


Thanks in advance.


That's because
Code:
<table>{foreach $arrayvar as $itemvar}
<tr><td>{$itemvar@key}</td><td>{$itemvar}</td></tr>
{/foreach}</table>

In other words, wrong syntax leads to wrong results.
Back to top
View user's profile Send private message
mhdn2
Smarty Rookie


Joined: 12 Aug 2015
Posts: 11

PostPosted: Fri Aug 14, 2015 9:19 am    Post subject: Reply with quote

Thanks for the response. I am going out of the country for a trip this weekend. I will refactor my code using your feedback and see if I can get it working on Tuesday. Looking forward to learning more about Smarty and PHP :-)
Back to top
View user's profile Send private message
mhdn2
Smarty Rookie


Joined: 12 Aug 2015
Posts: 11

PostPosted: Mon Aug 24, 2015 12:08 am    Post subject: Reply with quote

Alright, I'm working on converting all my code to PDO. It's going well.

Anyway, I'm still confused on how to solve the original problem. My original problem is that I need to pass two layers of data to Smarty. I need to pass a bunch of races, and then each race has a bunch of shifts. So these are the variables that need to get passed to Smarty:

Code:
Outside loop:
race_name
   Inside Loop:
   shift_name
   shift_date
   shift_time
   shift_enrolled
   shift_needed
   shift_percentage
weeks_until_event
days_until_event
signed_up_total
needed_total
percent_formatting
percent_average


Something is wrong with my code though and I can't get my inner loop data (shift data) to show up in Smarty correctly. It's just taking the last set of shift data and putting that for all the races. What am I doing wrong and what is the most efficient way to code this?

Code:
// outer loop
$races_t = array();
$query = mysql_query("SELECT DISTINCT race_id FROM shifts WHERE company_id = $company_id ORDER BY race_id ASC;");
while ( $races = mysql_fetch_array($query) ) {
   // bunch of assignments to the $races array

   // inner loop
   $shifts_t = array();
   $query2 = mysql_query("SELECT * FROM shifts WHERE race_id = $race_id ORDER BY shift_date ASC;");
   while ( $shifts = mysql_fetch_array($query2) ) {
       // bunch of assignments to the $shifts array

      $shifts_t[] = $shifts;
   }
   $smarty->assign('shifts_t', $shifts_t);

   // bunch of assignments to the $races array

   $races_t[] = $races;
}
$smarty->assign('races_t', $races_t);

$smarty->display('report.html');


Code:
{foreach item=race from=$races_t}
      <br />
      
      <table id="table">
         <tr id="title">
            <td colspan="6">
               {$race.race_name}
            </td>
         </tr>
         <tr id="firstrow">
            <td id="shift">
               Shift
            </td>
            <td id="date">
               Date
            </td>
            <td id="time">
               Time
            </td>
            <td id="signedup">
               Signed Up
            </td>
            <td id="needed">
               Needed
            </td>
            <td id="percent">
               %
            </td>
         </tr>

{foreach item=shift from=$shifts_t}
         <tr>
            <td>
               {$shift.shift_name}
            </td>
            <td>
               {$shift.shift_date}
            </td>
            <td id="bold_right_border">
               {$shift.shift_time}
            </td>
            <td>
               {$shift.shift_enrolled}
            </td>
            <td>
               {$shift.shift_needed}
            </td>
            <td {$shift.percent_formatting}>
               {$shift.percent}
            </td>
         </tr>
{/foreach}
         
         <tr id="bold_upper_border">
            <td align="center">
               {$race.weeks_until_event} Weeks Until Event
            </td>
            <td colspan="2" align="center" id="bold_right_border">
               {$race.days_until_event} Days Until Event
            </td>
            <td>
               {$race.signed_up_total}
            </td>
            <td>
               {$race.needed_total}
            </td>
            <td {$race.percent_formatting}>
               {$race.percent_average}
            </td>
         </tr>
      </table>
{/foreach}


Thank you.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Aug 24, 2015 4:59 am    Post subject: Reply with quote

Your outer while loop does overwrite $_shifts. you must create a nested array.
Code:

// outer loop
$races_t = array();
$query = mysql_query("SELECT DISTINCT race_id FROM shifts WHERE company_id = $company_id ORDER BY race_id ASC;");
while ( $races = mysql_fetch_array($query) ) {
   // bunch of assignments to the $races array

   // inner loop
   $shifts_t = array();
   $query2 = mysql_query("SELECT * FROM shifts WHERE race_id = $race_id ORDER BY shift_date ASC;");
   while ( $shifts = mysql_fetch_array($query2) ) {
       // bunch of assignments to the $shifts array

      $shifts_t[] = $shifts;
   }
 
   // bunch of assignments to the $races array
   $races['shifts_t'] = $shifts_t;
   $races_t[] = $races;
}
$smarty->assign('races_t', $races_t);

$smarty->display('report.html');


Code:
{foreach item=race from=$races_t}
      <br />
       
      <table id="table">
         <tr id="title">
            <td colspan="6">
               {$race.race_name}
            </td>
         </tr>
         <tr id="firstrow">
            <td id="shift">
               Shift
            </td>
            <td id="date">
               Date
            </td>
            <td id="time">
               Time
            </td>
            <td id="signedup">
               Signed Up
            </td>
            <td id="needed">
               Needed
            </td>
            <td id="percent">
               %
            </td>
         </tr>

{foreach item=shift from=$race.shifts_t}
         <tr>
            <td>
               {$shift.shift_name}
            </td>
            <td>
               {$shift.shift_date}
            </td>
            <td id="bold_right_border">
               {$shift.shift_time}
            </td>
            <td>
               {$shift.shift_enrolled}
            </td>
            <td>
               {$shift.shift_needed}
            </td>
            <td {$shift.percent_formatting}>
               {$shift.percent}
            </td>
         </tr>
{/foreach}
         
         <tr id="bold_upper_border">
            <td align="center">
               {$race.weeks_until_event} Weeks Until Event
            </td>
            <td colspan="2" align="center" id="bold_right_border">
               {$race.days_until_event} Days Until Event
            </td>
            <td>
               {$race.signed_up_total}
            </td>
            <td>
               {$race.needed_total}
            </td>
            <td {$race.percent_formatting}>
               {$race.percent_average}
            </td>
         </tr>
      </table>
{/foreach}
Back to top
View user's profile Send private message
mhdn2
Smarty Rookie


Joined: 12 Aug 2015
Posts: 11

PostPosted: Tue Aug 25, 2015 10:17 am    Post subject: Reply with quote

That worked. And I see the pattern now. I didn't know you could put {foreach item=shift from=$race.shifts}. That period is basically the key to doing nested loops in Smarty.

Well, that, and building your arrays correctly. You've got to be an array pro to do well with Smarty in these nested loop situations.

Thank you for your help. I appreciate it.
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