Page 1 of 1
passing Value outside loop?
Posted: Mon Nov 06, 2006 5:15 am
by Addos
Hi,
I’m trying to pass a value to WHERE ImageName = '$details’ in the code below but I can’t seem to find a way to do this.
If I run <?php echo $row_rstThumbs['ImageName']; ?> and it returns a value of say me.jpg and then hard code this into WHERE ImageName = 'me.jpg’ it works ok but when I try to pass this in a variable I either get an error saying “undefined variable details etc” I get the first or last value that the do loop returns.
Any ideas how to do this. I suppose what I’m trying to do is have the first value found in the loop passed to a variable for use outside the loop and this is where my problem is.
Thanks for any help
Brian
Code: Select all
<? $colname_GetDetails = "1";
if (isset($_GET['ImageName'])) {
$colname_GetDetails = (get_magic_quotes_gpc()) ? $_GET['ImageName'] : addslashes($_GET['ImageName']);
}
mysql_select_db($database_kelly, $kelly);
$query_GetDetails = sprintf("SELECT * FROM tblimages WHERE ImageName = '$details'", $colname_GetDetails);
$GetDetails = mysql_query($query_GetDetails, $kelly) or die(mysql_error());
$row_GetDetails = mysql_fetch_assoc($GetDetails);
$totalRows_GetDetails = mysql_num_rows($GetDetails); ?>
<?php
do { ?>
<?php $details= $row_rstThumbs['ImageName']; ?>
<a href="p7hg_img_4/fullsize/<?php echo $row_rstThumbs['ImageName']; ?> "><img src="p7hg_img_4/thumbs/<?php echo $row_rstThumbs['ThumbName']; ?>" ></a>
<?
} while ($row_rstThumbs = mysql_fetch_assoc($rstThumbs)); ?>
<?php echo $row_GetDetails['CaptionName']; ?>
<?php echo $row_GetDetails['TheCaption']; ?>
Posted: Mon Nov 06, 2006 6:27 am
by volka
sprintf("SELECT * FROM tblimages WHERE ImageName = '$details'", $colname_GetDetails);
php will substitute $details before the string is passed to sprintf. What is $details and what do you want sprintf to do?
Why do you fetch the first record and then start a do-while loop? There might be no record matching
WHERE ImageName = '$colname_GetDetails'. Also you've used a different name for the variable fetching the first record.
Better use a while{} loop.
Posted: Mon Nov 06, 2006 9:36 am
by Addos
Hi,
Thanks for the reply and as you can see I’m new to this. Basically in my database I have a field with details on each paining and I just wanted to display this on another part of my page to the right. You can see the actual page here
http://www.tyre-tyre.com/works_dynamic.php .
My problem was that I couldn’t get the information to display properly once outside the loop. For example if you look at the information on the right side you will see it doesn’t match the information at the bottom of the main image which I intended moving once I got this to display on the right hand side.
Below is the entire code of this page just in case it’s of any help. Maybe there is a better way but I thought if I could pass the value to $details= $row_rstThumbs['ImageName']; where $details would be like ‘0601_playing_on_sand.jpg’ then the correct information would be returned in relation to each main image on display. As I say if I hard code 0601_playing_on_sand.jpg (as seen in the live example) and replace $details with it, it works so I figured that’s all I needed in order to get this to work.
Thanks again for any further help
b
Code: Select all
<?php
mysql_select_db($database_kelly, $kelly);
$query_rstThumbs = "SELECT * FROM tblimages";
$rstThumbs = mysql_query($query_rstThumbs, $kelly) or die(mysql_error());
$row_rstThumbs = mysql_fetch_assoc($rstThumbs);
$totalRows_rstThumbs = mysql_num_rows($rstThumbs);
if (isset($row_rstThumbs['ImageName']) && file_exists('p7hg_img_4/fullsize/'.$row_rstThumbs['ImageName'])) {
$image_info_1 = getimagesize('p7hg_img_4/fullsize/'.$row_rstThumbs['ImageName']);
}
$dims_1 = isset($image_info_1) ? $image_info_1[3] : '';
?>
<body onLoad="P7_HGSinit(1,1,1,1,1,1,0,1)">
<div id="layout">
<div id="masthead2">
<div id="p7GSvpW3">
<div id="p7GSvp">
<div id="p7GSsc">
<ul>
<? $colname_GetDetails = "1";
if (isset($_GET['ImageName'])) {
$colname_GetDetails = (get_magic_quotes_gpc()) ? $_GET['ImageName'] : addslashes($_GET['ImageName']);
}
mysql_select_db($database_kelly, $kelly); // cant get $details to work here
$query_GetDetails = sprintf("SELECT * FROM tblimages WHERE ImageName = '0598_small_wooden_boat.jpg'", $colname_GetDetails);
$GetDetails = mysql_query($query_GetDetails, $kelly) or die(mysql_error());
$row_GetDetails = mysql_fetch_assoc($GetDetails);
$totalRows_GetDetails = mysql_num_rows($GetDetails); ?>
<?php
$counter = 0;
do { ?>
<?php
// include in loop to get each thumb dimension
if (isset($row_rstThumbs['ThumbName']) && file_exists('p7hg_img_4/thumbs/'.$row_rstThumbs['ThumbName'])) {
$image_info_1 = getimagesize('p7hg_img_4/thumbs/'.$row_rstThumbs['ThumbName']);
}$dims_t = isset($image_info_1) ? $image_info_1[3] : '';
if ($dims_t) { ?>
<li><div><?php echo $row_rstThumbs['ImageName']; ?> <?php echo $row_rstThumbs['TheCaption']; ?></div>
<a href="p7hg_img_4/fullsize/<?php echo $row_rstThumbs['ImageName']; ?> "<?php if ($dims_1) { ?> <?php echo $dims_1; }?>><img src="p7hg_img_4/thumbs/<?php echo $row_rstThumbs['ThumbName']; ?>" <?php echo $dims_t; }?> ></a>
</li>
<?php
$counter++;
if($counter % 4 == 0 && $totalRows_rstThumbs !=$counter)
{
echo "</ul><ul>";
}
} while ($row_rstThumbs = mysql_fetch_assoc($rstThumbs)); ?>
</ul>
</div>
</div>
</div>
<div id="p7GSnv"></div>
</div>
<div id="menubar">
<div id="p7TBM">
<div id="p7TBMroot">
<div id="p7TBMrootbox">
<ul>
<li><a href="#">home</a></li>
<li><a href="#">works</a></li>
<li><a href="#">biography</a></li>
<li><a href="#">contact</a></li>
<li><a href="#">news</a></li>
<li><a href="#">reviews</a><br>
</li>
</ul>
</div>
</div>
<!--[if lte IE 6]>
<style>#p7TBM div, #p7TBM ul, #p7TBM a {height:1%;}</style>
<![endif]-->
</div>
</div>
<div id="columnwrapper">
<div id="mainbox">
<div id="maincontent">
<div id="p7GS">
<div><?php //echo $name; ?> <?php //echo $row_GetDetails['TheCaption']; ?></div>
<div id="p7GSfs"></div>
<div id="p7GSpb"><img src="p7gs/images/prog-bar.gif" alt="Now Loading" width="125" height="22"></div>
<div id="p7GSah"></div>
<!--[if IE 5]><style>#p7GSvp,#p7GSfsc,#p7GSfsd{text-align:left;}#p7GSnv a{height:1em;width:1em;}</style><![endif]-->
<!--[if lte IE 6]><style>#p7GS{height:1%;}</style><![endif]-->
</div>
</div>
</div>
<div id="sidebox">
<div id="sidecontent">
<h3 class="topheading">Details</h3>
<p class="topheading">
<?php echo $row_GetDetails['CaptionName']; ?>
<?php echo $row_GetDetails['TheCaption']; ?>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div></div>
<br class="clearit">
</div>
<div id="bottom"></div>
</div>
<div id="footerbox">
<div id="footer">
<p><?php include('footer.php'); ?></p>
</div>
</div>
</body>