Hi guys, you've helped me out before and I'm hoping you'll be able to help me out again.
This is actually a two-part question.
I have a gallery which I'm trying to create, and while the current code I have works with one table only, but after doing union selects, it messes up.
Here's my code (I've omitted the unnecessary parts and simplified the table entries)
<?php
// database connection stuff here
$x = $_GET["year"];
$cat = $_GET["year"];
$i=1;
if ($cat=="")
{ $query .= "select * from table1 union select * from table2 order by datetime desc "; }
else
{ $query .= "select * from table1 union select * from table2 where year='$cat' order by datetime desc "; }
$result = mysql_query($query, $connection) or die
("Could not execute query : $query ." . mysql_error());
$rows = mysql_num_rows($result);
if ($rows=="0") { echo "Nothing found."; }
$db_query .= "select * from table1 union select * from table2 order by datetime desc ";
if($x!="")
$db_query .= "where year='".$x."' ";
// Start paging variables
$screen = $_GET['screen'];
$PHP_SELF = $_SERVER['PHP_SELF'];
$rows_per_page=16; // number of records per page
$total_records=mysql_num_rows($result);
$pages = ceil($total_records / $rows_per_page); // calculate number of pages required
if (!isset($screen))
$screen=0;
$start = $screen * $rows_per_page; // determine start record
$query .= "LIMIT $start, $rows_per_page";
$result= mysql_query($query) or die
("Could not execute query : $query ." . mysql_error());
while ($row=mysql_fetch_array($result))
{
$id=$row["id"];
$name=$row["name"];
$type=$row["type"];
$year=$row["year"];
include "(template.php)";
if ($i==4) {
echo "<div class=\"clear\"></div>";
$i=0;
}
$i++;
}
echo "<div class=\"clear\"></div>
<div class=\"wardrobe-pagination\">";
// create the links
if ($screen > 0) {
$j = $screen - 1;
$url = "{$_SERVER['PHP_SELF']}?year=$cat&screen=$j";
echo "<a href=\"$url\">Previous</a>"; // generate the prev text link so long as page is not first page
}
// page numbering links now
$p = 5; // number of links to display per page
$lower = $p; // set the lower limit to $p
$upper = $screen+$p; // set the upper limit to current page + number of links per page
while($upper>$pages){
$p = $p-1;
$upper = $screen+$p;
}
if($p<$lower){
$y = $lower-$p;
$to = $screen-$y;
while($to<0){
$to++;
}
}
if(!empty($to))
{ for ($i=$to;$i<$screen;$i++){
$url = "{$_SERVER['PHP_SELF']}?year=$cat&screen=" . $i;
$j = $i + 1;
echo "<a href=\"$url\">$j</a>";
}
}
for ($i=$screen;$i<$upper;$i++) {
$url = "{$_SERVER['PHP_SELF']}?year=$cat&screen=" . $i;
$j = $i + 1;
echo "<a href=\"$url\">$j</a>";
}
if ($screen < $pages-1) {
$j = $screen + 1;
$url = "{$_SERVER['PHP_SELF']}?year=$cat&screen=$j";
echo "<a href=\"$url\">Next</a>";
}
?>
Here is what I have in my tables:
table1
(id) (name) (year) (type)
1 straw 2010 dress
2 orange 2010 dress
3 blue 2009 dress
(it goes on but this is the general idea)
table2
(id) (name) (year) (type)
1 exile 2003 skirt
2 poof 2005 skirt
(etc...)
Now, as for my actual problem. When I have one table selected (ie. table1), the above code will display my results and I have the ability to categorize the year by using ".php?year=(year)". However, once I union select table2, when I attempt to categorize by year, the page will display ALL results (regardless of the year) on its corresponding page and will only display the selected year on top. (ie. instead of only displaying "2003" results, on page 1 it will display "2003, 2010, 2009, 2008, 2007" and on page 2 it will display "2003, 2010, 2010, 2010, 2009", when I want it to ONLY display 2003 results.)
I can show you the code in action, if it is any help.
As for the second part of my question, I want to be able to categorize by other categories (other than year), such as "type". How would I be able to do that?