Display Multiple Arrays in Smarty Template

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Aleeious
Posts: 55
Joined: Tue Sep 13, 2011 1:22 pm

Display Multiple Arrays in Smarty Template

Post by Aleeious »

I am working on displaying the player's pending challenges using a smarty template. Here is how i am getting them(cut for brevity):

Code: Select all

class Match
{
	// constant for open Match
	const STATUS_OPEN = 0;

	// constant for completed Match
	const STATUS_COMPLETED = 1;

	// constant for cancelled Match
	const STATUS_CANCELLED = 2;

	// constant for match that was result of error
	const STATUS_ERROR = 3;

	// match id
	private $matchID = array();

	// challenger of match
	private $challenger = array();

	// defender of match
	private $defender = array();

	// instance of database
	protected $db;

	/**
	* class constructor
	* @param $db database class instance
	*/
    public function __construct(Database $db)
    {
        // get instance of database
        $this->db = $db;
    }

	/**
	* get the current matches the playerhas pending
	* @username username to check for matches for
	*/
	public function getMatches($username)
	{
		// prepare the sql statement to change the password
        $statement = $this->db->prepare("SELECT challenger, defender FROM " . TABLE_MATCHINDEXES . " WHERE challenger = ? or defender = ? and status = ? LIMIT 5");

        // bing the variables
        $statement->bind_param('ssi', $username, $username, $self::STATUS_OPEN);

        // if the statement executed successfully
        if ($statement->execute())
        {
        	// get the number of results
			$statement->bind_result($challenger, $defender);

			// fetch the results
			$statement->fetch();

			// if a result exists
			if($challenger || $defender)
			{
				// set the challenger
				$this->challenger[] = $challenger;

				// set the defender
				$this->defender[] = $defender;

				// return true
				return true;
			}

            // so matches were found so return true
            return true;
        }

        // the query failed so return false
        return false;
	}

}
And here is the template code so far:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Aleeious - Pending Challanges</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="templates/css/style.css" media="screen" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="mobile-web-app-capable" content="yes" />
</head>
 <body>
<h1>Pending Challenges</h1>
<table>
<tr>
<td>Challenger</td><td>Defender</td><td>Accept</td><td>Decline</td>
</tr>
{foreach $challengers as $challenger}
{foreach $defenders as $defender}
<tr>
<td>{$challenger}</td><td>{$defender}</td><td><a href="{$acceptlink}" title="Click here to accept this match">[accept]</a></td><td><a href="{$declinelink}" title="Click here to decline this match">[decline]</a></td>
</tr>
{/foreach}
{/foreach}
</table>
<p><a href="main.php" title="Click here to return to the main menu" accesskey="1">back</a></p>
</body>
</html>
Will this work or am i gonna have to do it another way? Any assistance in this matter would be greatly appreciated.

Sincerely,

Aleeious
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Display Multiple Arrays in Smarty Template

Post by Jackolantern »

Are you using a framework? I ask because Smarty is really designed for developers working with graphic designers on the same pages. It is pretty heavy for a single dev or a small team of devs. Due to PHP's ability to be embedded in HTML, many frameworks simply use embedded PHP for templates, and it works very well and intuitively.

Sadly, as for the actual question, I have not worked much with Smarty.
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”