PHP array question

Status
Not open for further replies.

threehundred

New member
Aug 22, 2006
469
9
0
I have a form with a multiline textarea. I'm trying to post this form to the same page, and grab each line and insert it into a MySQL db. I know how to do most of it, but i'm having problem with this part. There are notes and questions in the following code. Thanks in advance.


Code:
<?php

//open db connection
include '../includes/config.php';
include '../includes/opendb.php';

if(isset($_POST['leads'])){

////I don't Know What to do here????
////Each line of textarea input would be like:
////  1234  10.00
////  1235  10.00
////  1237  12.00
////I want to have each line of the array be broke into $leadid and $price.
////Then UPDATE like "UPDATE clicks SET leadprice = $price WHERE id = $leadid"
////How do I do this? I think the two variables are tab delimited on each line of the array


} else {

?>
<FORM action="form.php" method="POST">
<LABEL>Enter Lead IDs below:</LABEL><br>
<textarea multiple name="leads" cols="60" rows="30"></textarea><br>
<INPUT type="submit" value="Add Lead IDs to Database">
</FORM>

<?php

}


include '../includes/closedb.php';
?>
 


The first thing you need to do is take the $_POST and break it up line by line.

Something like this might work:

Code:
 $leadPrice = explode('\n', $_POST[leads]);

Now you've got a variable $leadPrice which is an array that contains each line of the post.

What you do now is loop through through each value in the array, and do another explode, then do your sql code:

Code:
 foreach ($leadPrice as $lead) 
{
    $values = explode("  ", $lead);
    $leadID = $values[0];
    $leadAmt = $values[1];
    // SQL Logic Here
 }

I haven't tested this code, I just typed it out, but I hope this helps. If anything you can get the logic going on here and apply it to your situation.
 
That seems like it would work, and I think i've already tried it that way. I tried again, here's my exact code. I just did an echo to see if it I could use the variables during the foreach, but it doesn't output right. Here's the code, and the output it gives.

Code:
<?php

//open db connection
include '../includes/config.php';
include '../includes/opendb.php';

if(isset($_POST['leads'])){

$leadPrice = explode('\n', $_POST[leads]);
 foreach ($leadPrice as $lead) 
{
    $values = explode('\t', $lead);
    $leadID = $values[0];
    $leadprice = $values[1];
    echo "lead ID: " . $leadID . "  -  lead price: " . $leadprice . "<br>";
}
} else {

?>
<FORM action="form.php" method="POST">
<LABEL>Enter Lead IDs below:</LABEL><br>
<textarea multiple name="leads" cols="60" rows="30"></textarea><br>
<INPUT type="submit" value="Add Lead IDs to Database">
</FORM>

<?php

}


include '../includes/closedb.php';
?>


Here's what echoed to the screen:

Code:
lead ID: 369	$12.00  399	$12.00  417	$12.00  3904	$12.00  3948	$12.00  5512	$12.00  5542	$12.00  5783	$12.00  5974	$12.00  5989	$12.00   -  lead price:

Thanks for your help so far.
 
BTW, I think the problem was the single vs. double quotes. At one point I had it working before I posted here, and I believe the problem was caused by changing from double to single. Thanks again!!
 
Status
Not open for further replies.