473,325 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Algorithm isn't working

Hey guys,

I seem to be having a problem with part of my code. This code is
supposed to connect to a database and perform a query to get all the
name fields in all the records. The program then compares the results
to the file names of all the files in a certain directory. I'm doing
this so I will know if another record needs to be added.
Unfortunately, the algorithm I am using does not work. I can't seem to
find what is wrong with it. Instead of the giving me the next file
name that needs to be added, it gives me the last file name that I have
already added.

Here is the code:
<?php
$files = scandir("c:\apache2triad\htdocs\pictures");
$numberElements = count($files);

// Connect to the server and select the database
@mysql_connect("localhost", "user", "password")
or die("Could not connect to MySQL server!");
@mysql_select_db("website") or die("Could not select database!");

// process query
$query = "SELECT name FROM pictures WHERE 1";
$result = mysql_query($query);

if(!$result) echo "Error processing query!";

// find number of rows returned in the result of query
$numberRows = mysql_numrows($result);

// compare query results with file names
for( $rowNumber = $numberRows; $rowNumber > 0; $rowNumber-- )
{
$exists = false;
$name = mysql_result( $result, $rowNumber - 1, name );
for( $elementNumber = $numberElements; $elementNumber > 0;
$elementNumber-- )
{
if( $files[elementNumber - 1] == $name )
$exists = true;
}
if( $exists == false )
{
echo "File $name needs to be added to the database!";
break;
}
}

mysql_close();
?>

Can you help me?
Thanks!

Jul 17 '05 #1
2 1376

On 10-Jul-2005, my****@gmail.com wrote:
I seem to be having a problem with part of my code. This code is
supposed to connect to a database and perform a query to get all the
name fields in all the records. The program then compares the results
to the file names of all the files in a certain directory. I'm doing
this so I will know if another record needs to be added.
Unfortunately, the algorithm I am using does not work. I can't seem to
find what is wrong with it. Instead of the giving me the next file
name that needs to be added, it gives me the last file name that I have
already added.

Here is the code:
<?php
$files = scandir("c:\apache2triad\htdocs\pictures");
$numberElements = count($files);

// Connect to the server and select the database
@mysql_connect("localhost", "user", "password")
or die("Could not connect to MySQL server!");
@mysql_select_db("website") or die("Could not select database!");

// process query
$query = "SELECT name FROM pictures WHERE 1";
$result = mysql_query($query);

if(!$result) echo "Error processing query!";

// find number of rows returned in the result of query
$numberRows = mysql_numrows($result);

// compare query results with file names
for( $rowNumber = $numberRows; $rowNumber > 0; $rowNumber-- )
{
$exists = false;
$name = mysql_result( $result, $rowNumber - 1, name );
for( $elementNumber = $numberElements; $elementNumber > 0;
$elementNumber-- )
{
if( $files[elementNumber - 1] == $name )
$exists = true;
}
if( $exists == false )
{
echo "File $name needs to be added to the database!";
break;
}
}

mysql_close();
?>


The break needs to be in the if block inside the inner for loop. There may
be more problems, but that's what jumped out at me.

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #2

<my****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hey guys,

I seem to be having a problem with part of my code. This code is
supposed to connect to a database and perform a query to get all the
name fields in all the records. The program then compares the results
to the file names of all the files in a certain directory. I'm doing
this so I will know if another record needs to be added.
Unfortunately, the algorithm I am using does not work. I can't seem to
find what is wrong with it. Instead of the giving me the next file
name that needs to be added, it gives me the last file name that I have
already added.

Here is the code:
<?php
$files = scandir("c:\apache2triad\htdocs\pictures");
$numberElements = count($files);

// Connect to the server and select the database
@mysql_connect("localhost", "user", "password")
or die("Could not connect to MySQL server!");
@mysql_select_db("website") or die("Could not select database!");

// process query
$query = "SELECT name FROM pictures WHERE 1";
$result = mysql_query($query);

if(!$result) echo "Error processing query!";

// find number of rows returned in the result of query
$numberRows = mysql_numrows($result);

// compare query results with file names
for( $rowNumber = $numberRows; $rowNumber > 0; $rowNumber-- )
{
$exists = false;
$name = mysql_result( $result, $rowNumber - 1, name );
for( $elementNumber = $numberElements; $elementNumber > 0;
$elementNumber-- )
{
if( $files[elementNumber - 1] == $name )
$exists = true;
}
if( $exists == false )
{
echo "File $name needs to be added to the database!";
break;
}
}

mysql_close();
?>

Can you help me?
Thanks!


What makes you so certain that the order in the $files array corresponds
one-to-one with the result of the query? Also, you say that you want to add
a record (to the DB) if the file is not there. Yet, you are only searching
over the records in the DB and not over the files you have from scandir. It
seems the opposite of what you want.

I think you should first query the database as you do and build an array of
names from there. Then you should loop over the files in the array you
obtain from scandir. For each loop you should check for its presence in the
array from the query. If it is not there, then add it.

Shelly
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
28
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
7
by: Wei | last post by:
Hi all, I found out I can use the max function which is defined in STL <algorithmwithout including this header in my program. The compilers I used are GNU g++ 3.4.4 and Visual Studio C++ 2005. ...
6
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my...
2
by: Bruce | last post by:
Hi I am trying to come up with an algorithm to solve the following problem. There are x components and y machines (y x) one machine can only run one component. Each component has 2 requirements...
1
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.