How do i highlight the option value in select box using php - php

I have record set where iam looping through and displaying options in select box if the record contains a value as status as "Y"
than i have to display the option in red color
echo "<select name='select1' size='10' multiple >";
foreach ($this->arr['record'] as $rows) {
if($rows['status'] == "Y"){
echo "<option value='". $rows['COLA'] . '*' . $rows['COLB'] . "' bgcolor='#ff0000' style = 'bgcolor=red;font-size=5'>".$rows['COLC'].":".$rows['COLD'].":".$rows['COLE'].":".$rows['COLF'].":".$rows['COLG']." </option>";
}else{
echo "<option value='". $rows['COLA'] . '*' . $rows['COLB'] . "' bgcolor='#ff0000' style = 'bgcolor=red;font-size=5'>".$rows['COLC'].":".$rows['COLD'].":".$rows['COLE'].":".$rows['COLF'].":".$rows['COLG']." </option>";
}
}
echo "</select>";
Here if we have status as "Y" than we are want to highlight the entire option . I used the font tag but it is not working can you please tell us what to do

This has to do with HTML, not PHP. Set the selected attribute for the <option>.
In your case:
if($rows['status'] == "Y") {
echo '<option selected="selected" ....
}

please just look on the generated html source code. Your question is really not related to php. Use the source view of your browser or use firebug to determine why your html attributes / css style is not applied.

Derby, you're using the style attribute the wrong way. Inside the style attribute you may specify "inline" CSS, just according to the general CSS rules.
So to highlight the selected item in red use the following code according to your needs:
echo "<option value='". $rows['COLA'] . '*' . $rows['COLB'] . "' style='background-color: red; font-size: 5pt'>".$rows['COLC'].":".$rows['COLD'].":".$rows['COLE'].":".$rows['COLF'].":".$rows['COLG']." </option>";
But please note, as Jason already stated, that drop-down boxes are normally handled by the Operating System, so this style MAY apply but there's no official way to style the drop-down elements.
More Info on how to use inline style sheets may be found here.

Related

How can I turn this row into a link?

I want to make that row a link, but I dont know how to add an a href attribute... That row shows information from a DB and I just want to make ir clickeable and not only as plain text. Thank you
This is the row I wanna turn into a link...
echo '<td>' . $row['nombre'] . '</td>';
Wrap the text in an anchor (a) tag.
echo '<td>' . $row['nombre'] . '</td>';
You should also probably take a look at the W3schools HTML5 tutorial.
Just add the <a> to your HTML, and include the URL in it.
echo "<td><a href='script.php?id={$row['nombre']}'>{$row['nombre']}</a></td>";
echo "<td>
<a href='yourfile.php?subDom=$row['nombre']'</a>
</td>";
Then you can check for if link is clicked, you can trigger action for click. Like below:
if($_GET['whateverComingFromDB'] == "whatDoYouWantToCompare"){
//...Anything you want to happen after click the link
}

how to add custom icon on jquery ui select menu

i use jquery UI Select menu and i want to make a custom icon for every option tag
i use php and mysql to get my icons and elements
i try to use span and give it an icon as a background image but it's not working
<label class="langLabel" for="filesA">Select your language:</label>
<select name="filesA" id="filesA">
<?php
$langs = select("*","lang");
while($lang=fa($langs)){
echo "<option value={$lang['lang_id']} data-class=\"ui-icon-script\">{$lang['lang_full_name']}
</option>";
echo "<span class='test' style='background: url('".
"img/flags2".$lang['lang_flag'] ."') />";
}
?>
</select>
Please have a look at the following jQuery UI demo.
The first example is most likely what you are looking for.
The source code is available too.

PHP drop down from JSON populating, but text is invisible

I am attempting to create a drop down of items from a JSON file located on a remote server. The drop down appears to be populating (as there are options to choose from), but the text is not visible. I have attempted changing the style color (worth a try, right?) and multiple browsers.
<?php
echo '<select name="version" style="width: 300px">';
$url = 'http://s3.amazonaws.com/Minecraft.Download/versions/versions.json';
$jsonData = file_get_contents($url);
$jsonDataObject = json_decode($jsonData);
foreach($jsonDataObject->versions as $option){
echo '<option value=' . $option->type . '</option>';
}
echo '</select>';
?>
Thanks in advance for any assistance offered.
You are not populating the display text
echo "<option value= { $option->type } >{$option->type}</option>";
This is a little different from your statement, but the logic is the same. You need to write some text between option tags
<option value="val">displayText</option>
It should be something like-
echo '<option value=' . $option->type . '>'.$SOME_VALUE_HERE.'</option>';
// ^

php var value contains quotes

So... I have a mysql_fetch_array and I'm running into an issue when some of the mysql data contains single or double quotes. This is the dumbed down version of my code:
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(\"" . $row['item'] . "\");'>" . $row['item'];
echo "</td></tr>";
}
The edit_form() function is used to send the value of the current item back to the value of the input in the form so the user can then easily edit their entry and then sends an UPDATE command to mysql along with the proper primary key id (which I left out because of irrelevancy). The only issue I have is if a user puts single or double quotes into the form then it messes up the onclick attribute. Please help!! I am pretty new to php and can't figure this out. I've messed around with htmlentites() and html_entity_decode() but am still getting no where. Thank you so much!
Use htmlspecialchars on $row['item'] before inserting it in your document.
So your "dumbed-down" code should be:
while($row=mysql_fetch_array($list)) {
$item = htmlspecialchars($row['item']);
echo "<tr>";
echo "<td onclick='edit_form(\"" . $item . "\");'>" . $item;
echo "</td></tr>";
}
Try the below line instead of the original in your code and see if it works:
echo "<td onclick='edit_form(\"" . str_replace('"',"&quote;",$row['item']) . "\");\">" . $row['item'];
And where you would like to display the $item field, just replace it in reverse if you get &quote; instead of ' " '. For example:
$qoute_free= str_replace('&quote','"',$passed_value);
If you are using it in javascript, the function can be as below:
function edit_form(passed_value)
{
new_value=passed_value.replace(/&quote;/g,'"');
}
I advise using json_encode. That way you don't have to worry about special cases htmlspecialchars might miss (such as newlines).
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(" . json_encode($row['item'], JSON_HEX_APOS) . ");'>" . $row['item'];
echo "</td></tr>";
}
I'm just putting the values back into my <form> (when they click on a <td>) and when they hit SUBMIT it directs the data to a different .php file that uses a MySQL UPDATE instead of an INSERT. I finally found some code that works!
$list is a mysql_query I ran at the top of the document
$item is the title of one of my columns in MySQL
while($row=mysql_fetch_array($list)){
$item = json_encode($row['item']);
$item = str_replace("'","'",$item);
echo "<td onclick='edit_form(" . $item . ");'>" . $row['item'] . "</td>";
}
Now it display's correctly in the <form> when <td> is clicked (by jQuery input.val($item)) and it shows up in the table correctly via $row['item']. I don't really understand exactly how it's working (how the encode is making it work) but I am glad it is. Crazyness!!!! Thanks for responding! and thanks for your effort!!

Having trouble with echoing HTML in php script

I am trying to make a form that show users name and I am having a little trouble with this part. The ' ' and " " marks dont quite work how they are supposed to. Im trying to echo the options in drop down menu and some how the $wholenames and the last " sign appear in the wrong part of the page. Could someone please tell me what is the correct way of doing this?
Thanks
echo' "<option>'; echo $wholenames; echo'</option>"';
Actually I had looked it wrong it is a little bit more complex. Below you can see the code. The whole dropdown menu does not appear. The wholenames integer appears, but the menu does not...
echo'
<label for="addusertogroup">Add user to an existing group:</label>
<select name="addusertogroup" id="addusertogroup">
'; if(mysql_num_rows($userresult))
{
while($row2 = mysql_fetch_assoc($userresult))
{
$wholename = array("$row2[f_name] $row2[s_name]");
foreach ($wholename as $wholenames) {
echo "<option>$wholenames</option>";
}
}
}
else {
echo "<option>No Names Present</option>";
}
To make it work, simply do this:
echo "<option>";
echo $wholenames;
echo "<option>";
or this:
echo "<option>$wholenames</option>";
or this:
echo '<option>'.$wholenames.'</option>';
All will work, just up to you which one you pick.
You shouldn't have " before <option> and after </option>
It should be something like
echo "<option>". echo $wholenames; echo "</option>";
Also, if $wholenames is an array, you'd better iterate over it:
foreach ($wholenames as $name){
echo "<option>". echo $name; echo "</option>";
}
Any text for options in a HTML SELECT box are written inside the tag. If you don't put your text between the <option> tags the browser will try to insert it to the select box's DOM.
So you could change your code to this:
echo '<option value="myvalue">"' . $wholenames . '"</option>';
Haven't actually tested this code.
Update if the Quotation mark was not meant to be in the output you would simply need to write:
echo '<option value="myvalue">' . $wholenames . '</option>';
In php you can use both " " and ' ' with strings.

Categories