tgavin Posted January 25, 2007 Share Posted January 25, 2007 I'm trying to count how many times there's an insert and then echo the result.[code=php:0]foreach($_POST['foobar'] as $bar) { $query = "INSERT INTO table (foo) VALUES ('$bar')"; $sql = mysql_query($query,$conn) or die(mysql_error());}echo 'foo was inserted $i times!';[/code] Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/ Share on other sites More sharing options...
The Little Guy Posted January 25, 2007 Share Posted January 25, 2007 $i = 0;foreach($_POST['foobar'] as $bar) {$query = "INSERT INTO table (foo) VALUES ('$bar')";$sql = mysql_query($query,$conn) or die(mysql_error());$i++;}echo 'foo was inserted'. $i.' times!'; Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169255 Share on other sites More sharing options...
tgavin Posted January 25, 2007 Author Share Posted January 25, 2007 I've tried that and keep getting 1 as a result. Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169259 Share on other sites More sharing options...
boo_lolly Posted January 25, 2007 Share Posted January 25, 2007 [quote author=tgavin link=topic=124038.msg513488#msg513488 date=1169758185]I've tried that and keep getting 1 as a result.[/quote]is there only one update? Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169262 Share on other sites More sharing options...
tgavin Posted January 25, 2007 Author Share Posted January 25, 2007 [quote author=boo_lolly link=topic=124038.msg513491#msg513491 date=1169758405]is there only one update?[/quote]There shouldn't be. If I have nine items to insert, then I get 9 records in the db.Here's everything I have, maybe somebody can spot the problem.[code=php:0]// begin building the INSERT query for the subscribers_temp table$query = "INSERT INTO {$database}.subscribers_temp(email) VALUES ";foreach($emails as $email) { $query = $query . " ('$email'),";}$insert_query = rtrim($query,',');// create temp table for inserting new email addresses$query = "CREATE TEMPORARY TABLE {$database}.subscribers_temp(email VARCHAR(60)) TYPE=HEAP";$sql = mysql_query($query,$conn) or die(mysql_error());// insert new email addresses into subscribers_temp table$sql = mysql_query($insert_query,$conn) or die(mysql_error());// get email addresses from subscribers_temp table and insert into subscribers table$query = "INSERT IGNORE {$database}.subscribers(id,email,added_by,subscribed,bounced,bounce_count,bounced_id,ref_url,date_added,activated) SELECT '', email, 'ad', '1', '0', '0', '', '', now(), '1' FROM {$database}.subscribers_temp";$sql = mysql_query($query,$conn) or die(mysql_error());// add to mailing list if neededif(isset($_POST['ls_list_id'])) { foreach($_POST['ls_list_id'] as $ls_list_id) { $query = "INSERT IGNORE {$database}.list_subscribers(ls_list_id,ls_sub_id) SELECT {$ls_list_id}, {$database}.subscribers.id FROM {$database}.subscribers INNER JOIN {$database}.subscribers_temp ON {$database}.subscribers.email = {$database}.subscribers_temp.email"; $sql = mysql_query($query,$conn) or die(mysql_error()); }}[/code] Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169266 Share on other sites More sharing options...
boo_lolly Posted January 25, 2007 Share Posted January 25, 2007 how many entries are being inserted into the database? Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169362 Share on other sites More sharing options...
tgavin Posted January 26, 2007 Author Share Posted January 26, 2007 [quote author=boo_lolly link=topic=124038.msg513593#msg513593 date=1169765649]how many entries are being inserted into the database?[/quote]All of them. If I have ten email addresses to insert, then I'll have ten records each, in the subscribers and list_subscribers tables. Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169406 Share on other sites More sharing options...
Jessica Posted January 26, 2007 Share Posted January 26, 2007 [quote author=tgavin link=topic=124038.msg513637#msg513637 date=1169769855][quote author=boo_lolly link=topic=124038.msg513593#msg513593 date=1169765649]how many entries are being inserted into the database?[/quote]All of them. If I have ten email addresses to insert, then I'll have ten records each, in the subscribers and list_subscribers tables.[/quote]I think boo meant THIS time. When you're trying it and only getting 1. Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169409 Share on other sites More sharing options...
tgavin Posted January 26, 2007 Author Share Posted January 26, 2007 All of themIt's always inserting every single address I feed it. From 1 to 10,000. Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169410 Share on other sites More sharing options...
boo_lolly Posted January 26, 2007 Share Posted January 26, 2007 [quote author=jesirose link=topic=124038.msg513640#msg513640 date=1169770196][quote author=tgavin link=topic=124038.msg513637#msg513637 date=1169769855][quote author=boo_lolly link=topic=124038.msg513593#msg513593 date=1169765649]how many entries are being inserted into the database?[/quote]All of them. If I have ten email addresses to insert, then I'll have ten records each, in the subscribers and list_subscribers tables.[/quote]I think boo meant THIS time. When you're trying it and only getting 1.[/quote]i love it when you call me boo... hahahaha jk ;Dwhere is this page being called from? you have $_POST but no form, so you can't be posting all of your code. let's have a look see. Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169465 Share on other sites More sharing options...
JasonLewis Posted January 26, 2007 Share Posted January 26, 2007 just add an echo in the foreach that echos out i.[code=php:0]$i = 0;foreach($_POST['ls_list_id'] as $ls_list_id) {$query = "INSERT IGNORE {$database}.list_subscribers(ls_list_id,ls_sub_id) SELECT {$ls_list_id}, {$database}.subscribers.id FROM {$database}.subscribers INNER JOIN {$database}.subscribers_temp ON {$database}.subscribers.email = {$database}.subscribers_temp.email";$sql = mysql_query($query,$conn) or die(mysql_error());echo $i."<br>";$i++;}[/code]then just see if i is incrementing or not. Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169471 Share on other sites More sharing options...
tgavin Posted January 26, 2007 Author Share Posted January 26, 2007 It's incrementing. On the form there are 2 check boxes. If I only have one ticked, I get 0 echoed. However, if I select both check boxes, then 01is echoed. It's working as it should. I just never ticked both boxes! (dumbass!).So, it's actually the $query that's doing the inserting of the number of records. The foreach seems to be there to split the array. I had this feeling this is how it was going to be, but was hoping I could get another way to count. In actuality, I already have the count and just didn't see it. I had to put you guys through this to actually see it.This is what happens when you get 4 hours of sleep because you're balls-deep (eyeballs, that is) in php. Ugh. I apologize for wasting everyone's time. Link to comment https://forums.phpfreaks.com/topic/35715-solved-loop-number-of-items-in-an-array/#findComment-169491 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.