SalsaScript Tutorial (1, 2, 3, 4, 5)

Part III - Creating a SalsaScript News Aggregator

For the next section, we're going to use our package to build a basic img to add and list entries to the "news_item" database in SalsaScript.

How It Will Work

The news aggregator will have two parts; A form to add new entries to the database, and a section to list all of the entries that have been added.

Adding News Items

First we're going to create an HTML form to add in the news entries. All forms to save content in Salsa need to be sent to the global "/save" mechanism with the name of the database table as a variable named "table". At it's most basic this would result in a form like this:

<form action="/save" method="POST">
	<input type="hidden" name="table" value="news_item" />
</form>

Additionally, we can add in a variable for an entry key if we were updating a pre-existing entry in the database. If we know the value of the entry key, (e.g. 13), the form would look something like this:

<form action="/save" method="POST">
	<input type="hidden" name="table" value="news_item" />
	<input type="hidden" name="key" value="13" />
</form>

For our news_item form, we want to create a new entry with a Title and a Description. The values of the fields for Title and Description have to correspond to the names for the database fields ("Name_or_Headline" and "Short_Description"). The HTML code for the form would then look something like this:

<form action="/save" method="POST">
	<input type="hidden" name="table" value="news_item"/>
	<label for="Name_or_Headline">Name of Article</label>
	<input type="text" name="Name_or_Headline">
	
	<label for="Short_Description">Description</label>
	<input type="text" name="Short_Description">
	<input type="submit" value="Submit">
</form>

Reading the data back

So we've built an img that adds entries to the application. Next up, we are going to read the data back as list items and display it with some div tags. To do this, we're going to introduce a little SalsaScript and the concept of the "salsa.getObjects" call which pulls entries from the database and then be listed on the page. To load all of the entries from the "news_item" table into a variable it would look like this:

<? var results=salsa.getObjects("news_item");  ?>

The result is an array called "results" where each item in the array is a specific entry in the table.

Conditions can also be added to the getObjects call to narrow the results returned. To add a new condition we first create a "Condition" variable and supply it with our requirements. A "Condition" variable is an array of the form:

new Condition("databaseFieldName",'=','value')

Here, "databaseFieldName" is the name of the field in the database that were are putting the restriction on. The equals operator is the type of condition we are making, and "value" is the value that the field is being restricted to. So if we want to create a condition for our news_item database call it would look like this:

<? 
	var conditions = new Array(new Condition('Name_or_Header','=',"%asd%"));
	var results=salsa.getObjects("news_item",conditions);
?>

We first create a variable called "conditions" that is an Array of Condition objects, and then add that variable to our getObjects call. Here, we are looking for new items whose Name/Header has the letters "asd" in them. The percent character ("%") is a wildcard. For more information on the syntax of Conditions and the getObjects call, please see our documentation on the Database Object in SalsaScript.

We can then loop through the results variable to display all of the entries with the SalsaScript for each function:

<?
	var conditions = [new Condition('Name_or_Header','=',"%asd%")];
	var results=salsa.getObjects("news_item",conditions);
	print('<ul>');
	for each (salsa_entry in results){
		print('<div >\n');
		print('\t<li>' + salsa_entry.Name_or_Headline + '</li>');
		print('\t<li>' + salsa_entry.Short_Description + '</li>');
		print('</div>');
	}
	print('</ul>');
?>

Alternatively, users may use a more simplified syntax as follows:

<?
	for each (salsa_entry in results){
	?>
	 <div >
		<li><?=salsa_entry.Name_or_Headline?></li>
		<li><?=salsa_entry.Short_Description?></li>
	</div>
	<?
	 }
?>

Continue to Page 4