PHP Help - Added a default value to blank field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChuckAkins
Forum Newbie
Posts: 5
Joined: Wed Jun 30, 2010 3:24 pm

PHP Help - Added a default value to blank field

Post by ChuckAkins »

I am working on a site and need to add a value to fields that are blank or null within the database.

E.g. if XXX field is empty add a marketing link as an upgrade package etc.

Here is the code:

Code: Select all

$em_columns = array(
					"logo" =>array("header"=>"Logo", "type"=>"label", "title"=>"Logo"),
					"contact_name" =>array("header"=>"Contact Name", "type"=>"label", "title"=>"Contact Name"),
					"company" =>array("header"=>"Company", "type"=>"label",  "title"=>"Company Name"),
					"city" =>array("header"=>"City", "type"=>"label", "title"=>"City"), 
					"state" =>array("header"=>"State", "type"=>"label", "title"=>"State"),
					"zip" =>array("header"=>"Zip", "type"=>"label", "title"=>"Zip"), 
					"county" =>array("header"=>"County", "type"=>"label", "title"=>"County"),
					"phone" =>array("header"=>"Phone", "type"=>"label", "title"=>"Phone"),
					"website" =>array("header"=>"Website", "type"=>"label", "title"=>"Website"),
					"sic_desc" =>array("header"=>"Company Description", "type"=>"label", "title"=>"Company Description"),
					"about" =>array("header"=>"About", "type"=>"label", "title"=>"About"),
				  );

Thanks in advance for any help!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Help - Added a default value to blank field

Post by AbraCadaver »

You didn't give any explanation for your array, so here is a wild guess:

Code: Select all

foreach($em_columns as $column => $array) {
	// UPDATE table_name SET $column = 'marketing link etc' WHERE $column = '' OR $column IS NULL
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ChuckAkins
Forum Newbie
Posts: 5
Joined: Wed Jun 30, 2010 3:24 pm

Re: PHP Help - Added a default value to blank field

Post by ChuckAkins »

Shawn,

Thanks for the reply.

The table displays information from a database in a directory format. This particular array is to display the 'more details' on each company. The goal is to create links to a marketing page for a premium listing. (e.g. having a published logo in your listing cost $$).

I will try your code. I am somewhat of a PHP novice (enough to use it, but not enough to write it). Where do I need to put this? Inside the array?

Thanks very much for your help!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Help - Added a default value to blank field

Post by AbraCadaver »

No, don't use what I gave you. You're talking about displaying a link and not updating the database. You need to show your display code and comment where you want to display a link etc...
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ChuckAkins
Forum Newbie
Posts: 5
Joined: Wed Jun 30, 2010 3:24 pm

Re: PHP Help - Added a default value to blank field

Post by ChuckAkins »

This is part of the datagrid system, so there really is no display portion. If you change that array it updates automatically based on the data from the database. It's not like a typical database page in that you set the parameters then write out HTML display code.

That is where I am stuck.

And have no idea what to do.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Help - Added a default value to blank field

Post by Jonah Bron »

Something like this?

Code: Select all

foreach ($em_columns as $em_key => $em_column) {
    foreach ($em_column as $key => $field) {
        if (empty($field)) { // if field is empty...
            $em_columns[$em_key][$key] = "blah blah"; // put whatever you want into it
        }
    }
}
ChuckAkins
Forum Newbie
Posts: 5
Joined: Wed Jun 30, 2010 3:24 pm

Re: PHP Help - Added a default value to blank field

Post by ChuckAkins »

Where would I put this in to test it?
ChuckAkins
Forum Newbie
Posts: 5
Joined: Wed Jun 30, 2010 3:24 pm

Re: PHP Help - Added a default value to blank field

Post by ChuckAkins »

Bump for help!

Thanks guys!
Post Reply