|
First of you don't know (or at least I don't know) how many sets of 6 are in the array. But lets presume one set of 6, so 6 entries.
The loop will only run once, so your statement is true. However inside the loop (before the loop increments) you have that check to append the ','. Since the for loop only increases $i by 6 as it reaches its closing bracket the if statement inside the loop will always be true, don't forget that $i is stil 0 at this point.
|
|
|
|
|
It's not easy to read, but it looks like the problem is a trailing comma in your query. Your loop adds 6 to $i with each iteration, but the test for adding a comma checks if $i + 1 < $c . This should be a 6 instead of a 1.
|
|
|
|
|
I have used used loops to make a multiple insert query like above but form a tad different. I suspect the issue is in the query contruction itself.
Also why are you reassigning the $c variable with every loop of the for? This will slow down your code.
I guess the $headerTable is an single diminsional array? Currious why you did not use a multi-diminsional array...?
It looks like you are skipping 6 rows at a time...so looking at your error message if the values are all of the correct type (they are all varchar or similar?) then it looks like there maybe a second row starting
...
VALUES('SOF','1','1500','19970519','112904','1 '),
...
note the comma on the end there.
also
...
MySQL server version for the right syntax to use near ''
...
I suspect you may have a extra line of data in the array with an empty row sent to your function
Can you post the $query when you echo it? You should be able to copy and past it into the mysql admin tool, phpmyadmin, or other system that will let you run the query inside the tool. although I suspect you will see the same error.
btw I would write this function like this...
<?php
WriteArrayToHeaderDatabase($headerTable,$table);
function WriteArrayToHeaderDatabase($headerTable,$table)
{
$query = "INSERT INTO ".$table."(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES";
$c = count($headerTable);
echo "headerTable Count = ".$c;
for($i = 0; $i < $c; $i += 6)
{
if($i==0)
{
$query.= "('".$headerTable[$i]."','".$headerTable[$i+1]."','".$headerTable[$i+2]."','".$headerTable[$i+3]."','".$headerTable[$i+4]."','".$headerTable[$i+5]."')";
}
else
{
$query.= ", ('".$headerTable[$i]."','".$headerTable[$i+1]."','".$headerTable[$i+2]."','".$headerTable[$i+3]."','".$headerTable[$i+4]."','".$headerTable[$i+5]."')";
}
echo $query;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
return true;
}
?>
this will run faster and is easier to read. It will also tell you how many rows are in the array sent to your variable.
|
|
|
|
|
I get the following error
headerTable Count = 7INSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 ')INSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 '), ('','','','','','')Query failed: Duplicate entry '1' for key 2
Below is the rewritten code(whole code) as per your suggestions
|
|
|
|
|
In the future could you please post your code in the <pre> tag, which would make it more readable for us. But back to your issue. You still have $result = mysql_query($query) or die('Query failed: ' . mysql_error()); line of code inside your loop. Please move it below the for loop and try again.
|
|
|
|
|
I have changed the code to the following and decremented array count by 1 on $c = count($headerTable)-1; and it seems to work
Thanks a lot guys for helping
$query = "INSERT INTO ".$table."(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES";
$c = count($headerTable)-1;
echo "headerTable Count = ".$c;
$numberOfLoops=0;
for($i = 0; $i < $c; $i += 6)
{
if($i==0)
{
$query.= "('".$headerTable[$i]."','".$headerTable[$i+1]."','".$headerTable[$i+2]."','".$headerTable[$i+3]."','".$headerTable[$i+4]."','".$headerTable[$i+5]."')";
}
else
{
$query.= ", ('".$headerTable[$i]."','".$headerTable[$i+1]."','".$headerTable[$i+2]."','".$headerTable[$i+3]."','".$headerTable[$i+4]."','".$headerTable[$i+5]."')";
}
echo $query;
echo 'The number of loops';
echo $numberOfLoops++;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
return true;
echo 'The number of loops';
echo $numberOfLoops++;
}
|
|
|
|
|
Sorry I gave you a bad example as stated above (thanks, Gerben Jongerius for pointing that out).... the mysql_query needs to be outside the loop. To be accurate this is how that should be set up...
$query = "INSERT INTO ".$table."(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES";
$c = count($headerTable)-1;
echo "headerTable Count = ".$c;
$numberOfLoops=0;
for($i = 0; $i < $c; $i += 6)
{
if($i==0)
{
$query.= "('".$headerTable[$i]."','".$headerTable[$i+1]."','".$headerTable[$i+2]."','".$headerTable[$i+3]."','".$headerTable[$i+4]."','".$headerTable[$i+5]."')";
}
else
{
$query.= ", ('".$headerTable[$i]."','".$headerTable[$i+1]."','".$headerTable[$i+2]."','".$headerTable[$i+3]."','".$headerTable[$i+4]."','".$headerTable[$i+5]."')";
}
}
echo $query;
echo 'The number of loops';
echo $numberOfLoops++;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
return true;
}
if you do not move it outside the loop then when you have more than one set of six records to process you will generate a bad query on the second pass.
if example what you currently have one the first pass will produce a query that looks like this on the first pass;
INSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 ')
and on the second pass;
INSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 '), ('','','','','','')
this has 2 errors, one is the duplucate data being entered into the table and the other is the empty data on the second pass.
If you move the mysql_query to outside of the loop you would get one insert like this;
INSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 '), ('','','','','','')
That will take care of the duplcate data issue. To fix the blank data look at your data source. Something is wrong with it adding an extra line as your output shows;
headerTable Count = 7
so one extra row is being added to your data source before it hits this function, this should be looked at.
Hope this helps
Chris J
|
|
|
|
|
hello, i know the cacti is founded by php. i want to build it again by c++.
may be ? or give me some advices,
thank you
|
|
|
|
|
it sounds like you want to convert the php scripts to an executable. Besides doing a lot of coding by hand, I can only think of one application that may help you and it is called "hip hop". It is used by facebook to covert their php scripts to compiled executables to improve the performance. I am not sure how the cacti license is set up so your on you own there.
http://developers.facebook.com/blog/post/358[^]
|
|
|
|
|
when i run:sudo make
the console write ouput :
compiling FrameworkTest.cpp
In file included from /usr/include/c++/4.4/memory:53,
from tests/Fit/FrameworkTest.cpp:15:
/usr/include/c++/4.4/bits/stl_tempbuf.h: In function ‘std::pair<_Tp*, int> std::get_temporary_buffer(ptrdiff_t)’:
/usr/include/c++/4.4/bits/stl_tempbuf.h:95: error: ‘operator new("/usr/include/c++/4.4/bits/stl_tempbuf.h", 94)’ cannot be used as a function
can you tell me why? thank you
|
|
|
|
|
Please Give us stl_tempbuf.h
|
|
|
|
|
Why apache tomcat creates problem in windows 7 while working.its tomcat 6.0.26. i have tried on several systems but on every system having os as windows 7,it creates problem.
Actually ir starts working at that time but when we restart pc then its working stop and we need to reinstall it to work on.
Can anyone elaborate this reason?
|
|
|
|
|
this beacause apache company didnt devlop the version which support windows7 yet
|
|
|
|
|
|
Posted Today, 12:07 AM
Hi All
Thanks in advance for the help, I have the PHP code below to read contents of the file.
//The code does not work, it returns the output below
" outputResource id #3
Quote
Warning: Invalid argument supplied for foreach() in C:\XAMPP\htdocs\kampcoordinators\test.php on line 10"
|
|
|
|
|
Its how you are handling the variable $output.
1st you assign it a file resource returned from the fopen function.
2nd you use a str_replace on said file resource, not sure why it does not barf here.
3rd the foreach loop requires and array as the first value.
... try replacing the fopen() with file_get_contents() as it will return the entire file contents as a string. Then uncomment the explode which will take the string and replace it with an array. That should allow the foreach to work.
always remember to find out what the return type of a function call is before you use it.
|
|
|
|
|
$output should turn into an array after the explode() right? i think something else is wrong.
|
|
|
|
|
the php function defines for reference.
--------- fopen function....
resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )
--------- explode function....
array explode ( string $delimiter , string $string [, int $limit ] )
--------- foreach looping construct....
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
now step through his code....
$output=fopen("testfile.txt","r") or exit("Unable to open file!"); <--- $output is a file resource, this is not the file content.
$output = str_replace(","," ", $output); <--- he takes the $output which is a file resource and does a string replace on it and re-assigns the variable $output as a string.
echo "output"; <--- echos a string called "output"
echo $output; <--- echoes a variable currently a string and most likely just the name of the file resource returned from the fopen call.
foreach($output as $var) <--- in a foreach the first argument MUST be an array!, it is a string right now and so reports an error.
{
echo " ";
echo"display each value as array ";
echo $var;
}
...again more function definitions.
string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen = -1 ]]]] )
if he writes his code like this.....
$output = file_get_contents('path/to/file.txt'); //<--- Reads entire file into a string, so $output is a string and contains the files content.
$output = explode(",",$output); //<-- Split $output by "," and returns an array, $output is an array.
// these are moved outside of foreach loop so they do not repeat with each pass.
echo " ";
echo "display each value as array";
foreach($output as $fragment)
{
echo $fragment."<br>"; // this will print each "fragment" of the variable array $output.
// <br> is for browser output, change to \n for consol output.
}
----------------- BTW
if he wants to read each and every character then his original code will never work unless the contents of his source file contains a comma between each character.
If it is just text then he could just load the file contents using the file_get_contents() then use strlen to get the total character count of the contents and then he can use a for to walk through each character of the file contents. like this....
$output = file_get_contents('path/to/file.txt');
$cnt = strlen($output);
for$i=0;$i<$cnt;$i++)
{
echo $output[$i]." ";
}
|
|
|
|
|
I see, it was a resource indeed. I think he also tried to replace the spaces with a comma but did it the other way around. The comma (if there was any) was replaced by spaces then he used explode with comma as a delimiter.
|
|
|
|
|
it may indeed be what you suggest with the commas, but at any rate even if he did it that way he would end up with words being split and not the characters.
One of the nice things with strings in php is you can treat them as an array and just step through them.
|
|
|
|
|
I think this is what happened. You misused the str_replace() function. If you will check the documentation for it. What you actually did was find an occurrence of "," and replace it with " " and then you searched for "," on your explode(). The character no longer exists by then. Try interchanging the first two arguments of your str_replace(). I think that should do.
|
|
|
|
|
|
How can i develop login and registration system using php???
|
|
|
|
|
Check out this link for some samples and don't multi-post...
Samples[^]
Apart from these links, start from the basics, that will help you a lot...
Cheers,
Karthik
modified on Monday, October 11, 2010 9:13 PM
|
|
|
|
|
Hi i am a very new on this line and i want to stay on this line, I mean to say I want to be a web developer. Okay, Now i want to know how can execute or develop a login and registration system using php code. Any one here who wants to help me????????
|
|
|
|
|