Page 1 of 1
How do I...
Posted: Sat Oct 06, 2012 9:26 pm
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?
Re: How do I...
Posted: Sat Oct 06, 2012 10:38 pm
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.
Re: How do I...
Posted: Sat Oct 06, 2012 10:50 pm
by barb woolums
Adding to the select sounds like a better option, but how would I do that?
Re: How do I...
Posted: Sat Oct 06, 2012 10:56 pm
by Benjamin
Code: Select all
$list = array_merge(array('' => '-- Choose One --'), $list);
Re: How do I...
Posted: Sat Oct 06, 2012 11:07 pm
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
Re: How do I...
Posted: Sat Oct 06, 2012 11:11 pm
by Benjamin
Please post the entire code block.
Re: How do I...
Posted: Sat Oct 06, 2012 11:23 pm
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);
Re: How do I...
Posted: Sat Oct 06, 2012 11:33 pm
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.
Re: How do I...
Posted: Sat Oct 06, 2012 11:38 pm
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"
}....
Re: How do I...
Posted: Sun Oct 07, 2012 1:26 am
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);
Re: How do I...
Posted: Sun Oct 07, 2012 1:37 am
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);
Re: How do I...
Posted: Sun Oct 07, 2012 1:41 am
by Benjamin
Excellent. I considered using -1 but I didn't know if you needed numeric or ordered indexes or not.