Jump to content

Cannot figure out: calling images from DB


Immortal55

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.
Link to comment
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
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.