Horizontal display of image from mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bickyz
Forum Newbie
Posts: 5
Joined: Tue Dec 01, 2009 12:54 pm

Horizontal display of image from mysql

Post by bickyz »

hi,
im trying to do one college project where i have to display images horizontally. The image links are saved in the mysql db with the surname.

This is the script:

Code: Select all

 
<?php require_once('Connections/conn1.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
 
<body>
<?php
  $letter = isset($_GET['letter']) ? $_GET['letter'] : "0";
  //alphabetical pagination links
  echo '<div align="center"><b>';
  foreach(range('A','Z') as $c){
    ($letter == $c)
      ? printf('%s&nbsp',$c)
      : printf('<a href="?letter=%s">%s</a>&nbsp;',$c,$c);
  }
  echo "</b></div><p>";
 
mysql_select_db($database_conn1, $conn1);
$query_rs1 = "SELECT * FROM tblvdo WHERE UCASE(surname) LIKE '{$letter}%' order by surname";
$rs1 = mysql_query($query_rs1, $conn1) or die(mysql_error());
?>
<table >
  <tr>
    <?php
$rs1_endRow = 0;
$rs1_columns = 4; // number of columns
$rs1_hloopRow1 = 0; // first row flag
do {
    if($rs1_endRow == 0  && $rs1_hloopRow1++ != 0) echo "<tr>";
   ?>
    <td><img src="<?php echo $row_rs1['imgurl']; ?>" width="100px"><td>
    <?php $rs1_endRow++;
if($rs1_endRow >= $rs1_columns) {
  ?>
  </tr>
  <?php
$rs1_endRow = 0;
  }
} while ($row_rs1 = mysql_fetch_assoc($rs1));
if($rs1_endRow != 0) {
while ($rs1_endRow < $rs1_columns) {
    echo("<td>&nbsp;</td>");
    $rs1_endRow++;
}
echo("</tr>");
}?>
</table>
</body>
</html>
<?php
mysql_free_result($rs1);
?> 
 
In this project the alphabetical pagination will be pulling all the images horizontally with the particular letter from the surname field.

you can check live demo here: http://centos.uk.to/vdo3.php

The problem is if you click on letter B or S, there is a blank section of the same size as image before first image. I checked the source of html and found this line there and i dont no how its appearing

Code: Select all

 
<td><img src="" width="100px"><td>
 
I will be grateful if someone can please help me finding the problem so that i dont get blank section before images.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Horizontal display of image from mysql

Post by AbraCadaver »

You're trying to use $row_rs1['imgurl'] in the img but you don't fetch the $row_rs1 array until the end of the loop. Try replacing the do with a while.

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Horizontal display of image from mysql

Post by John Cartwright »

AbraCadaver wrote:You're trying to use $row_rs1['imgurl'] in the img but you don't fetch the $row_rs1 array until the end of the loop. Try replacing the do with a while.

-Shawn
Better yet...

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
   //each row
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Horizontal display of image from mysql

Post by AbraCadaver »

John Cartwright wrote:
AbraCadaver wrote:You're trying to use $row_rs1['imgurl'] in the img but you don't fetch the $row_rs1 array until the end of the loop. Try replacing the do with a while.

-Shawn
Better yet...

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
   //each row
}
Better? That's exactly what I said! :-) However, better yet...

Code: Select all

while ($row_rs1 = mysql_fetch_assoc($rs1)) {
 //each row
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
bickyz
Forum Newbie
Posts: 5
Joined: Tue Dec 01, 2009 12:54 pm

Re: Horizontal display of image from mysql

Post by bickyz »

sorry im newbie in this php. I normally use dreamweaver to desing dynamic site using php mysql where no hardcoding is require. This one i used bit code from dreamweaver-php mysql then found somebodys code in internet ; modified both of them to make one and thats what it gave me (98%) of my job.

please tell me what will be the modified code:

is it like this?

Code: Select all

 
<body>
<?php
  $letter = isset($_GET['letter']) ? $_GET['letter'] : "0";
  //alphabetical pagination links
  echo '<div align="center"><b>';
  foreach(range('A','Z') as $c){
    ($letter == $c)
      ? printf('%s&nbsp',$c)
      : printf('<a href="?letter=%s">%s</a>&nbsp;',$c,$c);
  }
  echo "</b></div><p>";
 
mysql_select_db($database_conn1, $conn1);
$query_rs1 = "SELECT * FROM tblvdo WHERE UCASE(surname) LIKE '{$letter}%' order by surname";
$rs1 = mysql_query($query_rs1, $conn1) or die(mysql_error());
?>
<table >
  <tr>
    <?php
$rs1_endRow = 0;
$rs1_columns = 4; // number of columns
$rs1_hloopRow1 = 0; // first row flag
while ($row = mysql_fetch_assoc($result)) {
//each row
}?>
</table>
</body>
</html>
<?php
mysql_free_result($rs1);
?>
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Horizontal display of image from mysql

Post by AbraCadaver »

From your original post, change your line 63 to my line 1. Change your line 74 to my line 3.

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
bickyz
Forum Newbie
Posts: 5
Joined: Tue Dec 01, 2009 12:54 pm

Re: Horizontal display of image from mysql

Post by bickyz »

i did xactly as u said but im getting error:
http://centos.uk.to/vdo4.php

i replaced
do {
to
while ($row_rs1 = mysql_fetch_assoc($rs1)) {

and
} while ($row_rs1 = mysql_fetch_assoc($rs1));
to
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Horizontal display of image from mysql

Post by John Cartwright »

AbraCadaver wrote:
John Cartwright wrote:
AbraCadaver wrote:You're trying to use $row_rs1['imgurl'] in the img but you don't fetch the $row_rs1 array until the end of the loop. Try replacing the do with a while.

-Shawn
Better yet...

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
   //each row
}
Better? That's exactly what I said! :-) However, better yet...

Code: Select all

while ($row_rs1 = mysql_fetch_assoc($rs1)) {
 //each row
}
Oops. I thought you said a do-while loop :D
Post Reply