We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Why is my array_keys() not outputting any keys??

FairyBusiness
on
Medium Priority
278 Views
Last Modified: 2012-05-11
Hi, I am trying to send keys with their values but its only sending the values.

var_dump($attributes)  returns:

array(5) { ["id"]=> NULL ["filename"]=> string(12) "56504579.jpg"
["type"]=> string(10) "image/jpeg"
["size"]=> int(46234) ["caption"]=> string(3) "box" }


You can see I am trying to upload images and give them a caption:

http://auroriella.com/photo_upload.php

Anyone know how I can get the key (caption) ??


protected function attributes() {
	// return an array of attribute keys and their values
	$attributes = array();
	foreach(self::$db_fields as $field) {
		if(property_exists($this, $field)) {
			$attributes[$field] = $this->$field; // use dynamic variable
			if($attributes == !NULL) {
				echo var_dump($attributes);
			}
		}
	}
	return $attributes; 
}

protected function sanitized_attributes() {
	global $database;
	$clean_attributes = array();
	// sanitize the values before submitting
	// Note: does not alter the actual value of each attribute
	foreach($this->attributes() as $key => $value) {
		$clean_attributes[key] = $database->clean_strings($value);
	}
	return $clean_attributes; 
}

protected function create() {
	global $database;
	$attributes = $this->sanitized_attributes();
	$adjust = "ALTER TABLE users AUTO_INCREMENT = 1";
	$sql = "INSERT INTO " . self::$table . " (";
	$sql .= join(", ", array_keys($attributes));
	$sql .= ") VALUES ('";
	$sql .= join("', '", array_values($attributes));
	$sql .= "')";
	var_dump($sql);
	$noGaps = $database->query($adjust);
	if($database->query($sql)) {
		$this->id = $database->insert_id(); // Update the variable
		return true;
	}
	else {
		return false;
	}
}

Open in new window

Comment
Watch Question

Aaron TomoskyDirector, SD-WAN Solutions
CERTIFIED EXPERT

Commented:
Foreach ($attributes as $key=>$value{
Dostuff
}

Author

Commented:
It gave me this error message:

Notice: Undefined variable: attributes in /hermes/web09c/b2950/moo.auroriellacom/includes/images.php on line 156

Warning: Invalid argument supplied for foreach() in /hermes/web09c/b2950/moo.auroriellacom/includes/images.php on line 156
Aaron TomoskyDirector, SD-WAN Solutions
CERTIFIED EXPERT

Commented:
Foreach ($attributes as $key=>$value){
//you have to put somethinghere
Echo $key;
Echo '</br>';
echo $value;
}

Author

Commented:
I did this:

foreach($attributes as $key => $value) {
      $clean_attributes[key] = $database->clean_strings($value);
}

will that not have the key too??
Director, SD-WAN Solutions
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Can't believe I didn't catch that! Thanks!!!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.