PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Dec 15, 2018 12:29 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sat Oct 06, 2018 8:50 am 
Offline
Forum Contributor

Joined: Thu Feb 23, 2012 7:51 am
Posts: 301
Hello:

I hope this is the correct forum for this question.

I am trying to construct a dynamic <select><option> list based off of results from a SELECT statement. And I'm running into problems. Specifically - the final output looks like the attached .png file.

There is an extra \ slash in the last </option> tag. Even though I don't have the \ slash in the code snippet. Following is my code snippet. Does anyone have any idea why the forward slash is being inserted into my </option> tag????

Syntax: [ Download ] [ Hide ]
$prep_LogAccess = $link->prepare($select_LogAccess);
                $prep_LogAccess->execute(array());
       
                $list = "<option value=''>test</option>";
       
        $LogAccess_Results = $prep_LogAccess->fetchAll();
        foreach($LogAccess_Results as $LogAccess) {
        $list = $list .  "<option class='optPrivileges' value=" . $LogAccess['ActivityCode'] . ">" . stripslashes($LogAccess['ActivityDesc'])  . "</option>";
        }
               
        echo(json_encode($list));


Thanks in advance - Pavilion


Attachments:
selectOption.png
selectOption.png [ 45.44 KiB | Viewed 1017 times ]
Top
 Profile  
 
PostPosted: Sat Oct 06, 2018 9:39 am 
Offline
Forum Contributor

Joined: Thu Feb 23, 2012 7:51 am
Posts: 301
Figured it out. I don't need to json_encode() the $list variable. Once I simply echoed it, all was well...


Top
 Profile  
 
PostPosted: Wed Oct 31, 2018 2:45 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 367
off-topic advice: avoid bouncing back and forth between html and php as much as possible it will save you from countless small bugs. Consider using and array to contain list items. You may find it useful to have them in an array for various reasons in the future.

Syntax: [ Download ] [ Hide ]
$list = $list .  "<option class='optPrivileges' value=" . $LogAccess['ActivityCode'] . ">" . stripslashes($LogAccess['ActivityDesc'])  . "</option>";


Syntax: [ Download ] [ Hide ]
$list = [];
$option_template = '<option class="optPrivileges" value="%s"> %s </option>';
foreach( $foo as $bar ) {
    $list[] = sprintf( $option_template , $LogAccess['ActivityCode'] , stripslashes($LogAccess['ActivityDesc']) );
}

echo implode('',$list);
 

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Exabot [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group