View Single Post
Old 09-18-2007, 07:33 AM   #2 (permalink)
Shaun
The Contributor
 
Shaun's Avatar
 
Join Date: Sep 2007
Posts: 41
Thanks: 0
Shaun is on a distinguished road
Default

Basically can help you cut down on repetitive code. I'd show an example, but about to go out.
will add more later

EDIT:
Lets say you wanted to update your database, i make a class that includes an 'update' function.

So below i have the query that is going to be sent to the function
PHP Code:
<?php
            $data 
= array(
                    
'movie_title' => $_POST['title'], 
                    
'movie_rating' => $_POST['movie_rating'],
                    
'movie_genre' => $_POST['genre'],
                    
'movie_synopsis' => $_POST['content'],
                    
'movie_link' => $_POST['link'],
                    
'movie_release_date' => $_POST['release_date'],
                    
'movie_running_time' => $_POST['running_time'],
                    
'movie_image' => $image
                    
);    
                
            
Sql::update'movies'$data "movieID = '" .$_POST['movieID']. "'");
?>
As you can see, i have this in an array, which makes it really easy for me to see what's what. Basically the function gets the table first, in my case its "movies", next is the data array im trying to update, and finally, what its got to match, again in my case, i want the "movieID" is the row in which i want to edit.

Next, the function:
PHP Code:
<?php
    
function update$table$data$where){
        
$query "UPDATE ".$table." SET ";
        foreach(
$data as $var => $value){
            if(
$i>0){
                
$query .= ", ";
            }
            
$query .= $var" = '" .$value"'";
            
$i++;
        }
        
$query .=" WHERE ".$where." LIMIT 1;";
        
Sql::query($query);
        
        return 
true;
    }
?>
The foreach grabs all the data from the "$data" array and puts it back together, placing a "," when needed, and then "$where" it matches, it then executes the query using my "Sql::query()" function.

I know this is only brief, but i hope that helps you a little, and im sure their a more advanced PHP coders here that could show an even better way of doing it, as im certainly not advanced lol.

Last edited by Shaun : 09-18-2007 at 11:08 AM. Reason: Added more
Send a message via MSN to Shaun Send a message via Skype™ to Shaun
Shaun is offline  
Reply With Quote