jQuery rating system - php

I'm trying to make a rating system from 0, so far I have two classes on and off, there are total of 5 stars,
It would show the rating of the item, until the person hovers over them, then it would hilight the previous stars and hide the current rating,
<div id="stars">
<div class="on"></div>
<div class="on"></div>
<div class="on"></div>
<div class="off"></div>
<div class="off"></div>
</div>
the JS I got so far
$('#stars').mouseover(function() {
$(this).children().prevAll().andSelf().attr('class', 'on');
$(this).nextAll().attr('class', 'on');
});
Which only hilights all the starts and messes up everything, anyone could help out? Working too many hours and just can't get it to work..
Thanks!

If you're using this as a preview before a click, then you may want to use the hover function like this:
$('#stars .star').hover(
function() {
$(this).prevAll().andSelf().addClass('on');
$(this).nextAll().removeClass('on');
},
function() {
$(this).parent().children().removeClass('on');
}
);
That goes with the HTML in this jsFiddle where you can see it work: http://jsfiddle.net/jfriend00/aRTzQ/
I'd also suggest using addClass() and removeClass() rather than manipulating the attribute directly as it gives you more flexibility to have other classes on the object and it's more readable.
To make a click sticky so it then remembers that setting, you could use something like this: http://jsfiddle.net/jfriend00/QxADg/. The corresponding CSS/HTML is in the fiddle.
$('#stars .star').hover(
function() {
$(this).prevAll().andSelf().addClass('preview');
$(this).nextAll().removeClass('preview');
},
function() {
$(this).parent().children().removeClass('preview');
}
);
$('#stars .star').click(function() {
$(this).parent().children().removeClass('on');
$(this).prevAll().andSelf().addClass('on');
});
$('#stars').hover(
function() {$(this).addClass("inPreview");},
function() {$(this).removeClass("inPreview");}
);

There's a lot of plugins out there, such as the one PPrice links to. I agree with others that that would be the better way to go unless they don't meet your needs for some reason.
If you want to make this code work or if you're just doing this for the learning, maybe this is what you're trying to do?
You can hover over the "stars" (boxes) to see the colour change.
http://jsfiddle.net/pW3Sn/
$('#stars > div').mouseover(function() {
$(this).prevAll().andSelf().attr('class', 'on');
$(this).nextAll().attr('class', 'off');
});

Actually you can implement it using pure CSS.
HTML code:
<div id="stars">
<div class="star">
<div class="star">
<div class="star">
<div class="star">
<div class="star"></div>
</div>
</div>
</div>
</div>
</div>
And CSS:
#stars {
height: 16px; /* height of stars block */
}
#stars .star {
height: 16px; /* height of one star */
padding-left: 16px; /* width of one star */
background-image: url('http://url.to.your.star.image');
float: left;
}
#stars .star:hover,
#stars .star :hover {
background-position: 0px -16px; /* toggle image */
}
A working example: http://jsfiddle.net/4CfzD/

Why not just use someone's plugin? e.g. http://www.fyneworks.com/jquery/star-rating/

Related

A option of night mode in an app which automatically reduces the screen brightness. Will it workout using some php or html or css code?

This code is basic one which changes the background to black one.
PHP CODE
if ($res160[0] == "1") {
echo "<script>$('body').css('background-color', 'black'); $('body').css('color', 'white');</script>";
}
How do I write a code to reduce the brightness?
There is a CSS property filter which is used to reduce the brightness.
filter: brightness(50%);
Hope this is what you are looking for.
I would do this by changing the brightness of the container(body or div) something like this.
var x = $("#myRange");
$("body").css({
"opacity": x.val()
});
x.on("change mousemove", function() {
$("#contents").css({
"filter": "brightness(" + $(this).val() * 100 + "%)"
});
});
div {
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Choose Light Level<br>
<input id='myRange' type="range" min="0.3" max="1" step="0.1" value=1><br> dark -> light
<div id="contents">
<div style="">
<img src="http://lorempixel.com/100/100/nightlife/Dummy-Text">
<img src="http://lorempixel.com/100/100/food/Dummy-Text">
</div>
<div style="background-color: red;">
<img src="http://lorempixel.com/100/100/nightlife/Dummy-Text">
<img src="http://lorempixel.com/100/100/food/Dummy-Text">
</div>
</div>

How to keep my text on the same line with AJAX call?

I'm trying to keep my ajax call on the same line, as detailed here: http://external.sidewaykill.com/versound/motd.php.
It won't, so what can I do?
$(document).ready(function() {
$.ajaxSetup ({
cache: false
});
var ajax_load = "<img src='http://static.sidewaykill.com/img/ajax-loader2.gif' alt='loading...' />";
// load() functions
var loadUrl = "playercount.php?id=2";
$("#servercount").html(ajax_load).load(loadUrl);
});
This has nothing to do with PHP but everything to do with the absolutely awful markup of your HTML and the lack of CSS. You're going to want to wrap all of this "free-text" inside of a div or two, and then make sure you're putting the wrapper div inside of the infobar div
Sample CSS:
#infobar p{
float: left;
margin-left: 5px;
}
HTML:
<div id="infobar">
<div style="float: left;">
<p>Welcome! Our server count is:</p>
<p>1 / 10</p>
<p>We hope you enjoy your stay! We are currently playing on gm_construct.</p>
</div>
</div>
its because the ajax call returning a div element,
as this :
<div id="count"> 0 / 10</div>
Since div is a block element it will always render in a new line. there are two solutions for this
one would be to return a <span> element instead of the div
second will be if u are unable to change the return value you can fix it via css
#servercount #count { display : inline }
My suggestion is to go with the first solutions

How to create drop down menus on dynamically generated div fields

I am loading divs with information from the database using php code:
for ($i=0; $i < $stmt->rowCount(); $i++){
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$name = $row['uc_name'];
$image_url = $row['uc_image'];
$color = $row['uc_color'];
The creates a box for however many rows there are: http://jsfiddle.net/pKR5t/1/
I am trying to create a drop down menu for each item retrieved from the database. When the user hovers over class="header", the drop down menu should show but the list inside should be pertaining to that of the box hovered:
I believe I can use the .on() function to accomplish this but I am completely lost on how to use it. Any help on getting this accomplished would be helpful. Thanks
To use on and hover you will want to use the mouseenter and mouseleave events like so:
$("div.box div.header").on("mouseenter", function() { //mouseenter event
var currentBox = $(this).closest("div.box");
var currentTitleText = $(this).find("div.title").html();
//popup the list
}).on("mouseleave", function() { //mouseleave event
//close the list
});
For some additional info, check this post.
EDIT
Updated mouseover and mouseout to the slightly better mouseenter and mouseleave events.
You can do this without javascript using css.
If you insert your hover box inside your header div you can use css :hover to hide and show it on hover.
HTML
<div class="header">
<div class="title">PHP Row 1</div>
<div class="hoverBox">
<div class="hoverBoxTitle">Hover</div>
<div class="hoverBoxContent">Hover Content</div>
</div>
</div>
CSS
.hoverBox {
display: none;
position: absolute;
top: 0px;
left: 170px;
width:170px;
height:90px;
background:#333333;
z-index: 100;
}
.header:hover .hoverBox {
display: block;
}
Demo

PHP: Fill block of an image with color and save the information

I'm looking to setup a page which holds an image of grid paper. These grids can be selected and depending on what type of link the user chooses the color will be filled and saved. Once all of the blocks are filled this will be archived and then a new sheet will be displayed. I wanted to use PHP / MySQL on the backend but was wondering what would be good client side?
Why a image for the grid? Better solution is to use a table or a list filled with divs to create the grid. Then use jQuery to fill in the backgrounds when the user clicks a link.
Example for the grid:
<ul id="grid">
<li>
<div class="cell" id="cell_id"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</li>
<li>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</li>
...
</ul>
CSS example:
<style>
#grid {
list-style: none;
}
#grid li {
height: 50px;
}
#grid .cell {
float: left;
width: 100px;
height: 50px;
border: 1px solid black;
}
</style>
And finally use jQuery to fill the cells when a user clicks a link. You can find the cell by calculating the rows and cells or just give the cells a id. The saving part can be done by a AJAX call to a page that will save the information to the DB.
Simple jQuery example (you have to think up the rest yourself):
<script>
$('link').click(function() {
//place check for new sheet here
var id = $(this).attr('id');
//set the bg color
$('cell_id').css('background-color', 'red'); //or color code #FF0000
//save the info
$.post('save_info.php', { cell_id: id, color: "red" }, function(data) {
alert('saved!');
});
});
</script>
You can find more info and download jQuery at: http://jquery.com/
The jQuery code to create a new sheet when all the cells are filled can be done with a little check in the click function. This check must count all the cells that have a bg color, is this the same as the total cells? Then create a new sheet by removing all the cells their bg color and by setting new id's. But you can figure this out by yourself ;).

Rotating images and text

I want to have an image with it's corresponding block of text rotating every few seconds on my website. Just like these guys http://hellofisher.com/
I know I can get a javascript to rotate the images but I haven't found where I can have the block of text alongside rotating to suit the images.
Just google for jquery content rotators or content sliders. There's a million ready-made plugins already done like this:
For example
I'll show you a simple example which you can easily use on your site. This example uses 3 slides and isn't a dynamic slider where you can have as many slides as you want to but it's easy to edit it into one if the amount of slides can vary.
HTML:
<div id="slider">
<div id="slider-area">
<div class="slide">content</div>
<div class="slide">content</div>
<div class="slide">content</div>
</div>
</div>
Slider is the visible area which the user can see, slider-area is the area where you put the slides and each slide contains a different image and text.
CSS:
#slider {
width: 400px;
height: 400px;
overflow: hidden;
position: relative;
}
#slider-area {
width: 1200px;
height: 400px;
position: absolute;
top: 0;
left: 0;
}
.slide {
width: 400px;
float: left;
}
As you can see, slider-area's width is 3 times the size of slider because there are 3 slides. Slider has overflow: hidden which means scrollbar won't be visible and position: relative so that we can absolutely position the slider-area inside the slider. Then we simply insert slides inside the slider-area.
JS (using JQuery):
$(document).ready(function() {
var timer = setInterval(changeSlide, 5000);
});
function changeSlide() {
var pos = $('#slider').attr('scrollLeft');
if (pos==800)
pos=0
else
pos=pos+400;
$('#slider').animate({scrollLeft: pos}, 600, 'swing');
}
Here you set a timer which will call the changeSlide function every 5 seconds. The changeSlide function will take the current position of the slider's scrollbar and animate it accordingly. If it's displaying the last slide, next one will be the first slide (pos=0) and if it's not displaying the last slide, it adds 400 to the position so that it will scroll to the next slide. Basically the sliding animation is simply moving a hidden scrollbar with a timer.
Hopefully that helps!
Just use jQuery to rotate out a <div> containing an image and associated text. Use $.hide() and $.show() and Javascript's time functions.
You could try:
JAVASCRIPT
var imagelist = ["IMAGE1","IMAGE2","IMAGE3"]
var textlist = ["TEXT1","TEXT2","TEXT3"];
counter=0;
countmax=3;
var next = function(){
$('rotating_image').innerHTML="<img src=\"" imagelist[counter%countmax] + "\">";
$('rotating_text').innerHTML=textlist[counter%countmax];
countmax++;
}
var rotate = window.setInterval(next, SECONDS * 1000);
HTML
<span id="rotating_image"></span>
<span id="rotating_text"></span>

Categories