rating stars script - cannot vote again - php

I have a simple rating stars script as given below. My problem is that once the user has put in his vote, he cannot change his vote (if he has a change of mind and wants to change his vote before submitting). My script goes as follows:
$(function() {
$('a').hover(
// Handles the mouseover
function() {
$(this).prevAll().andSelf().addClass('hoverstar');
$(this).nextAll().removeClass('visited');
},
// Handles the mouseout
function() {
$(this).prevAll().andSelf().removeClass('hoverstar');
$(this).nextAll('a').removeClass('hoverstar');
$(this).nextAll('a').removeClass('visited');
}
);
$('a').click(
function() {
$(this).prevAll('a').andSelf().addClass('visited');
$(this).nextAll('a').removeClass('visited');
}
);
});
The style-sheet is as follows:
.rate_widget ul
{
background: url('star-white16.gif') repeat-x;
}
.rate_widget a.visited{
background: url('star-red16.gif') repeat-x;
}
.rate_widget a.hoverstar{
background: url('star-gold16.gif') repeat-x;
}
The stars are displayed as links as follows:
<div class='rate_widget'>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>

This isn't the best of code, but it should put you on the right track:
$('a').click(
function() {
if ($(this).parent().parent().attr('voted') !== '1') {
$(this).prevAll('a').andSelf().addClass('visited');
$(this).nextAll('a').removeClass('visited');
$(this).parent().parent().attr('voted', '1');
}
}
);
Essentially what it does is when a click is done to vote it adds an attribute "voted" to the UL element. When clicked again if that value is set then it does not allow a vote to happen again. You would need to check against this when hovering as well so that it does not highlight the stars again.
Ideally you would want something neater than .parent().parent(). If you have a single star rating on the page rather use an id.

I have a pretty simple rating system that will communicate with PHP just fine if you add the $post() in place of the alert. The thing to notice is the star graphic is an inverted PNG.
.star {
float: left;
width: 21px;
height: 20px;
background-image: url('http://www.libertyeaglearms.com/graphics/star.png');
}
The area outside the star is white so when you change the background color via css the star appears to change color. Take a look and see if this will work for ya.
JSFIDDLE

Related

Hide element via css when another element is present on the page

I want to hide an element if one element is already present on the page.
If element with the id:
#wcfmmp_store_about
is present on the page, then hide following element:
.left_sidebar.widget-area.sidebar
So I've found a solution how to do this via js. but I need to this via .css, if possible
var element = document.getElementById('wcfmmp_store_about');
if (typeof(element) != 'undefined' && element != null)
{
document.querySelector('.left_sidebar.widget-area.sidebar').style.display = "none";
} else {
document.querySelector('.left_sidebar.widget-area.sidebar').style.display = "block";
}
Yes you can kind of do this. Here's an example, but it is not full proof though.
This only works if .tohide follows directly after .item or if .tohide follows .item as a sibling.
.item + .tohide {
display: none;
}
.item ~ .tohide {
display: none;
}
<div class="item">item</div>
<div class="tohide">I show up when item is gone</div>
So in this example, if .item exists on the page, then .tohide is hidden. Go ahead and remove <div class="item">item</div> and run it and you will see that .tohide appears now. You can play with it in this jsfiddle: https://jsfiddle.net/qzu7hkcg/1/

Animation of water in cylinder with jquery based on mySQL variables

I need to create an animation of the water-level in a tank, based on MySQL Variables, which I get every 5 seconds from a database.
The value is from 1 to 100, and it means %, so the goal is, that depending on the % value the water moves smoothly up or down.
So far I have this:
script to check every 5000ms the value from database :
$(document).ready(function () {setInterval(function() {$.get("http://www.h2o-info.com/betapage/script_water_detail.php", function (result) {$('#water_detail').html(result);});}, 5000); });
and show the result in a div:
<div id="water_detail"></div>
script_water_detail.php :
<?php
// Connect to MySQL
$link = mysql_connect( 'xxx', 'xxx', 'xxx' );
if ( !$link ) {
die( 'Could not connect: ' . mysql_error() );
}
// Select the data base
$db = mysql_select_db( 'xxx', $link );
if ( !$db ) {
die ( 'Error selecting database \'xxx\' : ' . mysql_error() );
}
$query = mysql_query("select V_00 from SensorLog where S_ID = 1 ORDER BY ID DESC LIMIT 1;");
while ($row = mysql_fetch_array($query))
{
?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#water_detailed").css({
height:'<?php echo round ($row['V_00'], 0); ?>px'})
});
</script>
<?php
if($row['V_00']<='0'){
print "<div class='water_fill_1'>";
echo round ($row['V_00'], 0);
"</div>";
}
elseif ($row['V_00']<='2'){
print "<div class='water_fill_5'>";
echo round ($row['V_00'], 0);
"</div>";
}else{
print "<div id='water_detailed'></div>";
}
} // End while loop
?>
the css properties :
#water_detailed {
background: #e2f4ff; /* Old browsers */
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2UyZjRmZiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjQ3JSIgc3RvcC1jb2xvcj0iI2ExZGJmZiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiMwMGIwZmMiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(top, #e2f4ff 0%, #a1dbff 47%, #00b0fc 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#e2f4ff), color-stop(47%,#a1dbff), color-stop(100%,#00b0fc)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #e2f4ff 0%,#a1dbff 47%,#00b0fc 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #e2f4ff 0%,#a1dbff 47%,#00b0fc 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #e2f4ff 0%,#a1dbff 47%,#00b0fc 100%); /* IE10+ */
background: linear-gradient(to bottom, #e2f4ff 0%,#a1dbff 47%,#00b0fc 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#e2f4ff', endColorstr='#00b0fc',GradientType=0 ); /* IE6-8 */
width: 280px;
height:0;
float:right;
color: #fff;
text-align: center;
vertical-align: middle;
line-height: 200px;
font-size: 20px;
}
#water_detail {
width: 280px;
margin: -275px 100px 0 0;
float: right;
}
So far I have the following result:
Betalink
How you can see, it doesn't work properly, how I said first, it should on the first page load smoothly "fill" from bottom to top regarding the actual value and then change up or down, depending on the values of the database.
Perhaps someone has any suggestions.
Imho the easiest solution would be to have a white div and put a blue child div into it, which has it's margin-top value from your database percentage
(Plus the parent div has to have it's overflow set to hidden, not showing the hidden water part).
Have a look at this jsfiddle to see it in action.
Code
HTML:
<div id="water_detail">
<div id="water_level"></div>
<div>
CSS:
#water_detail { height:200px; background:white; overflow:hidden; width:250px; }
#water_level { height:200px; background:blue; margin-top:200px;/*initially*/ }
Javascript:
$(document).ready(function () {
setInterval(function() {
$.get("http://www.h2o-info.com/betapage/script_water_detail.php", function (result) {
//$('#water_detail').html(result);
$('#water_level').animate({'margin-top' : (200-result*2)+'px'});
});
},5000);
});
The (200-result*2) is only due to the div being "twice the height" of your max value (100% = 200px). Possibly a more "accurate" way would be to write: div_width - (result/100 * div_width)
(In this case a level of 0% results in margin-top:200px, 50% in margin-top:100px, etc.)
How to make it look nicer
Set a static image as the childs background, instead of just a single color
Even better: Take an animated gif that moves the way water does
Working Example
See this jsfiddle for an example of a water level animation (with random water levels).
Solution, Specific To OP
$(document).ready(function () {
setInterval(function() {
$.get("http://www.h2o-info.com/betapage/script_water_detail.php", function (result) {
$('#water_level').animate( {'margin-top' : (200-result*2)+'px' } );
});
},2000);
});
This gets the water level every 2 seconds! Change the last 2000 to what you prefer, it is just the interval (to apply the script) in milliseconds.
Try using jQuery.animate() to make it appear that the water is filling up.
Use a blue <div> which is under (ie: covered by) a grey <div>. For this example, lets set both <div> height to 100 px. Hence reducing the grey <div> height would uncover more of the blue <div>.
Next, parse the output from PHP and on success, run $("#grey_div").animate( {"height": 100 - parseInt(result_from_php)} );
You are echoing so much inside script_water_detail.php.
You should just echo the % of water in this script using which you can animate the water level inside the jQuery function.
The animation shouldn't be the hard part, it is simply increasing the height of the #water_detailed div.
So basically, I think the problem will disappear once the structure is corrected.
Forgive me for not providing any code, but if you need anymore help, then I might code it and give you but I think it won't be needed.

PHP CSS Colour Shuffle array?

In a website i have a PHP snippet that on every refresh, certain elements change colour (div's links, text, hover states ect). This works fine but at the moment, on refresh they change to my themes colour by applying them to the the to css where necessary (examples below).
I now have a jQuery function for hover states which animates and all my link:hover states they are included in the colour shuffle. When the colour shuffle is applied though it's the default 'red','blue','green' if you were to put them in the css.
I have had a look online but i am unsure of where else to look as it is working just not on the jQuery snippet so i assume it's something to do with the jQuery function.
PHP at top of header:
<?php $colours = array('red', 'yellow', 'pink', 'blue', 'green');
shuffle($colours);
$random = $colours[0]; ?>
On the body tag it's echoed as an id:
<body id="<?php echo $random; ?>">
Link *<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>*
And on any div's or elements i want them to change colour before them in the css is #green,#red ect and the colour so if i wanted the 'share class' to change my css is like this:
#green .share {
color:#79b74c;
}
#red .share {
color:#900;
}
Now this all works apart from my hover shuffle states with the jQuery function:
$(document).ready(function(){
// colour rollover navigation
$(".share").hover(function() {
$(this).stop().animate({ 'color': "<?php echo $random; ?>" }, 300);
},function() {
$(this).stop().animate({ 'color': "#fff" }, 300);
});
It must be something to do with the jQuery as it works well when it's removed, and does work when applied but it's taking the colours from the php as css's default colour hex codes.
Thanks.
What about CSS3?
#green .share {
color:#79b74c;
}
#green .share:hover{
color:#79b74c;
}
#red .share {
color:#900;
}
#red .share:hover {
color:black;
}
.share:hover{
-webkit-transition: 0.3s;
-moz-transition: 0.3s;
-o-transition: 0.3s;
transition: 0.3s;
}

Arraylist not working in php

i have created a arraylist in php
It is place in a button . When i click on it opens the button, it opens all the aaraylist but there is some problem with the code the link to,the code is here.can someone tell me where is the problem.
var Arraylist<String> = new Arraylist<String>;
The problem is that any click will immediately hide the dropdown, after which you make it visible again by toggling it.
I'm trying to edit you fiddle to try and get it to work the way you want :).
Edit: here is a fiddle that does what I think you want it to do: fiddle
The idea I got from your fiddle is that you want to toggle the menu when you click it, and if you click anywhere else, the menu is hidden by default.
This works by remembering if the item was toggled or not, if not, it'll hide by default.
var nav = $('#nav');
nav.find('ul#nav').toggle();
var emp = $(this);
$("html").click(function() {});
$(document).bind('click', function(e) {
var dd = $("ul.dropdown");
var toggled = false;
var target = $(e.target); //2
if (target.parent().is('#nav')) {
var li = target.closest('li.menu');
toggled = true;
li.find('ul.dropdown').toggle();
}
if (!toggled) {
dd.hide();
}
});
OK, let's tackle all the problems with your code.
I'll explain what you're doing wrong and why, and what is recommended.
First up:
HTML Markup.
Your included example code contains markup errors. You're closing one of your list items inside the nested unordered list after the nested unordered list. Minor, but yet important for valid markup. Some browsers can go nuts over these things.
The second thing I noticed was that you're using the same ID for the lists. An ID is unique to the document in order to quickly reference it in CSS and Javascript. If you intend to select more than one element in the document, use classes, that's what they're there for. You can read more about it here or here.
Depending on what intended use you have for it; to achieve the same desired result, consider using this markup instead
<div class='emp_alt'>
<div class='container'>
<div class='title'>EMP</div>
<img src="http://www.iconarchive.com/download/i32215/tpdkdesign.net/refresh-cl/Symbols-Find.ico">
</div>
<div class='dropdown'>Contact no.</div>
</div>
Generally speaking, it requires less processing time for the client the less elements you have. When you apply CSS to the elements it is highly recommended to select them by class or ID, not by tag name. Using tag names can take up extra processing time because the client has to do more generalized searches. You can read more about efficient CSS here.
Here's a working example with your corrected markup.
Here's a working example using the alternate markup.
I'm not sure if you're trying to accomplish a sort of tooltip behavior, in which case, this example should suit you.
The JQuery (Javascript)
The other answers rightly pointed out your main problem was that you were hiding the dropdown on click. What they didn't address was that when the user clicks on something, and your document click picks up on it, it's going to return the element that is in the front.
So, when you click on the text, the #nav element is in the front. The image however, is an element in its own, and is in front of the #nav element.
This is why you should use the proper events instead because they include everything inside the element.
As you may have noticed in my examples above, there is a faster, cleaner and better way of achieving what you want.
You should be doing something like this instead
var dropdowns = $(".dropdown");
$(".nav > li").click(function(e){
//Prevent document click event from firing
e.stopPropagation();
var this_dropdown = $(this).children("ul.dropdown");
dropdowns.not(this_dropdown).hide();
this_dropdown.toggle();
});
//Hide all dropdowns if not clicked on a list item (or container)
$(document).click(function(){
dropdowns.hide();
});
Note: if you don't want to stop propagation you can use a "sensor" instead. Basically, a sensor determines a boolean's state which is then used to determine if the document click event should do anything or not. An if statement checking the boolean's state at document click should suffice.
var dropdowns = $(".dropdown");
var sensor_state = true;
$(".nav > li").click(function(e){
var this_dropdown = $(this).children("ul.dropdown");
dropdowns.not(this_dropdown).hide();
this_dropdown.toggle();
}).mouseenter(function(){
sensor_state = false;
}).mouseleave(function(){
sensor_state = true;
});
$(document).click(function(){
if(sensor_state){
dropdowns.hide();
}
});
This relies on the markup where the nav element has a dropdown child.
Here's an example using your example.
The problem here is that your markup makes this code toggle the dropdown when it itself is clicked. That's why I added a container to the earlier examples.
Hopefully, I've made at least some sense and cleared up your problem.
Update
After your latest comments, indicating you're using jqgrid and you want a dropdown inside, I decided to create a rather thorough example of how it can be done.
I suggest you study the code and learn from it. You don't have to do exactly as I did, as long as you know how and why.
Functionality:
When a user clicks a contact cell in the jqgrid it will show the dropdown container and the data provided from the "server" (custom array data is easier than staging an ajax event). The data provided by the server is in a hidden column (following the contact column)
If you click inside the dropdown container, it will not close. I added a close button inside it because users might otherwise get confused as to how to close it. It's better to have one than not to have one
If you click anywhere else in the document, it will close
If the user clicks the same cell while the dropdown container is visible, it will close
The dropdown container is not attached to the cell itself, but is instead positioned from the outside. This allows us to reuse the same container, instead of creating new containers for every single cell, saving us time in the process.
This is as far as writing your code I'm willing to go. The rest is up to you mate :)
I am reluctant to add more text to this post, but for completeness sake, I will add the entirety of the example code.
Code | JSFiddle Example
HTML
<link rel="stylesheet" type="text/css" media="screen" href="http://trirand.com/blog/jqgrid/themes/redmond/jquery-ui-custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="http://trirand.com/blog/jqgrid/themes/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" media="screen" href="http://trirand.com/blog/jqgrid/themes/ui.multiselect.css" />
<script src="http://trirand.com/blog/jqgrid/js/jquery.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/jquery-ui-custom.min.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/jquery.layout.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/ui.multiselect.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/jquery.jqGrid.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/jquery.tablednd.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/jquery.contextmenu.js" type="text/javascript"></script>
<table id="list"></table>
<div id="pager"></div>
<div id="contact_info"></div>
CSS
#contact_info{
position: absolute;
top: 0;
left: 0;
display:none;
background-color: white;
border: 1px solid gray;
padding: 5px 3px;
-moz-box-shadow: 3px 3px 4px #CCC;
-webkit-box-shadow: 3px 3px 4px #CCC;
box-shadow: 3px 3px 4px #CCC;
-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#CCCCCC')";
}
#list .contact{
cursor: pointer;
}
.container{
cursor: pointer;
}
td.contact:active{
background-color: #FBEC88;
}
.container .dropdown{
display: none;
}
.ui-jqgrid tr.jqgrow td{
white-space: normal;
}
/*
Style the dropdown box
*/
h2{
border-bottom: 1px solid lightgray;
}
p{
padding: 2px;
}
h2, p{
margin: 0;
}
.close_btn{
font-size: 10px;
line-height: 10px;
float: right;
text-decoration: none;
}
Javascript
//Preload the image to avoid flashes
search_img= new Image();
search_img.src = "http://www.iconarchive.com/download/i32215/tpdkdesign.net/refresh-cl/Symbols-Find.ico";
//The dropdown container
var contact_info = $("#contact_info");
//ID of last clicked row
var last_row;
//Sensor state determining whether document click will close the dropdown container on click
var sensor_state = true;
//Dropdown information data (base)
var dd_bp = "<img src='"+search_img.src+"'>",
dd_inf = "<h2>Contact information</h2><p>Phone: 555-12345<br/>E-mail: something#someplace.whatever<br/>P/O Box: 555555</p>"
//Data array (don't know what you use as a source, but I'll keep it simple)
//This setup allows you to send dropdown data from the server as well :)
var data_from_server = [
{id:"1",title:"Economy advisor",name:"Luke",lname:"North", contact: dd_bp, dropdown: dd_inf},
{id:"2",title:"Salesperson",name:"John",lname:"Smith",contact: dd_bp, dropdown: dd_inf},
{id:"3",title:"Economy advisor",name:"Jimmy",lname:"Hendrix",contact: dd_bp, dropdown: dd_inf},
{id:"6",title:"IT Manager",name:"Caroline",lname:"GlaDos",contact: dd_bp, dropdown: dd_inf},
{id:"5",title:"Quality Inspector",name:"Paul",lname:"Shoreman",contact: dd_bp, dropdown: dd_inf},
{id:"4",title:"Quality Inspector",name:"Liza",lname:"Ingridge",contact: dd_bp, dropdown: dd_inf},
{id:"8",title:"Distribution manager",name:"Elisabeth",lname:"Welman",contact: dd_bp, dropdown: dd_inf},
{id:"10",title:"Quality Inspector",name:"John",lname:"Johansson",contact: dd_bp, dropdown: dd_inf},
{id:"11",title:"Economy advisor",name:"Tommy",lname:"the Knuckle",contact: dd_bp, dropdown: dd_inf},
{id:"9",title:"Manufacturer",name:"Rosa",lname:"Minx",contact: dd_bp, dropdown: dd_inf}
];
//Adds the content to and repositions the dropdown container to the current row cell
function show_contact_info(rowid){
var row = $("#"+rowid),
contact_cell = $("td.contact", row),
dropdown_content = $("td.dropdown", row).html();
//Add the content
contact_info.html(dropdown_content).append("<a class='close_btn' href='#'>close</a>");
//Add a close button event
$(".close_btn").on("click", function(e){
e.preventDefault();
contact_info.hide();
});
//Position the contact info box
contact_info.css({
//The last calculations will center the container
left: contact_cell.offset().left - contact_info.outerWidth()/2 + contact_cell.outerWidth()/2,
//The last calculation will position the container below the cell, replace it with
// -contact_info.outerHeight() to position it above the cell
top: contact_cell.offset().top + contact_cell.outerHeight(),
});
contact_info.show();
}
function sensor_enter(){sensor_state = false;}
function sensor_leave(){sensor_state = true;}
function add_sensor(element){
element
.on("mouseenter", sensor_enter)
.on("mouseleave", sensor_leave);
}
//Setup jqgrid
$("#list").jqGrid({
datatype: "local",
width: 600,
colNames:['EID', 'Title','First name', 'Last name', 'Contact', "Dropdown"],
colModel:[
{name:'id',index:'id', width:20, sorttype:"int"},
{name:'title',index:'title', width:90},
{name:'name',index:'name', width:50, align:"left"},
{name:'lname',index:'lname', width:50, align:"left"},
{name:'contact',index:'contact', width:25, align:"center", classes:'contact'},
{name:'dropdown', index:'dropdown', hidden:true, classes:'dropdown'}
],
rowNum:10,
rowList:[5,10,15],
pager: '#pager',
viewrecords: true,
caption:"Employees",
onSelectRow: function(rowid, status, e){
var row = $("#"+rowid)
//"Hide" selection so that cell selection looks "cooler" :)
row.attr("class", "ui-widget-content jqgrow ui-row-ltr ui-state-hover");
},
onCellSelect: function(rowid, iCol, cellcontent, e){
if(iCol == 4){
if(last_row == rowid){
if(contact_info.is(":hidden")){
show_contact_info(rowid);
}else{
contact_info.hide();
}
}else{
show_contact_info(rowid);
}
last_row = rowid;
}
},
idPrefix: "emp_",
gridComplete: function(){
//Because the content is dynamic, we need to add it after the grid has finished
//This should be done for server generated content, ie the loadComplete event for server requests
add_sensor($("tr td.contact"));
}
});
$("#list").jqGrid('navGrid','#pager',{edit:false,add:false,del:false});
for(var i=0;i<=data_from_server.length;i++){
$("#list").jqGrid('addRowData',i+1,data_from_server[i]);
}
//Sensor for the dropdown container
add_sensor($("#contact_info"));
//Hide contact info on document click
$(document).click(function(){
if(sensor_state){
contact_info.hide();
}
});
Here is what it should look like
Here are all the references
Why must the ID attribute be unique on each page?
Using Classname (class) and ID in HTML
Writing efficient CSS
The difference between "return false;" and "e.preventDefault();"
return false vs e.stopPropagation();
.click
.children
.not
.on()
.offset()
.outerWidth()
.outerHeight()
jqGrid
jqGrid events
You just need to get rid of the additional hide() as you are hiding then toggling, overriding your toggle.
See this for instance with it working
http://jsfiddle.net/RxJer/11/
var nav = $('#nav');
nav.find('ul#nav').toggle();
var emp = $(this);
$("html").click(function() {});
$(document).bind('click', function(e) {
var target = $(e.target); //2
var dropdown = target.parents('#nav').find('li.menu ul.dropdown');
dropdown.toggle();
});​
Try this:
$(document).bind('click', function(e) {
var target = $(e.target); //2
if (target.parent().is('#nav') || target.parent().is('.menu')) {
var li = target.closest('li.menu');
var dd = li.find('ul.dropdown');
var isVis = dd.is(':visible');
$("ul.dropdown").hide();
(isVis) ? dd.hide() : dd.show();
} else {
$("ul.dropdown").hide();
}
});

PHP: Navigating through data

I have a PHP site I'm working on, and need to allow the user to select data and have different data appear on the next DIV (see attached image). Essentially I'd like a DIV (overflow:auto, so it scrolls) to populate using the SQL SELECT statement, and allow a user to click on a list item. That item creates a new SELECT statement for the div to the right, if that makes sense. Any input on the best way to go about this? New to PHP, not HTML/CSS though.
Zach
I think the best way to achieve this is with jQuery (a JavaScript-Library). I is quite easy to use, and if you got the trick, you can do amazing things with it.
For PHP/MySQL, you could use jQuerys Ajax functionalities (see http://api.jquery.com/jQuery.ajax/). Use the callback to display the loaded data (see below).
Here is a very simple example on how to show another div (in which could be more links to select) with dynamic content. If you combine this with Ajax, you should get what you need.
Include jQuery in within head tag:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
Code for the body:
<!-- First Box: click on link shows up second box -->
<div id="selectOne" style="float: left; margin-right: 10px; border: #666 thin solid; padding: 10px;">
One<br />
Two<br />
Three
</div>
<!-- Second Box: initially hidden with CSS "display: none;" -->
<div id="selectTwo" style="float: left; margin-right: 10px; display: none; border: #666 thin solid; padding: 10px;"></div>
<!-- The JavaScript (jQuery) -->
<script type="text/javascript">
//Do something when the DOM is ready:
$(document).ready(function() {
//When a link in div with id "selectOne" is clicked, do something:
$('#selectOne a').click(function() {
//Fade in second box:
$('#selectTwo').fadeIn(500);
//Get id from clicked link:
var id = $(this).attr('id');
//Depending on the id of the link, do something:
if (id == 'one') {
//Insert html into the second box which was faded in before:
$('#selectTwo').html('One<br />is<br />selected')
} else if (id == 'two') {
$('#selectTwo').html('Two<br />is<br />selected')
} else if (id == 'three') {
$('#selectTwo').html('Three<br />is<br />selected')
}
});
});
</script>
So if you would use jQuerys Ajax-Functionality, you could use something like that (not tested!):
$('#selectOne a').click(function() {
var id = $(this).attr('id');
$.ajax({
type: 'POST',
url: 'getYourData.php',
data: 'thisIsSentToPHPFile='+id,
success: function(msg){
//everything echoed in your PHP-File will be in the 'msg' variable:
$('#selectTwo').html(msg)
$('#selectTwo').fadeIn(500);
}
});
});
The getYourData.php could be:
$id = $_POST['id'];
$query = mysql_query('SELECT * FROM table WHERE id='.$id);
$result = mysql_fetch_assoc($query);
//Now echo the results - they will be in the callback variable:
echo $result['tablefield1'].', '.$result['tablefield2'];
Give it some tries, tweak it a little bit and you should get it working.
The most straightforward way to do this is probably to make the right portion an iframe.
Then each item in the middle list could be a link like so:
Company 2
In displaySubItems.php you would then use the $_GET['company'] value in your select statement to populate the table.
Alternatively you could make use of AJAX methods to populate the table on the right though I'm personally not that familiar with AJAX so I can't tell you more about that. Also I suspect iframes are more widely supported.
EDIT: changed some parts based on one of your comments

Categories