[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Help! smarty template issue

Help! I have been working with a smarty template for awhile now and so far I get it..but I am trying to render the results of a query, a (loop result) and I am stuck. I have it giving the results but they are an echo result and it shows up above the include header I can't get it to render within the template, because of the echo results. In the past all I needed to do was do something like this
EXAMPLE:
 $new_row = mysql_fetch_array($new_result);
 $user_name = stripslashes($new_row["Name"]);
 $user_name .= " ";
 $user_name .= stripslashes($new_row["Surename"]);
 $smarty->assign("user_name",$user_name);
}
and on the .tpl put in {if (isset($cur_student))} {$username} {/if} and I could put it any where I want it on the page, but I am not sure how to change the code for a loop to to show from the below code I have.
change it from an echo result to defined result and still do the loop?

if ($result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=".$session_data["CurStudentID"], $link))
   while ($row = mysql_fetch_row($result))  

       $number1 = mysql_num_rows($result);
  if (!$number1)
   printf("<center><font face=\"Verdana, Arial, Helvetica, sans-serif\">No Course History found.</center></font><br> \n");
 $i = 0;
while ($i < $number1):

$CourseID = mysql_result($result, $i,"CourseID");
$Subj = mysql_result($result, $i,"Subj");
$Number = mysql_result($result, $i,"Number");
$Date = mysql_result($result, $i,"Date");
$Date = date('m-d-Y', $Date);

if ($i%2 == 0) {     <!--- from here is where I am stuck!-->

        } else {
        echo  "<td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-2\">Course ID:&nbsp;$CourseID&nbsp;</font></td>
<td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-2\">Subject:&nbsp;$Subj&nbsp;</font></td>
<td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-2\">Course Number:&nbsp;$Number&nbsp;</font></td>
<td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-2\">Purchase Date:&nbsp;$Date&nbsp;</font></td><br>
</td> </tr>";
    }
$i++;
endwhile;
echo  "</table>";
}
0
tarakwar101
Asked:
tarakwar101
  • 11
  • 5
1 Solution
 
Brian BushSolutions ArchitectCommented:
Ok, I think I understand what you're wanting.
This is how you do that:

1. Create an array in PHP and assign it to a SMARTY variable.
2. Loop through the results in SMARTY to display.

for example:

$_test = array(
    array(
        "CourseID" => "101",
        "Subject" => "Physics",
        "Date" => "1/1/2006"
    ),
    array(
        "CourseID" => "103",
        "Subject" => "Physics",
        "Date" => "12/1/2006"
    ),
    array(
        "CourseID" => "101",
        "Subject" => "Calculus",
        "Date" => "1/1/2006"
    )
);

$smarty->assign("courses", $_test);

Then, in your template, something like this:

{foreach key=key item=item from=$courses}
    Row Number {$key}: {$item.Subject} {$item.CourseID} - {$item.Date}<br />
{/foreach}

Make sense?
--brian
0
 
tarakwar101Author Commented:
Well I got it to show in the right place !! Now the only issue is getting it to loop? It shows the first info just great but then it shows the same info on next 2 lines as well.
Here is what I did.

if ($result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=".$session_data["CurStudentID"], $link))
   while ($row = mysql_fetch_row($result))  

       $number1 = mysql_num_rows($result);
  if (!$number1)
   printf("<center><font face=\"Verdana, Arial, Helvetica, sans-serif\">No Course History found.</center></font><br> \n");
 $i = 0;
while ($i < $number1):

$CourseID = mysql_result($result, $i,"CourseID");
$form_value["CourseID"] = "value=\"".($row["CourseID"])."\"";
$Subj = mysql_result($result, $i,"Subj");
$Number = mysql_result($result, $i,"Number");
$Date = mysql_result($result, $i,"Date");
$Date = date('m-d-Y', $Date);

if ($i%2 == 0) {

        } else {
            
$showmy = array(
    array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
            "Date" => $Date
    ),
    array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
            "Date" => $Date
    ),
    array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
            "Date" => $Date
    )
)
}
$i++;
endwhile;
}
$smarty->assign("courses", $showmy);
0
 
Brian BushSolutions ArchitectCommented:
Sorry I wasn't clear about that.

$showmy[] = array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
          "Date" => $Date
);

Try it like that.
--brian
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
tarakwar101Author Commented:
Another question what if they have more then 3 do I have to put in as many arrays as I think they would have? Meaning if they have taken 20 courses do I have to add more arrays?
Is there a wat to code the array to just loop craeting another row?
0
 
Brian BushSolutions ArchitectCommented:
The "[]" on the $showmy[] tell PHP to just add another one to the end of the array.
You can have as many as you want.

I was showing the three at the top as a manual loading of an array to test.
Sorry it was misleading.
--brian
0
 
tarakwar101Author Commented:
When I added the $showmy[], then nothing showed up?
0
 
tarakwar101Author Commented:
ahhh ok I will retest
0
 
tarakwar101Author Commented:
Hmm a Little lost?
0
 
tarakwar101Author Commented:
Do I need to add something on the template page? []
     <td>{foreach key=key item=item from=$courses}
    Course: {$item.Subject} Course ID:{$item.CourseID} Date: {$item.Date}<br />
{/foreach}</td>

if ($result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=".$session_data["CurStudentID"], $link))
   while ($row = mysql_fetch_row($result))  

       $number1 = mysql_num_rows($result);
  if (!$number1)
   printf("<center><font face=\"Verdana, Arial, Helvetica, sans-serif\">No Course History found.</center></font><br> \n");
   
 $i = 0;
while ($i < $number1):

$CourseID = mysql_result($result, $i,"CourseID");
$Subj = mysql_result($result, $i,"Subj");
$Number = mysql_result($result, $i,"Number");
$Date = mysql_result($result, $i,"Date");
$Date = date('m-d-Y', $Date);

if ($i%2 == 0) {

        } else {
            
      $showmy[] = array(
    array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
            "Date" => $Date)
);
}
$i++;
endwhile;
}
$smarty->assign("courses", $showmy);
0
 
tarakwar101Author Commented:
this it what shows up on template using above code:
Course: Course ID: Date:
Course: Course ID: Date:
Course: Course ID: Date:
Course: Course ID: Date:
Course: Course ID: Date:
Course: Course ID: Date:

Which is the correct number of courses for the test student but nothing shows up.
before here is what showed up:

Course: State Regulatory Study Guide-Washington Course ID:116 Date: 02-14-2006
Course: State Regulatory Study Guide-Washington Course ID:116 Date: 02-14-2006
Course: State Regulatory Study Guide-Washington Course ID:116 Date: 02-14-2006
0
 
Brian BushSolutions ArchitectCommented:
$result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=" . $session_data["CurStudentID"],  $link);

$showmy = array();
while ($row = mysql_fetch_assoc($result)) {
    $showmy[] = array(
       "CourseID" => $row["CourseID"],
       "Subject" => $row["Subj"],
       "Number" => $row["Number"],
       "Date" => date('m-d-Y', $row["Date"]),
    );
}
mysql_free_result($result);

That is the code to run a query and dump the result to an array called $showmy.

I am concerned about this line in your code:
if ($i%2 == 0) {

        }

That tells it to skip every other result from the query. where $i (the row number) modulus (%) 2 is zero.
That is rownumber/2 has a remainder of zero.

--brian
0
 
tarakwar101Author Commented:
What should it be, I am still learning to use php. I was wondwering how that modulus (%) came into play?
The above code you posted though has no loop?
0
 
Brian BushSolutions ArchitectCommented:
The while($row = mysql_fetch_assoc($result)) is a loop.
It says loop through the $result until it is done.

You should take out these lines:

if ($result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=".$session_data["CurStudentID"], $link))
   while ($row = mysql_fetch_row($result))  

       $number1 = mysql_num_rows($result);
  if (!$number1)
   printf("<center><font face=\"Verdana, Arial, Helvetica, sans-serif\">No Course History found.</center></font><br> \n");
   
 $i = 0;
while ($i < $number1):

$CourseID = mysql_result($result, $i,"CourseID");
$Subj = mysql_result($result, $i,"Subj");
$Number = mysql_result($result, $i,"Number");
$Date = mysql_result($result, $i,"Date");
$Date = date('m-d-Y', $Date);

if ($i%2 == 0) {

        } else {
         
      $showmy[] = array(
    array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
          "Date" => $Date)
);
}
$i++;
endwhile;
}

and then substitute these:
$result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=" . $session_data["CurStudentID"],  $link);

$showmy = array();
while ($row = mysql_fetch_assoc($result)) {
    $showmy[] = array(
       "CourseID" => $row["CourseID"],
       "Subject" => $row["Subj"],
       "Number" => $row["Number"],
       "Date" => date('m-d-Y', $row["Date"]),
    );
}
mysql_free_result($result);

--brian


--brian
0
 
tarakwar101Author Commented:
Nothing happens now , nothing shows up?
0
 
tarakwar101Author Commented:
AHHH perfect I forgot to add the $smarty->assign("courses", $showmy); Now it works great!!!
0
 
tarakwar101Author Commented:
I knew I was over thinking this! You would crack up if you had seen what I had started with and what I tried after it didn't work! I actually had similiar code  to begin with. Grrrr
Thank you so much Brian! You have been an awsome teacher.
This gives me so much info for all the rest of the pages I have to do.
The next challenge is to do the same sort of thing. I have a page that when a cooordinator clicks link and page opens I have to show all other students that have the same companyid as he does?
But I will open that in another question WHEN I run into issues lol
thanks for all the help! A+
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now