Linking Image Table w/ Text Table  | Site Addict | | Join Date: Jul 2008 Location: Ocala, FL (United States)
Posts: 817
| |
Hey Bytes,
The website I'm working on is coming along just fine, and I'd like to thank all of you PHP folks who have been helping me out. I'm almost done with the coding!
I'm trying to get the data-basing code finished with. I've got my products, with lines of text next to it that serve as descriptions. With each of those entries, I have images. I've implemented the code for displaying the text, and I've also implemented the code for the images, but here is my problem:
The code is separate. The images are queried and displayed to the user in a list formation, and so is the text, but I don't have a clue how to put the two together.
Here is my code for displaying the text components: - <?php
-
include('books_login.php');
-
$connection=mysql_connect($mysql_host,$mysql_user,$mysql_password);
-
if(!$connection){
-
die("Could not connect to the database: <br/>". mysql_error());
-
}
-
$db_select=mysql_select_db($mysql_database);
-
if(!$db_select){
-
die("Could not select the database: <br/>".mysql_error());
-
}
-
-
// how many rows to show per page
-
$rowsPerPage = 10;
-
-
// by default we show first page
-
$pageNum = 1;
-
-
// if $_GET['page'] defined, use it as page number
-
if(isset($_GET['page']))
-
{
-
$pageNum = $_GET['page'];
-
}
-
-
// counting the offset
-
$offset = ($pageNum - 1) * $rowsPerPage;
-
-
$query = " SELECT * FROM air_registers" .
-
" LIMIT $offset, $rowsPerPage";
-
$result = mysql_query($query) or die('Error, query failed');
-
-
-
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
-
$type=$row["Type"];
-
$number=$row["Number"];
-
$desc=$row["Description"];
-
echo "<div id='type'><strong>Model Type: </strong>$type</div>";
-
echo "<div id='number'><strong>Model #: </strong>$number</div>";
-
echo "<br />";
-
echo "<div id='desc'><strong>Description: </strong>$desc</div>";
-
echo "<br />";
-
echo "<hr>";
-
}
-
// how many rows we have in database
-
$query = "SELECT COUNT(*) AS numrows FROM air_registers";
-
$result = mysql_query($query) or die('Error, query failed');
-
$row = mysql_fetch_array($result, MYSQL_ASSOC);
-
$numrows = $row['numrows'];
-
-
// how many pages we have when using paging?
-
$maxPage = ceil($numrows/$rowsPerPage);
-
-
// print the link to access each page
-
$self = $_SERVER['PHP_SELF'];
-
$nav = '';
-
-
for($page = 1; $page <= $maxPage; $page++)
-
{
-
if ($page == $pageNum)
-
{
-
$nav .= " $page "; // no need to create a link to current page
-
}
-
else
-
{
-
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
-
}
-
}
-
// ... the previous code
-
-
// creating previous and next link
-
// plus the link to go straight to
-
// the first and last page
-
-
if ($pageNum > 1)
-
{
-
$page = $pageNum - 1;
-
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
-
-
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
-
}
-
else
-
{
-
$prev = ' '; // we're on page one, don't print previous link
-
$first = ' '; // nor the first page link
-
}
-
-
if ($pageNum < $maxPage)
-
{
-
$page = $pageNum + 1;
-
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
-
-
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
-
}
-
else
-
{
-
$next = ' '; // we're on the last page, don't print next link
-
$last = ' '; // nor the last page link
-
}
-
-
// print the navigation link
-
echo $first . $prev . $nav . $next . $last;
-
-
mysql_close($connection);
-
?>
This works fine and displays my text information.
Here is the code for the image output to the user: - <?php
-
include('books_login.php');
-
include('pix.php');
-
$connection=mysql_connect($mysql_host,$mysql_user,$mysql_password);
-
if(!$connection){
-
die("Could not connect to the database: <br/>". mysql_error());
-
}
-
$db_select=mysql_select_db($mysql_database);
-
if(!$db_select){
-
die("Could not select the database: <br/>".mysql_error());
-
}
-
-
$strSQL = "select * from pix";
-
$rsPix = mysql_query($strSQL);
-
$numRows = mysql_numrows($rsPix);
-
$i = 0;
-
-
while($i < $numRows){
-
?>
-
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
-
<?php
-
$i++;
-
}
-
?>
-
-
And the code above refers to pix.php: - <?php
-
include('books_login.php');
-
$connection=mysql_connect($mysql_host,$mysql_user,$mysql_password);
-
if(!$connection){
-
die("Could not connect to the database: <br/>". mysql_error());
-
}
-
$db_select=mysql_select_db($mysql_database);
-
if(!$db_select){
-
die("Could not select the database: <br/>".mysql_error());
-
}
-
if (IsSet($_GET['pixID'])){
-
$gotten = @mysql_query("select imgdata from pix where pixID = ".$_GET['pixID']);
-
header("Content-type: image/jpeg");
-
while ($row = mysql_fetch_array($gotten,MYSQL_ASSOC))
-
{
-
print $row['imgdata'];
-
-
}
-
mysql_free_result($gotten);
-
}
-
?>
I have two tables in my mysql DB:
-one for the images (composed of a BLOB field and an int field for Unique Key)
-one for the text (composed of three or four varchar fields and an int field for Unique Key)
I believe I'm supposed to link these tables together using keys... is that correct? Then maybe I could write code that will utilize the two linked tables? I'm not sure what direction I should go here.
|  | Site Addict | | Join Date: Jul 2008 Location: Ocala, FL (United States)
Posts: 817
| | | re: Linking Image Table w/ Text Table
You know, I think I almost have this figured out. I'll post new code when I solve the problem.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,947
| | | re: Linking Image Table w/ Text Table
Assuming your ID (unique key) columns are a reliable relationship between the tables, simply perform a query to get your image data (as well as getting it's ID). Then use this ID in your second query for the details (using the ID as a WHERE clause).
You get me? Sorry if I'm a little hazy, I was at a wedding yesterday.
|  | Site Addict | | Join Date: Jul 2008 Location: Ocala, FL (United States)
Posts: 817
| | | re: Linking Image Table w/ Text Table
Hey Markus, thanks for the reply.
I've tried instead just making one table with all of my different items (I merged the imgdata with all of my varchars into one table). Is this being lazy? I understand that this method is poor normalization, but it seems easiest for me right now. Here is what I tried (and its not working): -
$query = " SELECT * FROM air_registers" .
-
" LIMIT $offset, $rowsPerPage";
-
$result = mysql_query($query) or die('Error, query failed');
-
-
-
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
-
$thumb=$row["Thumbnail"];
-
$type=$row["Type"];
-
$number=$row["Number"];
-
$desc=$row["Description"];
-
$i = 0;
-
echo "<div id='type'><strong>Model Type: </strong>$type</div>";
-
echo "<div id='number'><strong>Model #: </strong>$number</div>";
-
echo "<br />";
-
-
echo '<img src="pix.php?pixID=<?php echo mysql_result($result,$i,"pixID"); ?>"/>';
-
-
echo "<br />";
-
echo "<div id='desc'><strong>Description: </strong>$desc</div>";
-
echo "<br />";
-
echo "<hr>";
-
$i++;
Everything in bold is what was taken from my old list.php file. I know this is sloppy, but I'm trying to make it work. I'm very unhappy with the way it looks now, so more than likely I'll try to query two different tables (like you suggested Markus).
|  | Site Addict | | Join Date: Jul 2008 Location: Ocala, FL (United States)
Posts: 817
| | | re: Linking Image Table w/ Text Table
Hey all,
I ended up just dropping the images from my database. I put them in the file manager, and I'm using this bit of code to pull them out and onto the page: - echo "<img src=\"".$pixID."\" />"
$pixID is a field in my database that refers to the image name. This works fine for now, and I'll look into using the BLOB field later. I'm going to look through a lot of documentation and tutorials before I try that method again (the method that utilizes a BLOB field with images in the database instead of the file manager). Question:
If I have my images in the file manager, can I still implement an extension that will automatically re-size my pictures? I was looking at a GD extension provided by Atli in post #2 of this thread. Can I still write a PHP script that will change the picture size before it is rendered using this GD extension? Or is that a problem because my pictures are on the file manager instead of the MySQL database?
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,947
| | | re: Linking Image Table w/ Text Table Quote:
Originally Posted by tharden3 Hey all,
I ended up just dropping the images from my database. I put them in the file manager, and I'm using this bit of code to pull them out and onto the page: - echo "<img src=\"".$pixID."\" />"
$pixID is a field in my database that refers to the image name. This works fine for now, and I'll look into using the BLOB field later. I'm going to look through a lot of documentation and tutorials before I try that method again (the method that utilizes a BLOB field with images in the database instead of the file manager). Question:
If I have my images in the file manager, can I still implement an extension that will automatically re-size my pictures? I was looking at a GD extension provided by Atli in post #2 of this thread. Can I still write a PHP script that will change the picture size before it is rendered using this GD extension? Or is that a problem because my pictures are on the file manager instead of the MySQL database? You can still use GD on files that are located on your server. Although, providing 'on-the-fly' resized images is very resource intensive. The smarter thing to do is save a resized version and the original.
|  | Site Addict | | Join Date: Jul 2008 Location: Ocala, FL (United States)
Posts: 817
| | | re: Linking Image Table w/ Text Table Quote:
Originally Posted by Markus You can still use GD on files that are located on your server. Although, providing 'on-the-fly' resized images is very resource intensive. The smarter thing to do is save a resized version and the original. Ok, thats what I've been doing for now. I have a "full version" of each image and a re-sized "thumbnail" version. I've been using GIMP to re-size each image before I put it on the server. Is that ok for now? Or is there something quicker I could be doing? Anything to cut down on my production time is welcome ;)
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,947
| | | re: Linking Image Table w/ Text Table Quote:
Originally Posted by tharden3 Ok, thats what I've been doing for now. I have a "full version" of each image and a re-sized "thumbnail" version. I've been using GIMP to re-size each image before I put it on the server. Is that ok for now? Or is there something quicker I could be doing? Anything to cut down on my production time is welcome ;) You could cut out GIMP and upload the images through a form, which PHP would then create a resized image, and save them both.
|  | Site Addict | | Join Date: Jul 2008 Location: Ocala, FL (United States)
Posts: 817
| | | re: Linking Image Table w/ Text Table Quote:
Originally Posted by Markus You could cut out GIMP and upload the images through a form, which PHP would then create a resized image, and save them both. I'll try this out and post my results.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|