Jump to content


Photo

Cannot figure out: calling images from DB


  • Please log in to reply
2 replies to this topic

#1 Immortal55

Immortal55
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 13 September 2006 - 09:41 PM

Alright, well what i am trying to do is, I have a database and in the database it stores some info on an image, blah blah. and What I am trying to do is be able to call the images from the database. but when i do it, the same image comes up for all the images that are called from a catagory...

here is the script:

<?
          require_once('dbconnect.php');
          $conn = db_connect();
          $db = @mysql_select_db ("theblizz_mitchanderson", $conn)
              or die ("Registration failure, try again.");
    
    $sql = mysql_query("SELECT * FROM photos WHERE catagory = '{$_REQUEST['catagory']}' ORDER BY id DESC") or die(mysql_error());

while($row = mysql_fetch_array($sql))
    {
	
    stripslashes($row);	
	$path = $row['path'];
	$id = $row['id'];
	$image = $path;
	
	if(!$max_width)
		$max_width = 150;
	if(!$max_height)
		$max_height = 150;

	$size = GetImageSize($image);
	$width = $size[0];
	$height = $size[1];

	$x_ratio = $max_width / $width;
	$y_ratio = $max_height / $height;

	if( ($width <= $max_width) && ($height <= $max_height) ) {
		$tn_width = $width;
		$tn_height = $height;
		}
	else 
		if (($x_ratio * $height) < $max_height) {
		$tn_height = ceil($x_ratio * $height);
		$tn_width = $max_width;
		}
	else {
		$tn_width = ceil($y_ratio * $width);
		$tn_height = $max_height;
		}}
		
$sql = mysql_query("SELECT * FROM photos WHERE catagory = '{$_REQUEST['catagory']}' ORDER BY id DESC") or die(mysql_error());
		
while($row = mysql_fetch_array($sql))
    {
    stripslashes($row);	
	
		echo '<a href="deletepic.php?id=' . $id . '">';
		echo '<img src="' . $path . '" width="' . $tn_width . '" height="' . $tn_height . '" border="1">';
		echo '</a><br>';
						
		}
		?>

So pretty much, its just calling the same image x amount of times, i know its the same image because its the same id number, and the actual same image...


Thanks in advance.

#2 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 13 September 2006 - 10:37 PM

I did a quick reformat and moving around of the code to clear things up a little bit.  I'll write an explanation below.

<?php
$sql = mysql_query("SELECT * FROM photos WHERE catagory = '{$_REQUEST['catagory']}' ORDER BY id DESC") or die(mysql_error());

while($row = mysql_fetch_array($sql))
{
	$path = $row['path'];
	$id = $row['id'];
	$image = $path;
	
	if(!$max_width)
		$max_width = 150;
	if(!$max_height)
		$max_height = 150;
	
	$size = GetImageSize($image);
	$width = $size[0];
	$height = $size[1];
	
	$x_ratio = $max_width / $width;
	$y_ratio = $max_height / $height;
	
	if(($width <= $max_width) && ($height <= $max_height))
	{
		$tn_width = $width;
		$tn_height = $height;
	}
	elseif(($x_ratio * $height) < $max_height)
	{
		$tn_height = ceil($x_ratio * $height);
		$tn_width = $max_width;
	}
	else
	{
		$tn_width = ceil($y_ratio * $width);
		$tn_height = $max_height;
	}
	echo '<a href="deletepic.php?id=' . $id . '">';
	echo '<img src="' . $path . '" width="' . $tn_width . '" height="' . $tn_height . '" border="1">';
	echo '</a><br>';
}
?>

Okay, let's take a look at this.  Your code executes the SQL Query twice, which it doesn't need to be doing.  If you use a value from a while loop outside of the loop, it will only return the last value of the loop unless you entered them into an array, concatenated string, etc and used that for a fresh loop.  For your purposes, you would be better doing as above and moving the output mechanism INSIDE the while loop so that it outputs on each loop (Another way of doing this would be to assign the output to a variable by concatenation then echo that AFTER/OUTSIDE the loop).

A basic concept I use for while loops is this:

<?php
$output = ""; //Define the output variable explicitly before the loop
while(condition)
{
    $output .= ""; //This would be adding output to the variable, such as $row['path']
}
echo $output; //Echo the output or manipulate it etc
?>

//The same can be done with arrays like so:
<?php
$output = array();
while(condition)
{
    $output[] = "";
}
echo "<pre>\n";
print_r($output);
echo "</pre>\n";
?>

I hope I've been clear enough but if not please say and I'll try to explain it differently.

Dest

#3 Immortal55

Immortal55
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 14 September 2006 - 02:12 AM

Well, thank you, you cleared up a lot of questions I had even thought I did'nt ask. The explanation of the while loop helped a lot too, thank you.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users