Jump to content

Select dialog box doesn't display top entry


Go to solution Solved by Sepodati,

Recommended Posts

I have a php program that calls a function in my class to populate a select tag for display in the page.  For some reason I cannot seem to discover, it retrieves the information, it correctly creates the option tags, but then when it displays the dropdown dialog box, the very top entry is missing.  I sure could use another set of eyes on this to tell me what I'm missing as I know it has to be something simple.  

 

Here is the php page code that calls the function:

if ($redisp==1) {
    $table_ex_entries .= "<table><tr>";
    for ($x=0; $x<10; $x++) {
        //get the categories list with the selected one highlighted in the list
        $selected = $page->getGroupsForEntriesForm3($_SESSION['UID'],$entry_cat[$x]);
        //get the value for the off hours radio button
        if (isset($entry_offhrs[$x]) && $entry_offhrs[$x]=='y') {
            $set_checked_y = "checked='checked'";
            $set_checked_n = "";
        } else {
            if (isset($entry_offhrs[$x]) && $entry_offhrs[$x]=='n') {
                $set_checked_n = "checked='checked'";
                $set_checked_y = "";
            } else {
                $set_checked_n = "checked='checked'";
                $set_checked_y = "";
            }
        }
        $table_ex_entries .= "<tr>";
        $table_ex_entries .= "<td><input size='12' name='date" .   $x . "' type='text'  value='"    . $entry_date[$x] . "' id='datetimepicker" . $x . "'></td>";
        $table_ex_entries .= "<td><input size='12' name='id" .     $x . "' type='text'  value='"    . $entry_id[$x]   . "'></td>";
        $table_ex_entries .= "<td><input size='18' name='name" .   $x . "' type='text'  value='"    . $entry_name[$x] . "'></td>";
        $table_ex_entries .= "<td><select width='250' name='cat" . $x . "'              value='"    . $selected       . "</select></td>";
        $table_ex_entries .= "<td><input size='43' name='desc" .   $x . "' type='text'  value='"    . $entry_desc[$x] . "'></td>";
        $table_ex_entries .= "<td><input size='6'  name='hrs" .    $x . "' type='text'  value='"    . $entry_hrs[$x]  . "'></td>";
        $table_ex_entries .= "<td>Yes<input name='offhrs" .        $x . "' type='radio' value='y' " . $set_checked_y  . ">  
                                   No <input name='offhrs" .       $x . "' type='radio' value='n' " . $set_checked_n  . "></td>";
        $table_ex_entries .= "</tr>";
    }
    $table_ex_entries .= "</table>";
}

Here is the div in the html body (it displays fine as I have some javascript that determines which div to display based on the $redisp==1 show in the above code snippet)

        <div id="re_entry" style="display:none">
            <p align="center">
                *<b>Date/Category/Description/Hours are REQUIRED fields</b> <br>Please correct your entries and click Add Entries again to proceed. <br>
            </p>
            <?php echo $table_header; ?>
            <?php echo $table_ex_entries; ?>
        </div>

And here is the function that it calls to create the option tags for the select tag in the first set of code building the html table.  Oh and I do have categories preferences so it is retrieving just my list not the whole list in this function.

	public function getGroupsForEntriesForm3($uid,$category)
	{
		$out = '';
		$cats = array();
		$cat_id = array();
		$catname = array();
		$dbh = $this->rdb;
                //check to see if the user has set preferences for their list of categories
		$dbh->query("SELECT Categories FROM Users WHERE ID = :id");
		$dbh->bind(':id', $uid);
		$row = $dbh->single();
		$user_cats = (isset($row['Categories']) ? $row['Categories'] : '');
		if ($user_cats == '') {
                        //no special preference for categories so go get them all after getting the user's group ID
			$dbh->query("SELECT GID FROM Users WHERE ID = :id");
			$dbh->bind(':id', $uid);
			$row = $dbh->single();
			$gid = $row['GID'];
			$cats = $this->getCategories($gid);
		} else {
                        //user has preference for categories so only go get the ones they have in their preference list to create the select options
			$dbh->query("SELECT ID, Name FROM Categories WHERE ID IN ($user_cats) ORDER BY Name");
			$rows = $dbh->resultset();
			$xx=0;
			foreach ($rows as $row) {
				$catname[$xx] = $row['Name'];
				$cat_id[$xx] = $row['ID'];
				$xx++;
			}
		}
		$dbh = NULL;
		$select_opt = "";
		for ($xx=0; $xx<count($rows); $xx++) {
			//echo "category ID inside the function is set to " . $category . "<br>";
			//echo "catname while while creating the select options set to " . $catname[$xx] . "<br>";
			if ($category == $cat_id[$xx]) {
				//echo "match category is " . $category . " and cat_id[xx] is " . $cat_id[$xx] . " and catname[xx] is set to " . $catname[$xx] . "  and xx is set to " . $xx . "<br>";
				$select_opt .= "<option selected value='" . $cat_id[$xx] . "'>" . $catname[$xx] . "</option>";
			} else {
				//echo "category is " . $category . " and cat_id[xx] is " . $cat_id[$xx] . " and catname[xx] is set to " . $catname[$xx] . "  and xx is set to " . $xx . "<br>";
				$select_opt .= "<option value='" . $cat_id[$xx] . "'>" . $catname[$xx] . "</option>";
			}
		}
		return $select_opt;
	}

Thanks in advance! 

Ches

Edited by Cheslia
Link to post
Share on other sites

1 - you run your query before preparing it

2 - you bind the values before preparing it and executing it

3 - what is the single function?

 

 

Where do you loop thru the results to build the set of option tags for your select?  Perhaps you could have pointed out the SPECIFIC part of the code that is wrong besides the issues that I pointed out.

Edited by ginerjm
Link to post
Share on other sites
  • Solution

$table_ex_entries .= "<td><select width='250' name='cat" . $x . "'              value='"    . $selected       . "</select></td>";
should be

 

$table_ex_entries .= "<td><select width='250' name='cat" . $x . "'>" . $selected . "</select></td>";
-John
Link to post
Share on other sites

OMG John that was it.  So simple and so odd that that would make a difference, thank you so much!!!  So why would the value= cause it to drop the first element in the select options like that?  

Edited by Cheslia
Link to post
Share on other sites

Because your markup is tag soup. It's not even syntactically valid, so the browser can only render it on a best-effort basis.

 

Since the value attribute is never closed, it goes all the way to the next attribute of the next element, swallowing that element. Everything after that is of course nonsense, but the browser manages to recover from that to at least render the other options.

 

Always validate your markup, learn to write proper HTML and HTML-escape all PHP values. Right now, this is a complete mess, and you should be more surprised that the browser displays anything at all.

Link to post
Share on other sites

You're missing the point. I'm not talking about your syntax error. I'm giving you an approach for systematically avoiding errors and improving your code.

 

It's great that you've fixed this one error. But there will be many, many other errors in the future. Of course you could just keep coming back and asking others to debug them for you, but I'm afraid that will be very frustrating for everybody after a while. Or you can do the smart thing and try to learn from the mistake, so that one day you will be able to fix things yourself.

  • Like 1
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.