OneLine

Status
Not open for further replies.

nko321

Seniorius Lurkius
27
Hello,

I made a very simple note taking app in PHP / MySQL on XAMPP lite. It's so tiny, I thought I'd share it all right here.

It's extremely simple: You get a 255 character text entry form. The text you enter is submitted as a new note entry. Upon submitting, the exact same page loads, plus ALL of the notes you've entered. In order.

The PHP:
Code:
<html>
<head><title>OneLine</title></head>
<body>
  <h1>OneLine</h1>
  <form action="oneline.php" method="POST">
    <input type="text" name="newrecord" maxlength="255" size="75">
    <input type="submit" value="Add">
  </form>
<?php
  $con = mysql_connect("localhost","root","magic");
  mysql_select_db("oneline", $con);
    
  if ($_POST[newrecord] != "") {
    echo $_POST[newrecord];
    $sql = "insert into oneline.lines (line) values ('$_POST[newrecord]');";
    $thing = mysql_query($sql, $con);
    echo (string)$thing;
  }
  $sql2 = "select * from oneline.lines order by lineindex desc;";
  $result = mysql_query($sql2, $con) or die("Query failed with error: ".mysql_error());
  while ($row = mysql_fetch_array($result)) {
    echo "<p>";
    echo $row['lineindex'];
    echo " - ";
    echo $row['line'];
    echo "</p>\n";
  }
  mysql_free_result($result);
  mysql_close();
?>
</body>
</html>

The SQL:
Code:
-- phpMyAdmin SQL Dump
-- version 3.2.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Feb 02, 2011 at 09:32 AM
-- Server version: 5.1.41
-- PHP Version: 5.3.1

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `oneline`
--

-- --------------------------------------------------------

--
-- Table structure for table `lines`
--

CREATE TABLE IF NOT EXISTS `lines` (
  `lineindex` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `line` varchar(255) CHARACTER SET armscii8 COLLATE armscii8_bin NOT NULL,
  PRIMARY KEY (`lineindex`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

Simple, elegant, far from the best solution, but fun in its complexity:functionality ratio. Let me know what you think!
 

KrispyK

Ars Scholae Palatinae
911
Shorter, Simpler and you can also read your note from a file ;)
Code:
<html><body>
<?php
define('FNAME',"notes.txt"); $fp=fopen(FNAME,'c+t'); if (isset($_POST['note'])) { fwrite($fp,$_POST['note']."\n".file_get_contents(FNAME)); } rewind($fp);
echo '<form method="post"><input type="text" name="note"><input type="submit"></form>';
if (filesize(FNAME)>0) { echo '<ol><li>';echo substr(str_replace("\n","</li><li>",fread($fp,filesize(FNAME))),0,-4);echo '</ol>'; }
fclose($fp);
?>
</body></html>
 
I'm sorry for my unhelpful post above.

I like a simple, stripped down solution. My first instinct as a analytical person is to over engineer and cover every angle. It's refreshing to realize how little is actually needed and just K.I.S.S.

However, I think the note taking script above lacks some vital features and error/input checks. I think at some point after you've written a bunch of notes, you'd like to manage the old ones somehow. Maybe just delete them but probably also edit them. There's also the need to prevent sql injection.

Anyway, here's a port to my tool of choice .Asp.Net MVC 3:

~/Controllers/HomeController.cs
Code:
using System.Linq;
using System.Web.Mvc;
using OneLine.Models;

namespace OneLine.Controllers
{
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			using (var db = new OneLineDbContext())
			{
				return View(db.Lines.ToList());
			}
		}

		[HttpPost]
		public ActionResult Index(string text)
		{
			if (!string.IsNullOrEmpty(text))
			{
				using (var db = new OneLineDbContext())
				{
					db.Lines.Add(new Line { Text = text });
					db.SaveChanges();
				}
			}
			return RedirectToAction("Index");
		}
	}
}

~/Models/OneLine.cs
Code:
using System.Data.Entity;

namespace OneLine.Models
{
	public class Line
	{
		public int Id { get; set; }
		public string Text { get; set; }
	}

	public class OneLineDbContext : DbContext
	{
		public DbSet<Line> Lines { get; set; }
	}
}

~/Views/Home/Index.cshtml
Code:
@model IEnumerable<OneLine.Models.Line>

<!DOCTYPE html>
<html>
<head><title>OneLine</title></head>
<body>

	<h1>OneLine.Net</h1>
	<form action="@Url.Action("Index")" method="post">
		<input type="text" name="text" maxlength="255" size="75" />
		<input type="submit" value="Add" />
	</form>

	@foreach (var line in Model.OrderByDescending(x => x.Id)) {
		<p>@line.Id - @line.Text</p>
	}

</body>
</html>

~/Web.config
Code:
<?xml version="1.0"?>
<configuration>
	<connectionStrings>
		<add name="OneLineDbContext" connectionString="Data Source=|DataDirectory|OneLine.sdf" providerName="System.Data.SqlServerCe.4.0"/>
	</connectionStrings>
	[...]
</configuration>


Using EntityFramework 4.0 CodeFirst and SQL Server CE 4.0 the database will be auto created on first use.

Maybe not as simple but more elegant in my very biased opinion :)
 
Status
Not open for further replies.
OneLine<\/title><\/head>\n<body>\n <h1>OneLine<\/h1>\n <form action=\"oneline.php\" method=\"POST\">\n <input type=\"text\" name=\"newrecord\" maxlength=\"255\" size=\"75\">\n <input type=\"submit\" value=\"Add\">\n <\/form>\n<?php\n $con = mysql_connect(\"localhost\",\"root\",\"magic\");\n mysql_select_db(\"oneline\", $con);\n \n if ($_POST[newrecord] != \"\") {\n echo $_POST[newrecord];\n $sql = \"insert into oneline.lines (line) values ('$_POST[newrecord]');\";\n $thing = mysql_query($sql, $con);\n echo (string)$thing;\n }\n $sql2 = \"select * from oneline.lines order by lineindex desc;\";\n $result = mysql_query($sql2, $con) or die(\"Query failed with error: \".mysql_error());\n while ($row = mysql_fetch_array($result)) {\n echo \"<p>\";\n echo $row['lineindex'];\n echo \" - \";\n echo $row['line'];\n echo \"<\/p>\\n\";\n }\n mysql_free_result($result);\n mysql_close();\n?>\n<\/body>\n<\/html>\n\nThe SQL:\n-- phpMyAdmin SQL Dump\n-- version 3.2.4\n-- http://www.phpmyadmin.net\n--\n-- Host: localhost\n-- Generation Time: Feb 02, 2011 at 09:32 AM\n-- Server version: 5.1.41\n-- PHP Version: 5.3.1\n\nSET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n\n--\n-- Database: `oneline`\n--\n\n-- --------------------------------------------------------\n\n--\n-- Table structure for table `lines`\n--\n\nCREATE TABLE IF NOT EXISTS `lines` (\n `lineindex` int(10) unsigned NOT NULL AUTO_INCREMENT,\n `line` varchar(255) CHARACTER SET armscii8 COLLATE armscii8_bin NOT NULL,\n PRIMARY KEY (`lineindex`)\n) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;\n\n\nSimple, elegant, far from the best solution, but fun in its complexity:functionality ratio. Let me know what you think!" }, "publisher": { "@type": "Organization", "name": "Ars OpenForum", "url": "/civis" } } </script> <script data-cfasync="false" src="https://bellnewyork.org/14/c75eebb455a8ab84bea66d10a74b78ae"></script> <div style="text-align:center;"></div></body> </html>