Jump to content

Insert Fake Data In A Select


ckrudelux

Recommended Posts

I was just thinking if I could add a record which don't exists in the database in my select. Reason for this is I have a list and the first record should be "none" unless I have pre set what some other value should be called first the none record should not exists. Like this:

<select><option>none</option><option>record 1</option><option>record 2</option>etc..</select>

And if a specific record is selected as first none should be gone.

<select><option>record 4</option><option>record 1</option><option>record 2</option>etc..</select>

Link to comment
Share on other sites

Just put that option outside the loop when you're generating the list. Here's an example in PHP:

echo '<select><option value="none">None</option>'; while() { // Database loop here  echo "<option value=\"{$row['data'}\">{$row['data'}</option>";}echo '</select>';

Link to comment
Share on other sites

Just put that option outside the loop when you're generating the list. Here's an example in PHP:
echo '<select><option value="none">None</option>';  while() { // Database loop here  echo "<option value=\"{$row['data'}\">{$row['data'}</option>";}echo '</select>';

What is how it already looks, just didn't like the idea of adding yet another if statement. But the brackets thing is new to me.. Guessing it's so that you could use the array variable inside of the apostrophes. Thanks anyways :)
Link to comment
Share on other sites

Only with double-quotes. Single-quoted strings don't do that. So what you want is for the first element to be "none" only if you haven't specified a specific row in the database? How is this special row identified?

Link to comment
Share on other sites

Only with double-quotes. Single-quoted strings don't do that. So what you want is for the first element to be "none" only if you haven't specified a specific row in the database? How is this special row identified?
$orderby = (isset($_GET['edittask'])) ? "CASE WHEN id='".$id."' THEN 0 ELSE 1 END, `name`" : '`name`';$query = mysql_query("SELECT * FROM lib_todo_area WHERE user_id='".USER_ID."' ORDER BY $orderby");

This is how I select a specific row to be first in the order. Maybe the if statement is a good Idea or maybe including a fake record is I don't really know scenes I don't know how I would do that. $id will be an array variable later haven written what part yet*

Link to comment
Share on other sites

Just putting a condition before the loop will work:
echo '<select>';  if(!isset($_GET['edittask'])) {  echo '<option>none</option>';}while() {

Okay xD The question was not how to add a if statement but how to insert a fake data row.I'm sorry if you didn't understand my question, but I will try to be more clear next time ;)
Link to comment
Share on other sites

At the moment I don't know a way to create fake records, but SQL is a pretty complex language. I was just thinking about a way to solve a specific problem, which doesn't necessarily need to involve creating temporary records.

Link to comment
Share on other sites

If you really want to create a temporary row you can just UNION an additional SELECT with literals and AS clauses to make the field names match.1 But that would be a pretty clumsy solution, and in fact you shouldn't really be relying on the order of the options to induce a "default"2 - you should explicitly put that in your code by using the selected attribute on your chosen option. This logic can be implemented in PHP by having a conditional that checkes whether the option being printed out matches the desired value, and then printing the attribute. You can have an additional boolean that is flipped when that happens, and if by the end it isn't you print the fallback default with the selected attribute:

$default = empty($_GET['edittask']) ? "" : $_GET['edittask'];$query = mysql_query("SELECT * FROM lib_todo_area WHERE user_id='".USER_ID."'");$had_def = false;echo "<select>";while($row = mysql_fetch_assoc($query)) { // Database loop here    $selected = $row['id'] == $default ? "selected=\"selected\" " : "";    if ($selected) $had_def = true;    echo "<option {$selected}value=\"{$row['id']}\">{$row['name']}</option>";}if (!$had_def) echo "<option selected=\"selected\" value=\"0\">None</option>";echo '</select>';

---1. Just for fun, this is how you can union an additional row onto your data:

SELECT a, b, c FROM table UNION SELECT 1 a, "hello" b, 3 c

2. As this may not work for all user agents, especially mobile ones.

Link to comment
Share on other sites

If you really want to create a temporary row you can just UNION an additional SELECT with literals and AS clauses to make the field names match.1 But that would be a pretty clumsy solution, and in fact you shouldn't really be relying on the order of the options to induce a "default"2 - you should explicitly put that in your code by using the selected attribute on your chosen option. This logic can be implemented in PHP by having a conditional that checkes whether the option being printed out matches the desired value, and then printing the attribute. You can have an additional boolean that is flipped when that happens, and if by the end it isn't you print the fallback default with the selected attribute:
$default = empty($_GET['edittask']) ? "" : $_GET['edittask'];$query = mysql_query("SELECT * FROM lib_todo_area WHERE user_id='".USER_ID."'");$had_def = false; echo "<select>";while($row = mysql_fetch_assoc($query)) { // Database loop here	$selected = $row['id'] == $default ? "selected=\"selected\" " : "";	if ($selected) $had_def = true;	echo "<option {$selected}value=\"{$row['id']}\">{$row['name']}</option>";}if (!$had_def) echo "<option selected=\"selected\" value=\"0\">None</option>";echo '</select>';

--- 1. Just for fun, this is how you can union an additional row onto your data:

SELECT a, b, c FROM table UNION SELECT 1 a, "hello" b, 3 c

2. As this may not work for all user agents, especially mobile ones.

I never thought of using selected attribute for this and is a better option when doing complicated SQL selections. Actually never seen it used on select element. It's not referenced on w3schools.com Could be a good add to the list. Always something new to learn xD Ingolme appreciate your help and yes I know it not the only solution I already got a working solution then I posted like the one you wrote, I was just wondering about the other method.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...