Jump to content

[SOLVED] File EXISTS command with QUERY ? its for paging system ...


Recommended Posts

ok well i have a file exists on my page.  It check to see if there is a picture of the product.  If it exists, then it displays the item.  It works just fine.  I recently got a paging system to work to split the query results into numerous pages.  Now here is how it normaly works

 

QUERY

IF FILE EXISTS

  - display item

 

now here is how the paging system works..

QUERY - AND COUNT # OF ROWS MATCHING DATA

QUERY - split rows of matching data by PAGE LIMIT, etc.

 

NOW, the problem is when you mix them, the file exists works, but on the page #'s on the bottom counts ALL ITEMS MATCHING QUERY, just doesnt display them because of the file exists.  My question is to know if there is a way do the FILE EXISTS in conjunction with the paging system. 

 

so for example. i have 40 items in database, 4 of them have pics.  Paging system shows 4 per page... so the query finds all 40 items that match criteria and divides them my page limit (4) so you get 20 pages.  So the first page has the 4 items with pictures, then the last 19 pages display nothing because the FILE EXISTS is over riding it. 

 

So again, is there a way to use the FILE EXISTS inside a query, or do the FIEL EXISTS and make a variable out of it and use that somehow? THANKS!

NOBODY ?

 

When I first started, I had a field in the table called "PICTURE" and it was YES OR NO, and in the PHP in the Query i had "if picture = yes", etc.  Then i found out the file exists thing and that was way better than the old solution, but NOW, i could use that field again.  Is there some way to use the file exists in conjuction with a query, to only SELECT the stuff that had a successful FILE EXISTS command ?

yeah sorry i didnt post code earlier, I was making a smaller simpler test page to go from, here is the code below.  The first query checks the results and counts them to figure out the pages.  Then the second query searches for the SAME thing, but limits it depending on the number of pages it determines.  I need to limit BOTH of these queries with a file exists.    You'll notice I already have a FILE EXIST in the middle of the page after the second query.  This is for the WHILE thats checked each result, i don't want it displaying if a pic dont exists, this works fine, i just need to have the other queries work somehow to NOT COUNT the results if i don't have a picture for it.

 

[pre]

<?
mysql_connect("xxxx", "xxxx", ""xxxx) or die(mysql_error());
mysql_select_db("mczippo") or die(mysql_error());

//if ((file_exists("images/motorclothes/" . $strSQL['Cat1'] . "/" . $strSQL['Cat2'] . "/" . $strSQL['Style'] . ".jpg")) AND (file_exists("images/motorclothes/" . $strSQL['Cat1'] . "/" . $strSQL['Cat2'] . "/" . $strSQL['Style'] . ".gif")))
//{
//	$picexists = 'yes';
//}

$pagelimit = "2";

// need to somehow get this query below to work with file exists!! FOR EXAMPLE: 40 results, but only 4 have pictures in varaibled folder, so i need it to only find the 4 results

$strSQL = mysql_query("SELECT * FROM `mczippo1` WHERE Cat2 ='zippo lighters' AND OnHand > MinQty GROUP BY Style ORDER BY OnHand Desc");

   $totalrows = mysql_num_rows($strSQL);
   $pagenums = ceil ($totalrows/$pagelimit);
    if ($page==''){
        $page='1';
    }
    $start = ($page-1) * $pagelimit;
$starting_no = $start;
if ($totalrows - $start < $pagelimit) {
   $end_count = $totalrows;
} elseif ($totalrows - $start >= $pagelimit) {
   $end_count = $start + $pagelimit;
}
echo "<div align='center'>Showing $starting_no to $end_count of " . $totalrows . " results<br>\n";

if ($totalrows - $end_count > $pagelimit) 
	{
   $var2 = $pagelimit;
	} 
	elseif ($totalrows - $end_count <= $pagelimit) 
		{
		   $var2 = $totalrows - $end_count;
		}
$space = " ";
if ($page > 1) 
{
        echo "« <a href='page4.php?page=".($page-1)."' class=main>Previous" . $space . $pagelimit . "</a>" . $space . $space . "";
    }
    for ($i=1; $i<=$pagenums; $i++) 
	{
        if ($i!=$page) 
				{
            echo " <a href='page4.php?page=$i' class=main>$i</a>";
        			}
       				 else 
					{
		            echo " <b class='red'>$i</b>";
		        	}
}
    if ($page < $pagenums) 
{
        echo "" . $space . $space . $space . $space . " <a href='page4.php?page=".($page+1)."' class=main>Next " . $var2 . "</a>»";
}

$strSQL = mysql_query("SELECT * FROM `mczippo1` WHERE Cat2='Zippo Lighters' AND OnHand > MinQty GROUP BY Style ORDER BY OnHand Desc LIMIT $start,$pagelimit");
while ($part = mysql_fetch_array($strSQL))
	{
		if ((file_exists("images/motorclothes/" . $part['Cat1'] . "/" . $part['Cat2'] . "/" . $part['Style'] . ".jpg")) AND (file_exists("images/motorclothes/" . $part['Cat1'] . "/" . $part['Cat2'] . "/" . $part['Style'] . ".gif")))
		{
			echo 	"<div align='center'><br>
					<form action='mcdisplaypart.php?Categ1=Shirt&Categ2=Tee&Style=" . $part['Style'] . "' METHOD='post' style='display:inline;'>
					<INPUT TYPE='image' src='images/motorclothes/" . $part['Cat1'] . "/" . $part['Cat2'] . "/" . $part['Style'] . ".gif' BORDER=0 ALT='" . $part['Style'] . "'>
					</form><br>
					";
		}
	}
	echo " 	</td>
			</tr>
			</table>";



?> 

[/pre]

watch, here is the problem i took a screen shot.  ok, say thers 40 items that CATEGORY 2 = zippo lighters....  but only 4 have pictures.. the paging system counts EVERYTHING thats in Category 2 - Zippo Lighters, and adds it to the number of pages...  here is a screen shot of what im talking about.  For test purposes I set the page limit to 2 (to only show 2 items per page)  so the first 2 pages have lighters, and the rest have NOTHING,  this is the problem...

 

page4

ok here i change the PAGELIMIT to 5, so it shows 5 items per page, since these 4 are the only 1s with pics, the FILE EXISTS only shows those 4, however... the row counting QUERY does not have a way to check if FILE EXISTS, so as you can see, it only shows the 4, but still COUNTS all the other items in that category.

 

this is the query that finds the rows and counts them.  I need the WHERE clause to somehow know if the file exists and to count the row if it does.  So is there a way to query and if the file exists, then creata a variable out of it and add it to the query or something?

$strSQL = mysql_query("SELECT * FROM `mczippo1` WHERE Cat2 ='zippo lighters' AND OnHand > MinQty GROUP BY Style ORDER BY OnHand Desc");

 

Here is with page limit = 5, as you can see the number of pages went down because 40 results, 5 per page..  = 9 pages. if you go to page 2, the FILE EXISTS knows the picture does not exists, so it doesnt display the item, but again, the query still counts it in the results.

page4b.jpg

 

page4c.jpg

 

PS. sorry the code comes out so ugly above post, it doesnt look like that on my screen.  Yes mine is spaced for easy reading, but not like that. hahaha

You should query for the number of items, check for file exists, and loop through only those ones that have pictures. If you needed to do it in a second query you could make a query string out of the ones that have pictures in a loop where you append a string with "WHERE item = '$OneThatHasAPicture' ", then print that query out.

thats what it is doing... QUERY the number of items, and looping the print for the ones with pictures.  The problem isnt printing them, its COUNTING the ones with pictures, thats what i need to know how to do...  thanks for your help by the way.  Ive been stuck on this for a while.    I need to count ONLY the ones with pictures and use that final total to determine the number of pages.  Here I cleaned up my code a little and separated it into the 2 queries.

 

this first query finds the items in the table and counts them and displays the paging #'s at the top.

[pre]

$pagelimit = "5";

$strSQL = mysql_query("SELECT * FROM `mczippo1` WHERE Cat2 ='zippo lighters' AND OnHand > MinQty GROUP BY Style ORDER BY OnHand Desc");

$totalrows = mysql_num_rows($strSQL);
$pagenums = ceil ($totalrows/$pagelimit);
if ($page==''){
$page='1';
}
$start = ($page-1) * $pagelimit;
$starting_no = $start;
if ($totalrows - $start < $pagelimit) {
$end_count = $totalrows;
} elseif ($totalrows - $start >= $pagelimit) {
$end_count = $start + $pagelimit;
}
echo "<div align='center'>Showing $starting_no to $end_count of " . $totalrows . " results<br>\n";
if ($totalrows - $end_count > $pagelimit) 
{
$var2 = $pagelimit;
} 
elseif ($totalrows - $end_count <= $pagelimit) 
{
$var2 = $totalrows - $end_count;
}
$space = " ";
if ($page > 1) 
{
echo "« <a href='page4.php?page=".($page-1)."' class=main>Previous" . $space . $pagelimit . "</a>" . $space . $space . "";
}
for ($i=1; $i<=$pagenums; $i++) 
{
if ($i!=$page) 
{
echo " <a href='page4.php?page=$i' class=main>$i</a>";
}
else 
{
echo " <b class='red'>$i</b>";
}
}
    if ($page < $pagenums) 
{
        echo "" . $space . $space . $space . $space . " <a href='page4.php?page=".($page+1)."' class=main>Next " . $var2 . "</a>»";
}

[/pre]

 

this second query loops through them and prints them.  this has nothing to do with the pages though EXCEPT, the query has a limit on it as to how many to display per page.

 

[pre]

$strSQL = mysql_query("SELECT * FROM `mczippo1` WHERE Cat2='Zippo Lighters' AND OnHand > MinQty GROUP BY Style ORDER BY OnHand Desc LIMIT $start,$pagelimit");
while ($part = mysql_fetch_array($strSQL))
{
if ((file_exists("images/motorclothes/" . $part['Cat1'] . "/" . $part['Cat2'] . "/" . $part['Style'] . ".jpg")) AND (file_exists("images/motorclothes/" . $part['Cat1'] . "/" . $part['Cat2'] . "/" . $part['Style'] . ".gif")))
{
echo 	"<div align='center'><br>
<form action='mcdisplaypart.php?Categ1=Shirt&Categ2=Tee&Style=" . $part['Style'] . "' METHOD='post' style='display:inline;'>
<INPUT TYPE='image' src='images/motorclothes/" . $part['Cat1'] . "/" . $part['Cat2'] . "/" . $part['Style'] . ".gif' BORDER=0 ALT='" . $part['Style'] . "'>
</form><br>
";
}
}
echo " 	</td>
</tr>
</table>";
?>

[/pre]

That is my point. Your code currently gets everything, whether it has a picture or not and goes straight into printing the number of pages. What you need to do is, before printing the number of pages, check if the file exists. if it does, make a couter increment and at the end print the number of pages as that counter divided by how ever many pictures you want on a page.

well yeah i get thats what needs to happen, i just dont know how to do it =)  hahaha thats why im here.  ok say i add the file exists BEFORE it counts the pages.  here is what it would look like.  However, i don't know how to code a counter increment if  $picexists=yes.  I know its probably just 1 line, but I don't know how.  I dont know what to put AFTER the FILE EXISTS and BEFORE THE COUNT...

[pre]

$pagelimit = "5";
$strSQL = mysql_query("SELECT * FROM `mczippo1` WHERE Cat2 ='zippo lighters' AND OnHand > MinQty GROUP BY Style ORDER BY OnHand Desc");
if ((file_exists("images/motorclothes/" . $strSQL['Cat1'] . "/" . $strSQL['Cat2'] . "/" . $strSQL['Style'] . ".jpg")) AND (file_exists("images/motorclothes/" . $strSQL['Cat1'] . "/" . $strSQL['Cat2'] . "/" . $strSQL['Style'] . ".gif")))
{
$picexists = 'yes';
}

$totalrows = mysql_num_rows($strSQL);
$pagenums = ceil ($totalrows/$pagelimit);

[/pre]

 

 

If you replace this line:

$totalrows = mysql_num_rows($strSQL);

 

with this:

while ($part = mysql_fetch_array($strSQL))
	{
		if ((file_exists("images/motorclothes/" . $part['Cat1'] . "/" . $part['Cat2'] . "/" . $part['Style'] . ".jpg")) AND (file_exists("images/motorclothes/" . $part['Cat1'] . "/" . $part['Cat2'] . "/" . $part['Style'] . ".gif")))
			$totalrows++;
	}

(Which is basically copied from lower in your code)

The total rows should be the number of pictures availabe.

HAH! YES! it worked hahah.  i had to adapt it to my other page where it shows up a little different, compared to the test page, just change some variables but it works.  Man you r the shiznit.  i knew there had to be a way.  THANKS!!!!! 

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.