I have a toggle button next to each recipes on my website.
I have images in my database that I'd like to display. Each toggle button would display a different image.
How can i retrieve the image path dynamically ?
Should I had some php to my CSS or should I add something else to the Jquery code?
<div class="toggle" id="image<?php echo $recipe_id ?>"></div>
jQuery:
<script>
$(document).ready(function(){
var $content = $(".content").hide();
$(".toggle").on("click", function(e){
$(this).toggleClass("expanded");
$(this).siblings('.content').toggle();
});
});
</script>
CSS:
.toggle {
background:url(/images/muffin.png) }
Right now, all the toggle buttons display the same dummy image (muffin.png).
Well there are a few ways to do this. You could echo out the path to the image while the page is being parsed in PHP, or you could use Ajax to connect to PHP, figure and the path and spit it back o JavaScript, where you could append the image tag to the DOM.
I would recommend using the later method, as it is more correct in my opinion. I see you are echoing $image_id. Try to do
<img href="<?php echo $image_path; ?>">
and let me know if that works. If not, can you specify the table name and column name where the image path is stored? Also are you using any PHP frameworks?
To list the recipes from your database you probably have a query like this somewhere:
SELECT * FROM recipes
And after that your PHP script probably loops through the results of this query.
If this is the case, just make sure the query selects the path to the image, then do something like this:
<div class="toggle" id="image<?php echo $recipe_id ?>" style="background:url(<?php echo $query_result["image_path"] ?>)"></div>
If this is not the case, you can execute a query to get the image paths for each recipe individually:
SELECT image_path FROM recipes WHERE recipe_id=$recipe_id
Then use the result from this query.
Related
I am stuck in a problem and in need of support from you guys.
My problem is I want to pass a php variable(dynamically called through database in loop) to another div on the same page without page refresh
//This is a loop
<td><a class="linkright" href="#existingcase?case_id=<?php echo $row_mycases['case_id']; ?>"><?php echo $row_mycases['case_id']; ?></a></td>
//The div which should get the above php variable
<div class="tabright" id="existingcase">
<?php
$c_id = $_GET['case_id'];
echo $c_id;
?>
</div>
//the javascript code for calling divs on the same page
<script>
$(".linkright").click(function(){
$(".tabright").hide();
theDiv = $(this).attr("href");
$(theDiv).slideToggle();
});
</script>
It shows in the url like this index.php#existingcase?case_id=2012001 but never passes the case id to #existingcase. And also #existingcase div does not load either but without passing caseid value, #existingcase loads.
Any suggestions would be great.
I think you want to print clicked case_id in div without page load. To do this, you don't want to pass it using url. you can simply achieve it using javascript.
If your div id existingcase is just change your code like this below.
you are printing that same case_id as a text of anchor tag using $row_mycases['case_id'];. So, you can easily get that text using javascript and you can easily print it in your #existingcasediv.
<td><a class="linkright" href="#existingcase"><?php echo $row_mycases['case_id']; ?></a></td>
I don't know about your other scripts. in .linkright click function place this code like this
$(".linkright").click(function(){
$('#existingcase').text($(this).text()); //if your div id is existingcase it will print your case id.
});
try this code. And let me know the result.
SEE THIS FIDDLE DEMO
UPDATED:
To pass client side value to serverside without page reload, you can use jquery.post() method.
Place this PHP code at the top of your page.
<?php
if (isset($_POST['id'])) {
$caseid = $_POST['id'];
return print_r($caseid);
}
?>
$caseid will contain currently clicked case_id value. So, you can use this $caseid wherever you want in this page. If you click on the another case id, it will update with respect to currently clicked case_id
replace your js with below code,
$(".linkright").click(function () {
$(".tabright").hide();
theDiv = $(this).attr("href");
$(theDiv).slideToggle();
$.post("yourPHPFile.php", { //use your current php file name instead of "yourPHPFile.php"
id: $(this).text()
}, function (caseid) {
$('#existingcase').text(caseid);
});
});
id : $(this).text() means, get the current element .text() and assign it to $_POST variable name of id. It will post to yourPHPFile.php. And you can retrieve that value like $caseid = $_POST['id'];.
In function (caseid) {, caseid contains the value of $caseid. So, only in this code, I assigned $caseid = $_POST['id'];
By this you can directly print clicked case_id text to your #exixtingcase div.
Any values after # are not sent to the server. Since PHP is a server technology you php code will never see the values after #
<a class="linkright" href="#existingcase?case_id=<?php echo $row_mycases['case_id']; ?>">
try doing
<a class="linkright" href="existingcase?case_id=<?php echo $row_mycases['case_id']; ?>">
instead and your php code should fill the div.
In my understanding of your explanation I think you are making a mistake. You are trying to mix Javascript and PHP with out page refresh. This is not possible through your code as PHP is server side and only works on the server. You can't do href within the same page and expect PHP code inside this div to be executed. The PHP code will not be there. You can try "view source" in your browser and check it out.
I think the only way to solve your problem is to make AJAX call using Javascript, get the response and put wherever you want (without refreshing the page)
Hope this helps and excuse my English
EDIT : Here is a working example for making a simple AJAX call using Jquery.
Your table should be some thing like this:
<td>
<a class="linkright" href="#" data-id="<?php echo $row_mycases['case_id']; ?>"><?php echo $row_mycases['case_id']; ?></a>
</td>
You will notice I added "data-id" attribute. We will use this id in our AJAX call. Put this in your JS :
// Create Jquery ajax request
$("a.linkright").click( function() {
$.get("example.php", // Put your php page here
{ case_id : $(this).attr("data-id") }, // Get data, taken from the a tag
function(data) { // Call back function displays the response
$(".tabright").html("<p>" + data + "</p>");
}); // Get
}); // click event
}); // Document Ready
I tested the code and it is working. You will need to customize it to work for you. If all of this AJAX is new to you I suggest these links for you :
http://www.w3schools.com/ajax/default.ASP
http://learn.jquery.com/ajax/
I have dynamic gallery generated from a php code, wich are represented by one picture only. When you select that picture, it should open a color box with multiples photos.
The problem is, my colorbox seem to not recognize my rel.
<script>
$(document).ready(function(){
//Examples of how to assign the ColorBox event to elements
$("a.gallery").click(function(){
var galleryRelative = $(this).attr("rel");
alert(galleryRelative);
$(this).colorbox({rel:galleryRelative});
});
});
</script>
And my HTML
foreach($kittens as $key => $kitten){
echo '<div class="block1 smallblock">';
echo '<a class="gallery" rel="gallery0" title="blablabla" href="../img/block-03.jpg">
<img src="../img/block-03.jpg" width="299" height="233" alt="kitten" />
</a>';
echo '<p></p>';
echo '<p></p>';
echo '</div>';
}
I will generate the gallery0 without problems (im reading with success the rel attribute from my a.gallery in my script, but colorbox won't match it.
Colorbox will use your rel attribute by default. Just use:
$("a.gallery").colorbox();
By setting your own click event you are reassigning colorbox to your elements each time an item is clicked, which would be inefficient.
You can better try with the Pretty Photo plugin for displaying the multiple photos in the light box by clicking the single image. I used the Pikachoose gallery when i click the image from PikaChoose I show the list of images inside the light box using the Pretty Photo Plugin demo for Pretty. Hope this may be useful for you
I have many tags. I want click each tag, then post/get the tag's value to another page.
In another page, received the values and make a mysql query. Then return the resalt data to the first page(do not make an iframe).
I think jquery post and load may be can do that (but have no idea how to combine two functiton). Or maybe there have any other way. Thanks.
here is my code
products.php
model:hml03
model:hml04
model:hml05<!--post value from products.php-->
data.php
<div id="data">
<?php
$result = mysql_query("SELECT id,name,details,add_date,model FROM ctw_products WHERE (MATCH (name,details,model) AGAINST ('+$_GET['get']' IN BOOLEAN MODE) Order By add_date DESC LIMIT 20 "); // get value with a mysql query
while ($row = mysql_fetch_array($result))
{
echo '<div class="name">'.$row['name'].'</div>';
echo '<div class="model">'.$row['model'].'</div>';
echo '<div class="details">'.$row['details'].'</div>';// how to return this part of datas back to products.php
}
?>
</div>
...<!--many other tags -->
<div class="show_data"></div><!-- get the data back show in here without refresh the page.
First you need AJAX to be able to do this. The simplest way to achieve this is use a library like jQuery
You can either download the library and include it locally or link to it directly from the jQuery site. the following code should allow you fetch data from data.php without a page refresh
<!--include the jQuery library -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.js"></script>
<script type="text/javascript">
function get_data(get){
$.get('data.php?get='+get, function(data) {
$('.show_data').html(data);
});
}
</script>
Next, modify your links to call get_data onclick like this:
model:hml03
Notice how we pass the id of the product to get_data, you need to do this for every link.
I would recommend you read up on Ajax and JQuery to really get the idea.
Lets say a member is displaying 10 images by default but a link will display the rest of the users images by having them slide down when a user clicks a link.
So my question basically is I want to be able to display all the users images buy having them slide down when a user clicks on a link <a>. How would I be able to tackle this problem using JQuery, PHP & MySQL?
check this plugin, it's not what you asked, but it's (IMO) a better solution
http://www.appelsiini.net/projects/lazyload/enabled_fadein.html
BTW : PHP and MySQL are arbitrary in that question since it depends how your images are stored on the server
This will take care of it.
<script>
$(document).ready(function() {
$(".more").toggle(
function() {
$(this).text('less');
$(this).parent().children(".bottom_images").slideDown("fast");
return false;
},
function() {
$(this).text('more');
$(this).parent().children(".bottom_images").slideUp("fast");
return false;
});
});
</script>
<style>
.bottom_images {
display: none;
}
</style>
<div class="container">
<div class="top_images">
<?php
//num images to originally display
$num_show=10;
//current position
$i=1;
//some code to get user images
//from database etc
foreach($images as $image_url) {
if($i==10) {
echo '</div><div class="bottom_images">';
}
echo '<img src="'.$image_url.'" />';
$i++;
}
?>
</div>
more
</div>
Obviously there is some pseudo code for retrieving the user images as there are a number of ways you could get the images eg. from database (as a blob or text url), scanning the file system, user input etc etc. Also I have made it so you can add multiple containers (multiple users) into the one page.
It depends on various aspects how do you want to do it.
You can use LazyLoading as provided by Yanick Rochon
You can preload all the images and simply show the hidden ones when
clicked.
You can also load additional images using AJAX
You can load the visible images first and after all they're loaded - preload invisible images so to show them instantly after clicking a link
Describe your problem a little bit deeper.
I have the following jQuery code in my PHP file (edited Jan 19 2010 # 10:40 MST):
<?php
$count = 0;
foreach($attachments as $attachment) :
echo '<script type="text/javascript">
$(\'#a_'.$count.'\').click(function() {
$(\'#d_'.$count.'\').show(200);
});
// if "no" is clicked
$(\'#d_'.$count.' .no\').click(function() {
$(\'#d_'.$count.'\').hide(200);
});
// if "yes" is clicked
$(\'#d_'.$count.' .yes\').click(function() {
$(\'#d_'.$count.'\').hide(200);
// update database table -- this is why I need the script inside the for loop!
var jsonURL = \'http://path/to/update_db_script.php\';
$.getJSON(jsonURL, {\'post_id\' : '.$attachment->ID.'}, function(data) {
alert(\'Thank you. Your approval was received.\');
});
$(\'#a_'.$count.'\').replaceWith(\'<span>Approved</span>\');
});
</script>';
echo '<li>';
if($attachment->post_excerpt == 'approved') {
// Check the proof's status to see if it reads "approved"
echo '<span>Approved</span>';
} else { ?>
// If not yet approved, show options
<a class="approve" id="a_<?php echo $count; ?>" href="#">Click to Approve</a>
<div class="confirm-approval" id="d_<?php echo $count; ?>">
<p>Please confirm that you would like to approve this proof:</p>
<a class="yes" href="#">Yes, I approve</a>
<a class="no" href="#">No, not yet</a>
</div><?php
} ?>
</li>
<?php $count++;
endforeach; ?>
The page in question is available here. The "click to approve" links do not work (that's my problem).
When I view source, the PHP variables appear to have echoed properly inside the jQuery:
<script type="text/javascript">
$('#a_0').click(function() {
$('#d_0').show(200);
});
... etc ...
</script>
This looks correct, but nothing happens when I click any of the links. However, when I replace the PHP echo statements with plain numbers (0, 1, etc.) the click functions work as expected.
You may be asking: why on earth do you have this inside a for loop? The reason is that I need to retrieve the attachment->ID variable and pass it to an external PHP script. When someone clicks "approve" and confirms, the external script takes the attachment->ID and updates a database value to read "approved".
Why won't the click function fire when PHP is in place? Is there some kind of greater force at work here (e.g., hosting limitation), or am I missing a fundamental piece of how PHP and JavaScript interact?
Since you didn't post your HTML its a little hard to troubleshoot.
First, I am not sure why one is working and the other is not since the code it is outputting looks correct. Either way, I still would make some changes. Move your a_0,a_1, etc and d_0,d_1, etc into the id attribute instead of a class:
<div>Click Me</div>
<div class="confirm_approval" id="d_0">Show Me</div>
<div>Click Me</div>
<div class="confirm_approval" id="d_1">Show Me</div>
Now, instead of outputting your code in a loop in PHP, place this jQuery code once on your page:
$(document).ready(function(){
$("a.approve[id^='a_']").click(function(e){
var id = this.id.replace('a_',''); // Get the id for this link
$('#d_' + id + '.confirm-approval').show(200);
e.preventDefault();
});
});
This code finds any a element with the approve class that has an id that starts with a_. When this is clicked, it grabs the number off the id a_0 = 0 and uses that id to find the confirm-approval element and show it.
Since the javascript is run on the client and has no way of knowing whether the script was generated using PHP or not, I think that particular part is a wild goose chase...
When I replace the PHP echo statements
with plain numbers (0, 1, etc.) the
click function works as expected.
Do this again and compare the actual output using view-source in a browser. I'll bet you find that there is a difference between the working and failing scripts, other than one of them being generated by PHP.
It seems that the problem is in jQuery selectors. Instead of dynamically binding click() events on multiple objects with an output of PHP code, use just one class selector and bind to objects with this class. And you can specify an id attribute to make them unique.
Something strange too is to have the script tag and the
$(document).ready(function()
in the loop. I don't know if this causes any problems, but it's sure not very efficient, one time is enough.