Code not continuing through loop

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 583 Views - Last Post: 18 April 2019 - 08:35 AM Rate Topic: -----

#1 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Code not continuing through loop

Posted 15 April 2019 - 03:34 PM

Hello,
I am fairly new to php. I have written the below code, but when I run it, I am not getting any of the field checks when it hits my foreach loop. My compiler has ran through any syntax error that occur, but I am at a loss. Can anyone point me in the right direction? I am running a wamp server on windows 7, local only.

<?php

//defining function
function splitOne($V){

	//split array into smaller arrays
	array_chunk($data, 1);
	print_r("Chunked Out");

	//map thumb part of array
	$thumb = array_map(str_getcsv($data[5], ";"));
	print_r("Mapped to Thumb");

	//map keyword part of array
	$keyword = array_map(str_getcsv($data[6], ";"));
	print_r("Mapped to Keyword");

	//remove last two fields of data
	array_pop($data);
	array_pop($data);

	//merge arrays 
	$v = array_merge($data, $thumb, $keyword);

	//return new array from function
	return $v;

}

//Connecting to database
$mysqli = new mysqli("localhost", "root", "GreenSun10", "studio");

//field Check
print_r("Connection Established");

//Scanning Directory
$dollar = glob("C:\Users\user\Desktop\CVS\XH\*.CSV");

//field Check
print_r("Data Collected");

foreach($dollar as $value) {
	while (($data = fgetcsv($dollar, 1002, "|")) !== FALSE){

		//remove first field
		$fruit = array_shift($data);
		print_r("Shifted");

		//remove last string
		array_pop($data);
		print_r("Popped");

		//remove last string again
		array_pop($data);
		print_r("Popped");

		//remove last string a third time
		array_pop($data);
		print_r("Popped");

		//mapping array into function to manipulate
		array_map(splitOne, $data);

		
		$query = "INSERT INTO Quarantine (Url, EmbedUrl, Title, Duration, DateAdded, Thumb1, Thumb2, Thumb3, Thumb4, Thumb5, Thumb6, Thumb7, Thumb8, Thumb9, Thumb10, Keyword1, Keyword2, Keyword3, Keyword4, Keyword5, Keyword6, Keyword7, Keyword8, Keyword9, Keyword10) VALUES($V)";

		mysqli_query($conn, $query);
		//Field Check
		print_r("Data Entered Into Quarantine");
	}
}

?>


This post has been edited by JackOfAllTrades: 15 April 2019 - 03:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Code not continuing through loop

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5404
  • View blog
  • Posts: 14,305
  • Joined: 18-April 07

Re: Code not continuing through loop

Posted 15 April 2019 - 03:43 PM

First of all, have you confirmed that you have values appearing in your $dollar variable? Next, do realize that the first parameter to fgetcsv is a resource handle (in other words an opened file) and not an array like $dollar is.

So take a look at those two things and report back what you find out. :)
Was This Post Helpful? 0
  • +
  • -

#3 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Re: Code not continuing through loop

Posted 15 April 2019 - 04:18 PM

OK, so first, I got the following when I echoed out $dollar:


Connection Established
( ! ) Notice: Array to string conversion in C:\wamp\www\XH.php on line 38
Call Stack
# Time Memory Function Location
1 0.0000 386352 {main}( ) ...\XH.php:0
ArrayData Collected

And second, I realize that the fgetcsv is an open document, but I thought that the while loop was supposed to read it line by line and pass it to the $data value as an array? Or did I misinterpret that?

Thank you!
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2287
  • View blog
  • Posts: 6,978
  • Joined: 15-January 14

Re: Code not continuing through loop

Posted 15 April 2019 - 05:25 PM

You're trying to use a variable in the function called $data that isn't defined. The function takes a variable called $V but you don't use $V in the function. And inside that function you're not using array_map correctly, it needs at least 2 parameters. You're only passing 1 to it. Also, inside your foreach loop, you never use $value.
Was This Post Helpful? 1
  • +
  • -

#5 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5404
  • View blog
  • Posts: 14,305
  • Joined: 18-April 07

Re: Code not continuing through loop

Posted 16 April 2019 - 03:06 PM

View PostPrawnja, on 15 April 2019 - 04:18 PM, said:

And second, I realize that the fgetcsv is an open document, but I thought that the while loop was supposed to read it line by line and pass it to the $data value as an array? Or did I misinterpret that?

Thank you!


But you are passing $dollar, which is an array to the fgetcsv function. Nevermind the $data part, you are not specifying the right parameter to the function so that it can read the line and give it to $data. fgetcsv is expecting to be reading from an opened file. If you given it an open csv file handle then it can read it line by line and give it to $data as you are intending it to do.

Then of course ArtificialSoldier is on the money with the other problem. :)
Was This Post Helpful? 0
  • +
  • -

#6 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Re: Code not continuing through loop

Posted 16 April 2019 - 03:53 PM

Thank you all!!!!

So, Artificial soldier, I see where I made my mistake with the function in naming $V in the beginning. The tutorials I read made me think that the variable in () at the end of the function was what it passed back, not what it took. So, with that in mind, if I put the $data in there, for arguments sake as it will probably change the further this conversation goes lol, would the values of $data at the time be passed into the function? And as for the array_map in the function, would you say I should get rid of it and just use the str_getcsv for the $data[5] and $data[6] variables? And for the final point you made about $value, how would I use it? The tutorial I looked at (w3schools) did not give any info on the $value as to how to use it.

Martyr2, thank you for pointing that out to me! How would I pass the CSV file to the fgetcsv function opened? It is reading the first file from the glob function and then obviously I will need to close it, but how do I do this without typing out each individual name?
Was This Post Helpful? 0
  • +
  • -

#7 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5404
  • View blog
  • Posts: 14,305
  • Joined: 18-April 07

Re: Code not continuing through loop

Posted 16 April 2019 - 03:55 PM

glob is going to give you an array of filenames. Just give each filename from that array to fopen to get a handle.

https://www.php.net/...ction.fopen.php

That handle is what you give to fgetcsv. When you are done with it, fclose() is your friend. :D


Edit: Take a look at the page for fgetcsv. It has an example showing the use of fopen to open a file. Notice how they open it and set it to $handle, then they pass $handle to fgetcsv.

https://www.php.net/...ion.fgetcsv.php

This post has been edited by Martyr2: 16 April 2019 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#8 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Re: Code not continuing through loop

Posted 16 April 2019 - 04:09 PM

So, I want to make sure I understand this correctly from what I just read Martyr2, I would pass the array to fopen like this?
[Code]
$doc = fopen("$dollar", "r"):array;
[\Code]
And then pass the $doc to the fgetcsv instead of $dollar. And I would put it inside the foreach loop, but above the while loop?
Was This Post Helpful? 0
  • +
  • -

#9 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5404
  • View blog
  • Posts: 14,305
  • Joined: 18-April 07

Re: Code not continuing through loop

Posted 16 April 2019 - 04:25 PM

No glob is going to give you an ARRAY of file types. So you have to access the array values...

//Scanning Directory
$dollar = glob("C:\Users\user\Desktop\CVS\XH\*.CSV");


foreach($dollar as $value) {
   $doc = fopen($value, "r"); //<-- $value is your filename string from glob

   while (($data = fgetcsv($doc, 1002, "|")) !== FALSE){  //<-- Now we pass that $doc handle to fgetcsv
       // Do stuff with lines in $data
   }
   
   fclose($doc); //<-- Close this file handle, now go to foreach to read next file
}


Was This Post Helpful? 0
  • +
  • -

#10 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Re: Code not continuing through loop

Posted 16 April 2019 - 04:43 PM

Thank you so much Martyr2!!!!! That was so much clearer than the tutorial made it!!!
Was This Post Helpful? 0
  • +
  • -

#11 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Re: Code not continuing through loop

Posted 16 April 2019 - 05:34 PM

So, I have made the changes that were discussed, and added a couple of things to the function..... But alas I have still only gotten as far as the first post said. No farther than data collected gets return Ed. No other field checks appear, and no entries into the local database. Here is the updated code:
<?php

//defining function
function splitOne($data){

	//split array into smaller arrays
	array_chunk($data, 1);
	print_r("Chunked Out");

	//map thumb part of array
	array_map(thumbMaker, $data[5]);
	print_r("Mapped to Thumb");

	//map keyword part of array
	$keyword = array_map(keyMaker, $data[6]);
	print_r("Mapped to Keyword");

	//remove last two fields of data
	array_pop($data);
	array_pop($data);

	//merge arrays 
	$v = array_merge($data, $thumb, $keyword);

	//return new array from function
	return $v;

}

function thumbMaker($data){
//make an array of thumbnails
$thumb = str_getcsv($data[5], ";");
return $thumb;

}

function keyMaker($data){
//making an array of keywords
 $keyword = str_getcsv($data[6], ";");
return $keyword;

}

//Connecting to database
$mysqli = new mysqli("localhost", "root", "GreenSun10", "studio");

//field Check
print_r("Connection Established");

//Scanning Directory
$dollar = glob("C:\Users\user\Desktop\CSV\XH\*.CSV");

//field Check
print_r("Data Collected");

foreach($dollar as $value) {

	//opening $dollar field in array
	$doc = fopen($value, "r");
	while (($data = fgetcsv($doc, 1002, "|")) !== FALSE){

		//remove first field
		$fruit = array_shift($data);
		print_r("Shifted");

		//remove last string
		array_pop($data);
		print_r("Popped");

		//remove last string again
		array_pop($data);
		print_r("Popped");

		//remove last string a third time
		array_pop($data);
		print_r("Popped");

		//mapping array into function to manipulate
		array_map(splitOne, $data);

		
		$query = "INSERT INTO Quarantine (Url, EmbedUrl, Title, Duration, DateAdded, Thumb1, Thumb2, Thumb3, Thumb4, Thumb5, Thumb6, Thumb7, Thumb8, Thumb9, Thumb10, Keyword1, Keyword2, Keyword3, Keyword4, Keyword5, Keyword6, Keyword7, Keyword8, Keyword9, Keyword10) VALUES($V)";

		mysqli_query($conn, $query);
		//Field Check
		print_r("Data Entered Into Quarantine");
	}

	//closing dollar variable
	fclose("$doc");

}

?>



I can't see where the code is going wrong. can anyone point me in the right direction?

This post has been edited by JackOfAllTrades: 16 April 2019 - 05:38 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#12 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2287
  • View blog
  • Posts: 6,978
  • Joined: 15-January 14

Re: Code not continuing through loop

Posted 17 April 2019 - 10:53 AM

Quote

So, with that in mind, if I put the $data in there, for arguments sake as it will probably change the further this conversation goes lol, would the values of $data at the time be passed into the function?

No. The value of the parameter is whatever you pass to the function when you call it. The name is not important. The name does not link it up with some other variable somewhere else on the page. The name is just what you call the variable inside the function.

function add($one, $two) {
  return $one + $two;
}

add(10, 20);

$x = 50;
$y = -8;
add($x, $y);

The variable names $one and $two are only used inside the function. They do not link to any other variable names. It's just a name for inside the function. You can call it by directly passing it values that it will set as $one and $two, or you can call it with variables that are set to values, and those values get set as $one and $two.

In your case, you're sending splitOne to array_map, so look at what array_map does:

https://www.php.net/...n.array-map.php

It takes a function and one or more arrays, and it will call the function and pass it each item in the array. But you're not saving the return value of array_map, so nothing changes when you call it. If you want to use the values returned from splitOne then you need to save the return value from array_map. Look at the examples on the array_map documentation page. The first example shows that it returns an array of cubes. You're not saving the return value. If you think that using "return $v" and then later trying to use $V in your SQL query is going to magically do something, it's not. $v only has a value inside the splitOne function, and it's the value that gets returned. Also, in PHP variables are case-sensitive. Even if there was some magical link between the variables, $v and $V are not the same variable.

Instead of using print_r to just print status messages, use it to print $data so you can see how the array is changing and what your code is actually doing.

array_chunk also returns a value, you're not saving that return value either.
Was This Post Helpful? 0
  • +
  • -

#13 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Re: Code not continuing through loop

Posted 17 April 2019 - 02:45 PM

Thank you Artificial Soldier!!!

I fixed everything that you clarified for me, and I ran the code. The code stops after it tries to print the array to the screen to see it.
It passes the first check of connecting to the database, then the second for scanning the directory. Then I get a notice for the next line that says NOTICE: Array to string conversion in "filepathtoprogram" on line 77.
Line 77 is where I am attempting to print the array to the screen. I used print_r to call it. After the notice, it says Array, like it is trying to print the array, but there is nothing after it.
So, I am guessing there is nothing in the array. I know there is at least 1 file in the directory the glob is pointed at. So this baffles my brain as to why it is coming back empty. If anyone needs it, here is the updated code.

<?php

//defining function
function splitOne($chunker, $thumper, $keyser){

	//split array into smaller arrays
	$chunker = array_chunk($data, 1);
	print_r("Chunked Out");
	print_r("$chunker");

	//map thumb part of array
	$thumper = array_map(thumbMaker, $chunker);
	print_r("Mapped to Thumb");
	print_r("$thumper");

	//map keyword part of array
	$keyser = array_map(keyMaker, $chunker);
	print_r("Mapped to Keyword");
	print_r("$keyser");

	//remove last two fields of data
	array_pop($chunker);
	print_r("$chunker");

	array_pop($chunker);
	print_r("$chunker");

	//merge arrays 
	$pusher = array_merge($chunker, $thumper, $keyser);

	//return new array from function
	print_r("$pusher");
	return $pusher;

}

function thumbMaker($thumb){
//make an array of thumbnails
$thumb = str_getcsv($chunker[5], ";");

print_r("$thumb");
return $thumb;

}

function keyMaker($keyword){
//making an array of keywords
 $keyword = str_getcsv($chunker[6], ";");

 print_r("$keyword");
return $keyword;

}

//Connecting to database
$servername = "localhost";
$username = "root";
$password = "GreenSun10";
$databasename = "studio";

$conn = mysqli_connect($servername, $username, $password, $databasename);

//Check Connection
if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

}

echo "Connected Successfully";

//Scanning Directory
$dollar = glob("C:\Users\user\Desktop\CSV\XH\*.CSV");

//field Check
print_r("Data Collected");
print_r("$dollar");

foreach($dollar as $value) {

	//opening $dollar field in array
	$doc = fopen($value, "r");
	while (($data = fgetcsv($doc, 1002, "|")) !== FALSE){

		//remove first field
		$fruit = array_shift($data);
		print_r("Shifted");
		print_r("$data");

		//remove last string
		array_pop($data);
		print_r("Popped");
		print ("$data");

		//remove last string again
		array_pop($data);
		print_r("Popped");
		print_r("$data");

		//remove last string a third time
		array_pop($data);
		print_r("Popped");
		print_r("$data");

		//mapping array into function to manipulate
		$pusher = array_map(splitOne, $data);
		print_r("$pusher");

		
		$query = "INSERT INTO Quarantine (Url, EmbedUrl, Title, Duration, DateAdded, Thumb1, Thumb2, Thumb3, Thumb4, Thumb5, Thumb6, Thumb7, Thumb8, Thumb9, Thumb10, Keyword1, Keyword2, Keyword3, Keyword4, Keyword5, Keyword6, Keyword7, Keyword8, Keyword9, Keyword10) VALUES($pusher)";

		mysqli_query($conn, $query);
		//Field Check
		print_r("Data Entered Into Quarantine");
	}

	//closing dollar variable
	fclose("$doc");

}

//closing connection
mysqli_close($conn);

?>



This post has been edited by JackOfAllTrades: 17 April 2019 - 03:12 PM
Reason for edit:: Fixed code tags again

Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6252
  • View blog
  • Posts: 24,024
  • Joined: 23-August 08

Re: Code not continuing through loop

Posted 17 April 2019 - 03:13 PM

Remove the quotes around $dollar on line 77.
Was This Post Helpful? 0
  • +
  • -

#15 Prawnja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-April 19

Re: Code not continuing through loop

Posted 17 April 2019 - 03:26 PM

The array is still empty after I do, why is it empty?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2