Jump to content

[SOLVED] loop number of items in an array


tgavin

Recommended Posts

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

[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 needed
if(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]
[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 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 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  ;D

where 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.
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.
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
0
1
is 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.