Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 08-12-2004, 05:37 PM   #1 (permalink)
easilyi
Registered User
 
Join Date: Aug 2004
Location: USA - West Coast
Posts: 9
easilyi is on a distinguished road
Question Problem retrieving subset of records

Hello.
Sorry this is long, but I am stuck. I understand if it is too much for anybody to deal with, but I need to try...
I am trying to display a subset of values (in this case, image names) from my table and a strange thing is happening.

All my image names have a prefix.
For example: Default_Image1.jpg, Default_Image2.jpg, Misc_Image1.jpg, Misc_Image2.jpg.
The prefix represents the gallery name for that group of pictures. So with these pics I have a "Default" gallery and a "Misc" gallery.
I have code to retrieve all pictures in a certain gallery.
The gallery (image prefix) is passed in a variable.
However, when I have multiple galleries (prefixes) and I try to list everything from one, the galleries that come first in the alphabet take precedence over the others.

Here is what I mean:
I have 5 pictures with the prefix "Default" and 5 pictures with the prefix "Extra".
All the the "Default" pictures will display when i pass the "Default" value in my gallery variable, but none of the "Extra" pictures will display if I pass the "Extra" value in my gallery variable.
Now, if I add a picture with the prefix "Booya", only 4 of the "Default" pictures will now display because the 1 "Booya" picture is in there...
whatever gallery has the earliest letter in the alphabet will show all of the picture names.

I am confused.

Here is the code I have:
PHP Code:
<?php

  $dir
="./";
  if (
is_dir($dir))
  {
    
$fd = @opendir($dir);
    if(
$fd)
    {
      while ((
$part = @readdir($fd)) == true)
      {
        if (
$part != "." && $part != "..")
        {
          
$file_array[]=$part;
        }
      }
    }
    
sort($file_array);
    
reset($file_array);
    for(
$i=0;$i<count($file_array);$i++)
    {
      
$npart=$file_array[$i];
      if (!
strstr($npart,".inc.php") && !strstr($npart,"index.php"))
      {
        
$fsize=filesize($npart)/1000;
        if (
is_dir($npart))
        {

        }
        else
        {

          
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
          //  This section of code added to original code in order to find prefix on image names          //
          //  and use only the prefix for the gallery chosen, creating an array of images with the        //
          //  chosen prefix.                                                                              //
          //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

          
          
$mystring $npart;
          
$findme  $gallery;
              
$count strlen($findme);            // get length of gallery prefix for images
          
$rest substr($mystring0$count); // get string from image name the length of gallery prefix
          
$pos strpos($rest$findme);        // compare string to see if it matches gallery prefix
              
if ($pos === false)                   //if it does not match, do nothing
          
{

          }
          else                                
//if it does match, add image to currentgallery array
          
{
            
$currentgallery[$i] = $mystring;
                  }
                }
          }
        }



    print (
"<small><font color=\"008B45\" face=\"verdana\"><b>Pictures in gallery</b> <i>$gallery</i>:</font></small><br /><br />");

    
$db mysql_connect("localhost""user""pass");
    if (!
$db)
    {
      die(
'Could not connect: ' mysql_error());
    }
    
mysql_select_db("wordpress",$db);
    
$result mysql_query("SELECT * FROM wp_piccaption",$db);
    if (!
$result)
    {
      die(
'Query failed: ' mysql_error());
    }

    for(
$i=0;$i<count($currentgallery);$i++)
    {
      
$result1 mysql_query("SELECT id FROM wp_piccaption WHERE picture = '$currentgallery[$i]'") or die(mysql_error());
      list(
$id) = mysql_fetch_row($result1);
      
$result2 mysql_query("SELECT caption FROM wp_piccaption WHERE picture = '$currentgallery[$i]'") or die(mysql_error());
      list(
$caption) = mysql_fetch_row($result2);

        print (
"<small><a href=\"$currentgallery[$i]\"><img src=\"$currentgallery[$i]\" width=\"50\" height=\"50\"></a>
        $currentgallery[$i]&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"managepicsedit.php?picture=$currentgallery[$i]&caption=$caption&id=$id&gallery=$gallery\"><b>(</b>add/edit caption<b>)</b></a> <a href=\"managepicsedit.php?&deletecap=yes&id=$id&gallery=$gallery\"><b>(</b>delete caption<b>)</b></a></small><br /> "
);
      print (
"<small><b>Caption:</b> $caption</small><br /><br />");

    }


  
mysql_free_result($result);





  }

?>
easilyi is offline   Reply With Quote
Old 08-12-2004, 10:37 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Sorry I havn't got the time or the patience to realy understand the code..
But one thing disturbe my mind..
PHP Code:
...
  for(
$i=0;$i<count($file_array);$i++) 
    { 
      ....
      if (
$pos === false)   //if it does not match, do nothing 
      


      } 
      else 
//if it does match, add image to currentgallery array
      

        
$currentgallery[$i] = $mystring
      } 
    } 
Now my logic tells me, the $i is counting in the orriginal file array, but if you were to find one that dosn't match, then $i will point to the following position than intended in your $currentgalery[] on the next run through.
This will create an array with alot of holes in it..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-13-2004, 05:56 AM   #3 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Yes, it would seem you only want to populate the $currentgallery array with the name.

edit: ^ I didn't word that very well. Basically you don't want to assign the found filenames in $currentgallery with an array key of $i (the current counter) because, like redhead said, you'll have an array full of holes.

When you loop through $currentgallery you may perform 5 loops, but the array keys may not necessary be 0,1,2,3,4.

So you can fix the array assignment, OR just use a foreach loop.

eg:

PHP Code:
foreach ($currentgallery as $k => $v) {
   
$result1 mysql_query("SELECT id FROM wp_piccaption WHERE picture = '$v'") or die(mysql_error()); 

idx is offline   Reply With Quote
Old 08-13-2004, 06:01 AM   #4 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Also, try using a regex. It should be easier to match the filename.

eg:

PHP Code:

if (preg_match("/^$gallery/"$mystring)) {
   
$currentgallery[] = $mystring;

idx is offline   Reply With Quote
Old 08-13-2004, 09:50 AM   #5 (permalink)
easilyi
Registered User
 
Join Date: Aug 2004
Location: USA - West Coast
Posts: 9
easilyi is on a distinguished road
oh my gosh, thanks so much for the help!

i just read this and have not had a chance to go in and try it yet, but i will as soon as i can.

i knew it was a lot to ask to look at all that code, so thanks again for taking the time to give it a look.

since i am so new, once my code gets too long i start losing focus and troubleshooting gets more and more difficult.

i will work on that array loop as soon as i can and let you know



easilyi is offline   Reply With Quote
Old 08-13-2004, 12:27 PM   #6 (permalink)
easilyi
Registered User
 
Join Date: Aug 2004
Location: USA - West Coast
Posts: 9
easilyi is on a distinguished road
woo hoo!

it seems to be working fine now.
i went with the code:
PHP Code:
if (preg_match("/^$gallery/"$mystring)) {
   
$currentgallery[] = $mystring;

it was more clear to me, the other one confused me a bit

anyway, your explanations of the problem made perfect sense to me after i looked at the code again. i had tunnel vision in my troubleshooting and kept looking at my sql statements, assuming my select statements were wrong.

Thanks! you are both like cyberheroes for me...ok, that is too dorky, but really, thanks for the time and help.
easilyi is offline   Reply With Quote
Old 08-14-2004, 07:35 AM   #7 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Good to hear it worked for you!
idx is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help debugging a power problem Belisarius Lounge 0 10-25-2003 05:44 PM
This is a windows/C problem UnderWing Standard C, C++ 6 03-28-2003 07:17 AM


All times are GMT -8. The time now is 06:52 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting