Jump to content

Help with Function


nappyhead

Recommended Posts

I was given this function question for an interview. I don't think I'm interested in the job and therefore will not submit this, but I am curious how to answer this. Once I start something like this, I can't just quit without knowing the answer.

 

 

Questions:
1. Does the function have any errors?
2. What is the purpose of the function?
3. What can be done to improve it?
4. Why is isset() used instead of a simple conditional used above it?
5. What assumptions can you make about the database structure?

function auto_query( $table, $vars ) {
	$_result = $this->query( "EXPLAIN $_table_name" );
	$_fields = array(); // Empty array assigned to variable
	
	while( $_row = $_result->fetchRow() ) {
		array_push( $_fields, $_row['Field'] );  // Push one or more elements onto the end of array
	}
	
	if( $vars[$table . 'id'] ) {
		$_query = "UPDATE $_table SET ";
	} else {
		$_query = "INSERT INTO $_table SET " . $_table . '_date_created = NOW(), ';
	}
	
	$_query_params = array();  // Empty array assigned to variable
	
	foreach( $_fields as $_field ) {
		if( isset( $_vars[$_field] ) ) {
			$_query_params[] = "$_field = " . $this->dbh->quoteSmart( $_vars[$_field] );
		}
	}
	
	$_query .= implode( ',', $_query_params );  // Join array elements with a string
	
	if( $_vars[$_table . '_id'] ) {
		$_query .= " WHERE {$_table}_id = " . $this->dbh->quoteSmart($_vars[$_table . '_id']);
	}
	
	return $_query;
}
Link to comment
https://forums.phpfreaks.com/topic/280085-help-with-function/
Share on other sites

...why are you asking us your interview question?

 

Not to be a dick, but these are questions that you should actually investigate yourself before asking others.  Have you tried running the code?  Do you get any errors or warnings?  Can you trace what it's actually doing with the database?

Link to comment
https://forums.phpfreaks.com/topic/280085-help-with-function/#findComment-1440390
Share on other sites

...why are you asking us your interview question?

 

Not to be a dick, but these are questions that you should actually investigate yourself before asking others.  Have you tried running the code?  Do you get any errors or warnings?  Can you trace what it's actually doing with the database?

 

Haha!  You're not being a dick.  I thought the exact same things, except I questioned the " I don't think I'm interested in the job and therefore will not submit this".   :)

Link to comment
https://forums.phpfreaks.com/topic/280085-help-with-function/#findComment-1440395
Share on other sites

1. $this exists only inside object methods. But you have just some function.

 quoteSmart ??? I know about only a quote() in PDO

not $_vars but $vars$table and $_table.

$vars[$table . 'id']    <-----> $_vars[$_table . '_id']

2. it returns the query for saving some data in database table

3. use Yii's Active record instead. :-)

4. maybe because we have no ELSE logic here

Link to comment
https://forums.phpfreaks.com/topic/280085-help-with-function/#findComment-1440420
Share on other sites

Questions:
1. Does the function have any errors? yes
2. What is the purpose of the function? to be executed
3. What can be done to improve it? lots of stuff
4. Why is isset() used instead of a simple conditional used above it? demonstration of power to oppress
5. What assumptions can you make about the database structure? it has columns and stuff. Also it has those other things.

Link to comment
https://forums.phpfreaks.com/topic/280085-help-with-function/#findComment-1440422
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.