Issue
I use echo to display and add class names of all images in two different directories (one containing hoodies, and one containing teeshirts). I want to have radio buttons, that when clicked, only show that particular class (eg if you click teeshirts, it only displays teeshirts.
<input id="all" type="radio" name="category"> <label for="all">All</label> <br/>
<input id="hoodies" type="radio" name="category"/> <label for="hoodies">Hoodies</label> <br/>
<input id="teeshirts" type="radio" name="category"> <label for="teeshirts">Teeshirts</label> <br/>
<!-- Images -->
<?php
// Teeshirts
$dir = 'images/clothing/hoodies/small/';
$files = scandir($dir);
$images = array();
foreach($files as $file) {
if(fnmatch('*.png',$file)) {
$images[] = $file;
}
if(fnmatch('*.jpg',$file)) {
$images[] = $file;
}
}
foreach($images as $image) {
echo '<img src="images/clothing/hoodies/small/'.$image.'"width="15%" height="20%" hspace="2%" vspace="2%" class="hoodie" data-big="images/clothing/hoodies/large/'.$image.'" />';
// echo '<p>'.$image.'</p>';
}
// Hoodies
$dir = 'images/clothing/teeshirts/small/';
$files = scandir($dir);
$images = array();
foreach($files as $file) {
if(fnmatch('*.png',$file)) {
$images[] = $file;
}
if(fnmatch('*.jpg',$file)) {
$images[] = $file;
}
}
foreach($images as $image) {
echo '<img src="images/clothing/teeshirts/small/'.$image.'" width="15%" height="20%" hspace="2%" vspace="2%" class="teeshirt" data-big="images/clothing/teeshirts/large/'.$image.'" />';
}
?>
This is my css currently
#teeshirts:checked ~ .hoodie {
display: none;
}
#hoodies:checked ~ .teeshirt {
display: none;
}
The way it works right now though is by saying, if you click teeshirt, dont display hoodies (to only display teeshirts). But right now this does not work at all (it use to before when I was hardcoding every image but not now that I am using php). How can I get this to work? And how can I write this better (so it just displays that one class, as opposed to not displaying the other).
I'm also lost as to how to get the all button working.
Solution
this can be a quick solution with raw data, you can use PHP with it easily
HTML Code
<div>
<label><input type="radio" name="colorRadio" value="red"> red</label>
<label><input type="radio" name="colorRadio" value="green"> green</label>
<label><input type="radio" name="colorRadio" value="blue"> blue</label>
</div>
<div class="red box">You have selected <strong>red radio button</strong> so i am here</div>
<div class="green box">You have selected <strong>green radio button</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue radio button</strong> so i am here</div>
CSS Code
<style type="text/css">
.box{
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red{ background: #ff0000; }
.green{ background: #00ff00; }
.blue{ background: #0000ff; }
</style>
Try this script as well
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[type="radio"]').click(function(){
if($(this).attr("value")=="red"){
$(".box").not(".red").hide();
$(".red").show();
}
if($(this).attr("value")=="green"){
$(".box").not(".green").hide();
$(".green").show();
}
if($(this).attr("value")=="blue"){
$(".box").not(".blue").hide();
$(".blue").show();
}
});
});
</script>
Answered By - Kashif Latif
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.