How do I...

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
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

How do I...

Post by barb woolums »

I am populating a select option list from a db using the following code:

Code: Select all

$list = $con->query("SELECT * FROM query_owner_recipes_with_name_id($uid)")->fetchAll(PDO::FETCH_ASSOC);
	
$response['list'] = $list;
	
echo json_encode($response);
which produces JSON like this

"list": [
{
"column2": "text",
"column1": number
},....
]

I would like to add a placeholder as the first option like "choose one ..." with no value. Could someone please tell me the best way to do this?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I...

Post by requinix »

Manually add it to the SELECT? Sure you could add it to the $list but maybe there's a better option. Depends how you're getting that JSON into the list.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: How do I...

Post by barb woolums »

Adding to the select sounds like a better option, but how would I do that?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do I...

Post by Benjamin »

Code: Select all

$list = array_merge(array('' => '-- Choose One --'), $list);
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: How do I...

Post by barb woolums »

Benjamin

I tried that and the json looks totally different:
....
},
"237": {
"column1": 86356,
"column2": "Wool Mix"
},
"": "Choose a recipe..."
}
}



the new entry is added at the end and the only option I end up with in my select after mapping is the placeholder
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do I...

Post by Benjamin »

Please post the entire code block.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: How do I...

Post by barb woolums »

Below is the entire code block, this script is being called via a REST service in a mobile app which then maps the json returned into a select list. It works perfectly. and returns JSON in the below format.

So I need to insert the placeholder so it ends up in the same format, and at the beginning.

{
"list": [
{
"column2": "Ajoblanco con Uvas (Garlic Soup with Grapes)",
"column1": 86127
},
{
"column2": "All-American Meat Loaf",
"column1": 86128
},
{
"column2": "All-American Meat Loaf",
"column1": 86018
}
}

Code: Select all

include_once("../../dbvars.php");
	
	try {  
		$con = new PDO("pgsql:user=$dbuser dbname=$dbrecipes password=$dbpass");  
	}  
		catch(PDOException $e) {  
		$response["message"] = $e->getMessage();
		exit;
	}	
 
    $user=$_GET['user'];
	
	
    $rsuid=$con->query("SELECT id from owner where owner='$user'");
	foreach ($rsuid as $row) {
		$uid = $row['id'];
	}
	
	$list = $con->query("SELECT * FROM query_owner_recipes_with_name_id($uid)")->fetchAll(PDO::FETCH_ASSOC);
	
	$response['list'] = $list;
	
	echo json_encode($response);
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do I...

Post by Benjamin »

Ok, so is this what you tried?

Code: Select all

	$list = $con->query("SELECT * FROM query_owner_recipes_with_name_id($uid)")->fetchAll(PDO::FETCH_ASSOC);

        $list = array_merge(array('' => '-- Choose One --'), $list);
	
	$response['list'] = $list;
	
	echo json_encode($response);
I am assuming that $list contains an array of rows. The purpose of array_merge is to prepend a row to the beginning of that. You may need to tweak the code depending on the structure of the $list array.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: How do I...

Post by barb woolums »

Yes that is what I tried, and the JSON returned was in a different format than what I need.

That's what I'm trying to find out - how to tweak the code. :)

The list array looks like this

array(160) {
[0]=>
array(2) {
["column1"]=>
int(10160)
["column2"]=>
string(22) "All-American Meat Loaf"
}
[1]=>
array(2) {
["column1"]=>
int(18612)
["column2"]=>
string(14) "Almond Cookies"
}....
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do I...

Post by Benjamin »

Ok, I would practice using array_merge because it comes in handy fairly often.

The page for it is here: http://us2.php.net/manual/en/function.array-merge.php

That said, I think this is what you want:

Code: Select all

$list = array_merge(array('' => array('column1' => '', 'column2' => '-- Choose One--')), $list);
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: How do I...

Post by barb woolums »

You're a star.

That didn't work, but this did

$list = array_merge(array('-1' => array('column1' => '', 'column2' => '-- Choose One--')), $list);
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do I...

Post by Benjamin »

Excellent. I considered using -1 but I didn't know if you needed numeric or ordered indexes or not.
Post Reply