Jump to content


Check out our Community Blogs

John

Member Since 13 Jul 2006
Offline Last Active Aug 07 2017 01:57 PM
-----

#629901 Online Project For Thesis?

Posted by John on 16 May 2012 - 06:12 AM

Use github. :)
  • 1


#626955 New language. Any advice?

Posted by John on 15 April 2012 - 08:10 AM

And of course, if you have any questions, we are here to help.
  • 1


#626932 New language. Any advice?

Posted by John on 15 April 2012 - 05:58 AM

Well I was thinking about learning PHP. The point is, I want to move from Desktop programming to Web programming. I'd like to use API from website like Facebook/Twitter/Instagram/Youtube/Etc, what is better to learn and to use for this reason? PHP or Javascript? =)


For web development you will absolutely need to learn both. The best way to interact with those API's is probably though PHP, though most API's do have JavaScript libraries you can use.
  • 1


#626717 Degree In Computer Science: Is It Actually Useful?

Posted by John on 13 April 2012 - 12:42 PM

I think it really depends on your chosen field of endeavor after you have acquired your degree. Me, I'm a web developer, I write php, javascript, html/css for some of the largest media publishers on the internet, and there isn't anything I explicitly learned in college that I use in application. I use the data structures and sorting algorithms provided by the language, and when I have a problem, I Google it. One of the biggest things I learned was how to approach a problem and how to learn a new technology quickly, but this wasn't something that was taught, rather something I learned myself after five years of struggling.
  • 2


#626711 I'd Like To Get Into Making Android Apps

Posted by John on 13 April 2012 - 11:58 AM

Feel free to start a new thread in the lounge, I graduated college ~9 months ago with a BS in CS and EE, I'd love to discuss how incredibly useless my degree actually is... :-P
  • 1


#626541 The Http Web Server

Posted by John on 12 April 2012 - 11:34 AM

Very cool. What kind of model do you use to handle multiple (concurrent) requests? Event-driven? Threaded?
  • 1


#626002 How to start developing a website

Posted by John on 08 April 2012 - 07:24 PM

I use a whiteboard with dry erase markers.
  • 1


#606424 Does Git solve this problem?

Posted by John on 04 August 2011 - 08:40 AM

Yes, it will solve your problem. However, there will be (probably more) headaches at first while you are learning to use it, but in the long run, it will pay off. Github provides free public accounts. Try creating an account on there and play around with it before you do all the work of setting it up on your own server.

No, the server does not have to be powerful.
  • 1


#593279 Building a Secure Site in PHP

Posted by John on 06 March 2011 - 01:00 AM

Am I just oblivious to what a model really is? Are they the same thing? Or maybe similar but missing something?

Yes. :)
Generally, models act as a data abstraction. So, usually when working with a RDBMS, there will be a model for each table.

For example I have a database table that stores information about photo albums. So my AlbumsModel provides functions that allow me to easily work with data in that table.
<?php
/**
*
*/
class AlbumsModel {

/**
* Get all albums in the database
*/
function getAlbums()
{
global $wpdb;

$sql = "SELECT * FROM `{$wpdb->prefix}picasso_albums`";
return $wpdb->get_results($sql, OBJECT);
}

/**
* @param $id ID of an album
* @return a single variable from the database or NULL if no results are found
* Get the album specified by $id
*/
function getAlbumById($id)
{
global $wpdb;

$sql = "SELECT * FROM `{$wpdb->prefix}picasso_albums` WHERE `id` = %d";
return $wpdb->get_row($wpdb->prepare($sql, $id));
}

/**
* Get the filenames of the album cover pictures
*/
function getAlbumCovers()
{
global $wpdb;

$sql = "SELECT * FROM `{$wpdb->prefix}picasso_albums`
LEFT OUTER JOIN `{$wpdb->prefix}picasso_pictures`
ON {$wpdb->prefix}picasso_albums.cid = {$wpdb->prefix}picasso_pictures.id";

return $wpdb->get_results($sql);
}


function getAlbumCoverById($id)
{
global $wpdb;

$sql = "SELECT * FROM `{$wpdb->prefix}picasso_albums`
JOIN `{$wpdb->prefix}picasso_pictures`
ON {$wpdb->prefix}picasso_albums.cid = {$wpdb->prefix}picasso_pictures.id
WHERE {$wpdb->prefix}picasso_albums.id = {$id}";

return $wpdb->get_results($sql);

}

/**
* @return false on error or the id of the inserted row on success
* @param $args an array of unescaped "raw" data to be inserted
*/
function addAlbum($args)
{
global $wpdb;
/**
* http://codex.wordpress.org/Function_Reference/wpdb_Class#INSERT_rows
* Both $data columns and $data values should be "raw" (neither should be SQL escaped).
*/

$usedname = $wpdb->get_var("SELECT COUNT(*) FROM
`{$wpdb->prefix}picasso_albums` WHERE name='{$args['name']}'");
if($usedname > 0){
ErrorHelper::getInstance()->setError("The album name already exists.");
return false;
}

if($args['name'] == "") {
ErrorHelper::getInstance()->setError("You must supply an album name.");
return false;
}

if(strlen($args['name']) > 255) {
ErrorHelper::getInstance()->setError("The name must be less than 255 characters.");
return false;
}

$data = array('name' => $args['name'], 'dir' => $args['dir'], 'uid' => $args['uid']);
$wpdb->insert("{$wpdb->prefix}picasso_albums", (array) $data);

return $wpdb->insert_id;//returns false or the row id
}

/**
* @param $id the album id to be deleted
* @return (mixed) FALSE on error or the number of rows affected on success
* NOTE: both 0 and FALSE can be returned so check using === not ==
*/
function deleteAlbum($id)
{
global $wpdb;
$id = (integer) $wpdb->escape($id);
$sql = "DELETE FROM `{$wpdb->prefix}picasso_albums` WHERE `id` = $id";
return $wpdb->query($sql);
}

/**
* @param $aid Album ID
* @param $pid Picture ID
* Set the cover id (cid) of the album ($aid) to the picture id ($pid)
*/
function setAlbumCover($aid, $pid)
{
global $wpdb;
$wpdb->update("{$wpdb->prefix}picasso_albums",
array('cid' => $pid), array('id' => $aid));
}

/**
* @return (integer) The number of albums
* Get the number of albums
*/
function getNumAlbums()
{
global $wpdb;
return $wpdb->get_var("SELECT COUNT(*) FROM `{$wpdb->prefix}picasso_albums`");
}
}

?>


My view [album.php - used in controller below] (which just displays album thumbnails)
<div class="picasso">
<?php

foreach($pictures as $picture) {
$info = pathinfo($picture->filename);
$t = $info['filename'] . "_thumb." . $info['extension'];

echo "<a rel='picasso-album' href='" . PICASSO_UPLOAD_URL . "/{$album->dir}/$picture->filename'>";
echo "<img class='thumb' src='" . PICASSO_UPLOAD_URL . "/{$album->dir}/$t' />";
echo "</a>";
}

?>
</div>

<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("a[rel=picasso-album]").fancybox({
'transitionIn' : 'none',
'transitionOut' : 'none',
'titlePosition' : 'over',
'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? '   ' + title : '') + '</span>';
}
});
});
</script>


The controller and model communicate with each other, and the controller pass data to the view (the view is oblivious to the model [in a true MVC architecture]). Although the following isn't a "true" controller, you can see how all three parts work together.

	function gallery($atts)
{
$this->includeJS = true;

extract(shortcode_atts(array('id' => null), $atts));

if(isset($_GET['id'])) {
if($_GET['id'] == (integer)$_GET['id'])
$id = (integer)$_GET['id'];
}

$albums = explode(",", $id);

if(count($albums) > 1 && $id != null) {
echo "Not supported. Sorry. :'(";
}

else if(count($albums) == 1 && $id != null) {
$album = $this->albumsModel->getAlbumById($id);
$pictures = $this->picturesModel->getPictures($id);
require_once('views/album.php');
}

else {
$albums = $this->albumsModel->getAlbumCovers();
require_once('views/gallery.php');
}
}


Make any more sense? I should really write a tutorial on the MVC architecture.
  • 1


#593193 Building a Secure Site in PHP

Posted by John on 04 March 2011 - 06:44 PM

1
protected function templateRender($page, $variables = array()) {
		foreach($variables as $key => $value) {
			$$key = $value;
		}
		include_once(PATH."application/views/{$page}.php");
	}
The foreach can be replaced with:
extract($variables)

2
protected function stripSlashes() {
		if (get_magic_quotes_gpc()) {
			$types = array('_GET', '_POST', '_COOKIE');
			foreach($types as $type) {
				foreach($GLOBALS[$type] as $key=>$value) {
					$GLOBALS[$type][$key] = stripslashes($value);
				}
			}
		}
	}
This doesn't account for the fact that $_GET['foo'] can be an array. Instead of $GLOBALS[$type][$key] = stripslashes($value); and the two foreach loops you might want to use array_walk_recursive($_GET, 'stripslashes'); (if you are going for your Zend certification, I would start by memorizing ALL the array_ and str_ functions - there are a lot of useful ones that can save you tons of code)

3
die(mysql_error());

Production code should never die() and print diagnostic information to the user. You should present a "Woops, something went wrong." to the user, and you should log the diagnostic information using exceptions or a custom error handler. (PHP Error Handling | Compiled Thoughts | John Ciacia this can be implemented better)

4
	public function __construct() {
// URI
// index.php?class=name&function=name¶ms=name,name
preg_match("/[0-9a-z]+$/i", $_GET['class'], $matches);
$this->defaultClass = DEFAULTCLASS;
$this->fileName = $_GET['class'];
$this->className = $matches[0];
$this->funName = $_GET['function'];
$this->params = explode('/', $_GET['params']);
}

You might be interested in implementing something like this SEF URLs with out mod_rewrite | Compiled Thoughts | John Ciacia

5
I describe a feature here (No require with autoload | Compiled Thoughts | John Ciacia) that a lot of frameworks use to make things a little easier.

6
Here is a *framework* I started a while ago when I started learning about MVC/OOP/design patterns/frameworks/etc...
http://johnciacia.co.../ionManager.zip

7
Where do models fit into this?

8
Your database class is coupled to your framework. It is better to pass parameters to functions rather than rely on a [super] globals or constants.
	public function __construct($host, $user, $pass, $database) {
$this->link = mysql_connect($host, $user, $pass);
if(!$this->link) {
//custom error handling
}
if(!mysql_select_db($database, $this->link)){
//custom error handling
}
}

Also, people often make their db class a singleton since you rarely have more than one connection to a database. Oh, and use PDO. It is much better than your database class could ever be, plus it supports multiple database engines. If you insist on using your own class, at least use mysqli_ functions, they offer improved functionality.
  • -1


#591096 Email Form Not Working

Posted by John on 11 February 2011 - 02:56 PM

@mail($email_to, $email_subject, $email_message, $headers); 

The @ symbol suppresses errors. Removing it may shed more light on your situation.
  • 1


#590525 [NEWBIE ALERT] Putting functions into echo statements/other functions?

Posted by John on 06 February 2011 - 12:50 PM

$sentence = 'Lucas and Amanjot were dancing in the streets.'; 

echo $sentence . "<br />";
echo "The word dancing is in the following position: " . strpos($sentence, "dancing");

  • 1


#587962 Learning python part 2 (syntax and variables)

Posted by John on 16 January 2011 - 06:46 PM

Make the I in If lowercase.

And yes, it is not frowned upon to bump tutorials.
  • 1


#585779 New Years Celebrations?

Posted by John on 01 January 2011 - 08:19 PM

Rude, obese, and ignorant Americans? What a surprise. :-P

Mine consisted of a large quantity of alcohol. Thats about all I can remember.
  • 1


#582612 Girls

Posted by John on 02 December 2010 - 07:15 PM

And if you don't sign a prenuptial agreement, if you lose her, you also lose your money.
  • 1




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download