To all experts,

I am trying create a page that allows users to view and reuse images returned from mysql.

In the mysql database I have two columns "image_path" (which stores the path and name of an image stored in the file system) and "image_name" (a text name that the user can recognize).

The idea is to retrieve all image_path and image_name, and display the image_name (with a href), so when the user clicks on the image_name in the display, the actual image will display in another frame (THIS IS WORKING OK).

My problem is: when the image_name is clicked I also want use the image_path as a value in a form text field, to post to another page.

My code is below, but the line <input type="text" name="pick2field" id="pick2field" size="30" value="<?php echo $ipath ?>"/> is generating just one instance of the image_path, from the loop, and is not changed dynamically when the user selects a different image_name.

I have tried using javascript, additional php variables and combinations of both, but seem to be getting nowhere fast.

Any suggestions would be warmly welcomed.

<?
$i=0;
while ($i < $num) {
$ipath = mysql_result($result, $i, "image_path");
$iname = mysql_result($result, $i, "image_name");
$target ="picframe";
echo "<P><a href=".$ipath." target=".$target.">".$iname."</a>"; 
$i++;
}
?>
</td> 
<td width="50%"><iframe src="../blank.htm" name="picframe" scrolling="auto" height="175" width="100%" align="top" frameborder="0"></iframe></td>
  </tr>
  <tr>
    <td colspan="2">
      <form name="form1" method="post" action="">
        <div align="center">
         <label>
  <input type="text" name="pick2field" id="pick2field" size="30" value="<?php echo $ipath ?>"/>
  </label>   
        </div>
      </form>
    </td>
  </tr>

Looking at your code, I would expect that it would put the last $ipath that you read in the while loop into the text field. The user chooses a link from the list to put the image into the iframe but you don't have the same dynamic action happening for the path.

Thanks Chris,

Is there any way of getting around this without generating another SELECT, i.e. How would I get the FORM value to link to the variable the user has clicked - if that makes sense?

Thank you.

I'm still struggling here,

I have even tried to redefine the variable with an onclick event in the same event as the loop variable is being used, but the form variable is still not recognizing the correct value...any suggestions?

$i=0;
while ($i < $num) {
$ipath = mysql_result($result, $i, "image_path");
$iname = mysql_result($result, $i, "image_name");
$target ="picframe";
echo "<P><a href=".$ipath." target=".$target." onclick".$apath=$ipath">".$iname."</a>"; 
$i++;
}
?>
</td> 
<td width="50%"><iframe src="../blank.htm" name="picframe" scrolling="auto" height="175" width="100%" align="top" frameborder="0"></iframe></td>
  </tr>
  <tr>
    <td colspan="2">
      <form name="form1" method="post" action="">
        <div align="center">
         <label>
  <input type="text" name="pick2field" id="pick2field" size="30" value="<?php echo $apath ?>"/>
  </label>   
        </div>
      </form>
    </td>
  </tr>
<input type="text" name="pick2field" id="pick2field" size="30" value="<?php echo $apath ?>"/>

Where is $apath defined? I see it used in an onclick() but thats a javascript function executed on the client side and you're using $apath on the server side.

I'm just sayin...

thank you ppetree,

I believe this could be where my problem is, as I don't quite know how to define either the $apath value or get PHP to use the right ipath value in the form value.

Please help!

Looking at your code, I would expect that it would put the last $ipath that you read in the while loop into the text field. The user chooses a link from the list to put the image into the iframe but you don't have the same dynamic action happening for the path.

Thanks Chris,
I'm still struggling here, I've tried so many different variations but I think they're all on the same theme, given my limited knowledge.

I have even tried to redefine the variable with a client side onclick event in the same event as the loop variable that is being used, but the VALUE in the form is still not recognizing the correct variable...any suggestions?

Current time of trying to get a fix for this: 6 hours.....Current amount of hair left: < 10%

Many thanks.

What is apath supposed to be on the server side?

If $ipath is the image path and $iname is the image name, what is apath supposed to be? Is this what you want to be used as the form/text data?

If so, your apath on the server side will never work, its an empty field and you would want that to be $ipath. Would you not?

What is apath supposed to be on the server side?

If $ipath is the image path and $iname is the image name, what is apath supposed to be? Is this what you want to be used as the form/text data?

If so, your apath on the server side will never work, its an empty field and you would want that to be $ipath. Would you not?

Many thanks for your continued help,

$ipath is the file system path and file name of the image, i.e. /filesystem/image.png
$iname is a text description of the picture, i.e. Picture of me 2009.

Therefore I produce a list of image descriptions ($iname), i.e.

Picture of me 2009
Picture of me 2010
Picture of you 2010

and build a a href for each description using the $ipath, so I can show the actual image when the user clicks on the description (therefore when the user clicks on the description ($iname) I can retrieve the image from the file system ($ipath) and display a preview of the image.

On the display page, I also have a text form, and I would like to additionally populate the VALUE of the text form with the $ipath when the user clicks on the description ($iname).

FYI - I also have a textbox for the user to add some brief text (max 50 characters).

My ultimate aim is for the user to:
1. preview the stored images (completed)
2. on selection of the image, pass the path and image file name to a form VALUE
3. add some brief text

I can then post these value to a mysql table so later I can construct an html page that shows the selected image, with the user defined text sitting on top of the image.

By doing this I am expect to allow the user to dynamically construct/change a advertisement on their site, without 3rd party intervention.

Once complete I will to post the whole process + code (DB layout, adding images, retrieving images, image/text construction) so other can reuse.

Once again, many thanks for your continued assistance.

I basically understood what you were trying to do sans the overlaying text.

First, I would change your while() loop to a for() loop.

for($i=0; $i<$num; $i++) as I think that would be much cleaner and easier to read/understand AND is generally the method used on counting loops. While() loops are generally used for loops where you check a condition (non-counting) i.e. while(mysql_assoc($result)).

Lastly, the whole $apath thing you can throw out the window from the php side, you'll have to handle that in javascript because php on the server side can't possibly know what a user will do or click on the client side so the value=<?php echo "'$apath':; will always produce value='' and if you do a reveal source you will see this is indeed empty.

To do this, you'll create an onclick() function for your <a href= statement. And in the onclick you'll insert the info into the form using something like formname.textboxname.value=<?php echo "'$iname'"; ?> something like that.

Lastly I dont see how you are converting your $ipath into a url used by a <img src=''> tag. You could change the iframe src='' to blank.php and in your link call it with your <a href='blank.php?ipath=$ipath'> and then blank.php can use the $_GET[] variable to extract the image file name and build and return the appropriate html (including the <img src= ) to the iframe. This would seperate out the code and give you more control over the display AND make your code easier to follow.

Hope all this helps!

Pete

I basically understood what you were trying to do sans the overlaying text.

First, I would change your while() loop to a for() loop.

for($i=0; $i<$num; $i++) as I think that would be much cleaner and easier to read/understand AND is generally the method used on counting loops. While() loops are generally used for loops where you check a condition (non-counting) i.e. while(mysql_assoc($result)).

Lastly, the whole $apath thing you can throw out the window from the php side, you'll have to handle that in javascript because php on the server side can't possibly know what a user will do or click on the client side so the value=<?php echo "'$apath':; will always produce value='' and if you do a reveal source you will see this is indeed empty.

To do this, you'll create an onclick() function for your <a href= statement. And in the onclick you'll insert the info into the form using something like formname.textboxname.value=<?php echo "'$iname'"; ?> something like that.

Lastly I dont see how you are converting your $ipath into a url used by a <img src=''> tag. You could change the iframe src='' to blank.php and in your link call it with your <a href='blank.php?ipath=$ipath'> and then blank.php can use the $_GET[] variable to extract the image file name and build and return the appropriate html (including the <img src= ) to the iframe. This would seperate out the code and give you more control over the display AND make your code easier to follow.

Hope all this helps!

Pete

Manny, many thanks. This all makes very clear sense. As I said, once I get all to work, ill post the code so other may benefit.

Thank you.

You're welcome.

If you have what you need, mark it as solved... if you want to leave it open for a few days then thats fine too... just remember to close this out at some point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.