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>";
}
tarakwar101Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.