Jump to content

someway to recreate what onclick does in php


dfez44

Recommended Posts

So what I am trying to do is have clicking on a thumbnail do two things at once. Make the larger image go into the iframe on my page and change the caption text to the appropriate caption. Is there anyway to do this is php, I realize onclick is not compatible.

 

<?php

$results= mysql_query ("SELECT * FROM `gallery` WHERE `remove`=0 AND `category`=1 ORDER BY `order` DESC");

while($result= mysql_fetch_array($results)){
echo "
<a href='assets/".$result["img"]."' target='main'><img src='assets/thumbs/".$result["img"]."' alt='headshot1' onclick='MM_setTextOfLayer(\'caption'\,'','".$result["caption"]."')' class='th' /></a>				
";

}
?>

 

[attachment deleted by admin]

Link to comment
Share on other sites

If the image and caption are both within the iframe (shudder) then you just need to create the thumb as a link such that it uses the iframe as a target and pass it a URL such that the php page called can get the image. Example:

<A href="getImage.php?id=12"><image src="image.jpg" /></a>

 

Then create the PHP page to use that value on the query string to query for the image and caption.

Link to comment
Share on other sites

Ok so doing it a new page I am having trouble getting the id to transfer. Any suggestions? Here is the url http://rachelhippert.com/test/gallery.php.

 

The code from my gallery page:

 <?php

$results= mysql_query ("SELECT * FROM `gallery` WHERE `remove`=0 AND `category`=1 ORDER BY `order` DESC");

while($result= mysql_fetch_array($results)){
echo "
<a href= 'postImg.php?id=".$result["id"]."' target='main'><img src='assets/thumbs/".$result["img"]."' class='th' /></a>				
";

}
?>

 

And here is the 'postImg.php' code:

 

<?php
require("connect.php");
$id= $_GET["id"];

$results= mysql_query ("SELECT * FROM `gallery` WHERE `remove`=0 AND `category`=1 AND WHERE `id`=$id");

echo "<div class= 'img' align= 'center'> <img src='../assets/".$result["img"]."'><br/><p>".$result["caption"]."</p></div>";

?>

Link to comment
Share on other sites

You've posted code, but neither stated what you expect that code to do or what it is currently doing wrong. Are you getting errors, a blank page, what? Did you even check the HTML source code to verify that the links were being created with the right values?

 

The only thing I see wrong is that you have two WHERE statements in your query. You would have caught that if you added error handling to your code.. Here is a rewrite of your code above with some debugging and error handling added.

$query  = "SELECT `id`, `img` FROM `gallery` WHERE `remove`=0 AND `category`=1 ORDER BY `order` DESC";
$result = mysql_query($query);

if(!$result)
{
    echo "Error running query: {$query}<br>\n";
    echo "Error: " . mysql_error();
}
elseif(mysql_num_rows($result)==0)
{
    echo "No images found.<br>\n";
}
else
{
    while($row = mysql_fetch_assoc($results))
    {
        ##DEBUG ONLY LINE
        echo " [id={$result['id']}]:";
        echo "<a href= 'postImg.php?id={$row['id']}' target='main'><img src='assets/thumbs/{$row['img']}' class='th' /></a>\n";
    }

 

require("connect.php");

$id = $_GET["id"];
##DEBUG ONLYLINE
echo "GET['id'] = {$id}<br>";

$query  = "SELECT `img`, `caption` FROM `gallery` WHERE `id`={$id}";
$result = mysql_query ($query);

if(!$result)
{
    echo "Error running query: {$query}<br>\n";
    echo "Error: " . mysql_error();
}
elseif(mysql_num_rows($result)==0)
{
    echo "No image found.<br>\n";
}
else
{
    $row = mysql_fetch_assoc($result);
    echo "<div class= 'img' align= 'center'> <img src='../assets/{$row['img']}'><br/><p>{$row['caption']}</p></div>\n";
}

 

Link to comment
Share on other sites

I forgot to add, the you should use mysql_real_escape_string() on the id value coming from the URL. Otherwise you are open to SQL Injection - potentially compromising your entire database.

 

Use this:

$id = mysql_real_escape_string(trim($_GET['id']));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.