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
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]
Link to comment
Share on other sites

[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
Share on other sites

[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
Share on other sites

[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.
Link to comment
Share on other sites

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
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.