Jump to content

Archived

This topic is now archived and is closed to further replies.

Immortal55

Cannot figure out: calling images from DB

Recommended Posts

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:

[code=php:0]
<?
          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>';

}
?>
[/code]

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.

Share this post


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

[code]
<?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>';
}
?>
[/code]

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:

[code]
<?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";
?>
[/code]

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

Dest

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.