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.