Page 1 of 2

Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 12:34 pm
by reyes99
Hi I have been struggling with this code for a while now and I cant get it to work. I am kind of new to php and I have been trying to figure it out searching the web and trial and error. I am trying to display some records from my db into a table and one of the fields (exchmemberid) I want to be able to input it into an array. When I try to read it with foreach loop I get an error "Warning: Invalid argument supplied for foreach()" I guess it is because I am not setting up the array correctly and it is trying to read an array but it is not finding an array????

I am posing my code to see if someone could please tell me what is wrong.

Thanks in advance..

Code: Select all

 
<?php
require_once ("config.php");
 
$q1 = "SELECT * FROM exchanges ORDER BY exch_id";
$r1 = mysql_query($q1) or die(mysql_error());
 
$siterefid = $_SESSION['siterefid'];
$q2 = "SELECT * FROM users_exchg WHERE site_ref_id = $siterefid ORDER BY exch_id";
$r2 = mysql_query($q2) or die(mysql_error());
 
mysql_close();
 
    $output = "<form method='post' action='saveform.php'>";
    $output .= "<table cellSpacing='0' cellPadding='4' width='1046' border='1' bordercolorlight='#CE5A5A' bordercolordark='#CE5A5A' style='border-collapse: collapse' bordercolor='#111111' align='center'>\n";
    $output .= "<tr>\n";
    $output .= "<td class='cell5' bgcolor='#CE5A5A'>\n";
    $output .= "<font size='2' color='#FFFFFF' face='Arial'><b>Category</b></font></td>\n";
    $output .= "<td class='cell5' bgcolor='#CE5A5A'>\n";
    $output .= "<b><font face='Arial' size='2' color='#FFFFFF'>Company Name</font></b></td>\n";
    $output .= "<td class='cell4' bgcolor='#CE5A5A'>\n";
    $output .= "<font size='2' color='#FFFFFF' face='Arial'><b>Description</b></font></td>\n";
    $output .= "<td class='cell4' align='middle' bgcolor='#CE5A5A'>\n";
    $output .= "<b><font face='Arial' size='2' color='#FFFFFF'>Ref. Link</font></b></td>\n";
    $output .= "<td class='cell4' align='middle' bgcolor='#CE5A5A'>\n";
    $output .= "<b><font face='Arial' size='2' color='#FFFFFF'>Ref. ID</font></b></td>\n";
 
while($a1 = mysql_fetch_array($r1))
{
   $a2 = mysql_fetch_array($r2);
   $exchmemberid = $a2[exch_member_id];
   $a3 = array(exchmemberid);
  
   IF (strlen($a2[exch_member_id] <= 0)) {
     $exchlinkid = $a2[exch_ref_by];
   }
   else {
      $exchlinkid = $a2[exch_member_id];
   }
  
   $refurl = $a1['reflink'] . $exchlinkid;
   $output .= "<tr onmouseover=\"this.bgColor='#FEF2CB';\" onmouseout=\"this.bgColor='#FFFFFF';\" bgColor=\"#ffffff\">\n";
   $output .= "<td><font face='arial' size='2'>" . $a1["category"] . "</font></td>\n";
   $output .= "<td><font face='arial' size='2'>" . $a1['coname']  . "</font></td>\n";
   $output .= "<td><font face='arial' size='2'>" . $a1["description"] . "</font></td>\n";
   $output .= "<td><font face='arial' size='2'>";
   $output .= "<a href='$refurl' TARGET='_blank'>Sign Up Here</a></font></td>\n";
   $output .= "<td><font face='arial' size='2'>";
   $output .= "<input type='text' name='exchmemberid' value='$a3[exchmemberid]'></font></td>\n";
   $output .= "</tr>\n";
   
}
   $output .= "</table>\n";
   $output .= "<input type='submit' value='Submit' name='submit' onSubmit='return check()'>";
   $output .= "</form>\n";
   $output .= "<br>\n";
   $exchmemberid = $ad[exchmemberid];
   
 
?>
<table width='946' border="0" align="center" cellSpacing='0' cellPadding='4'>
<tr>
        <td>
                <font face="Arial, Verdana" size="5">
                        <b>Title goes here</b></font><br>
                        <br>
                <font face="Arial, Verdana" size="2">
                Description goes here....</font>
        </td>
</tr>
<tr>
        <td>
                <?php echo $output ?>
        </td>
 
</tr>
 
</table>
 

Code: Select all

 
******************saveform.php**********************************
<?php
require_once ("config.php");
 
$siterefid = $_SESSION['siterefid'];
$q1 = "SELECT * FROM users_exchg WHILE site_ref_id = $siterefid ORDER BY exch_id";";
$r1 = mysql_query($q1) or die(mysql_error());
 
mysql_close();
 
 
// list $a3 array values
$exchmemberid = $_SESSION[exchmemberid];
 
 
foreach ($exchmemberid as $exchmemberid_value) {
    echo "Value: " . $exchmemberid_value . "<br />\n";
    echo "Count: " . count($a3) . "<br />";
}
 
// UPDATE users_exchg SET exch_member_id = $exchmemberid";
 
 
?>

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 12:44 pm
by andyhoneycutt
what does print_r reveal about your array that you are trying to loop through? e.g.

Code: Select all

print_r($exchmemberid);
Do that before your foreach and post the output here and I can help you troubleshoot a bit better.

-Andy

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 12:56 pm
by reyes99
it shows 1 even though I am inputting more that one.

Thanks

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 1:28 pm
by andyhoneycutt
I don't see where you are populating the $_SESSION with the value of $exchmemberid.

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 1:36 pm
by reyes99
sorry I had to add it to test the print_r. I put it under the last $output after I set $exchmemberid = $ad[exchmemberid];

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 1:49 pm
by andyhoneycutt
ok, now i'm confused. where are you actually assigning the value of $exchmemberid then?

EDIT: So I do see where you're setting the value on the first entry. What does one code block have to do with the other? In the second code block where you are using the foreach, you are assigning the value of $exchmemberid to $_SESSION['exchmemberid']. No where in the first code block do you assign a value to the $_SESSION['exchmemberid'] variable. Am I missing something?

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 1:55 pm
by reyes99
Sorry I edited the post before you saw it. But I also found another error. where I assigned $exchmemberid = $ad[exchmemberid]; should of been $a3[exchmemberid];

I made that change and now in the print_r i get

exchmemberid1


Here is the code...sorry about that.

Code: Select all

  $output .= "</table>\n";
   $output .= "<input type='submit' value='Submit' name='submit' onSubmit='return check()'>";
   $output .= "</form>\n";
   $output .= "<br>\n";
   $exchmemberid = $a3[exchmemberid];
   $_SESSION['exchmemberid'] = $exchmemberid;

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 1:59 pm
by andyhoneycutt
Ok, now i think i'm understanding a bit better. I'll try to keep up. On line 32 of the first code block, after you assign the value of $a3, if you do a print_r($a3) what do you see?

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 2:08 pm
by reyes99
I get : Array ( [0] => exchmemberid ) 1Array ( [0] => exchmemberid ) 1
before the form loads and before I enter any data.

If I enter data and click Submit I still get : exchmemberid1 on the saveform.php

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 3:07 pm
by andyhoneycutt
change this:

Code: Select all

$a3 = array(exchmemberid);
to this:

Code: Select all

$a3 = array($exchmemberid);
and let me know what your results are.

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 3:16 pm
by reyes99
ok now I get this with the print_r after the $a3 = array($exchmemberid);

Array ( [0] => ) 1Array ( [0] => ) 1

and after I click submit i get the same thing (from saveform.php) :

exchmemberid1

Thanks

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 3:20 pm
by andyhoneycutt
Ok, after line 29 of the first code block, print_r $a1, after assigning $a2, do a print_r on it as well. Try and follow this thing back to wherever data is missing.

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 4:59 pm
by reyes99
Still can't figure it out, but I think it might be this line:

$output .= "<input type='text' name='exchmemberid' value='$a3[exchmemberid]'></font></td>\n";

Do you think that the name should be $a3[exchmemberid] also?? It seems like it is not posting the values when I click submit. Do you think that could be the problem?

Thanks

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 5:01 pm
by andyhoneycutt
Oh, try enclosing $a3[exchmemberid] in { } in that line.

Re: Form Input variable into array - Please help

Posted: Mon Jan 05, 2009 5:16 pm
by reyes99
sorry not sure where the { } go

This is where i put them and got the same results:

$output .= "<input type='text' name='{a3[exchmemberid]}' value='$a3[exchmemberid]'></font></td>\n";

I also tried this:

$output .= "<input type='text' name={'a3[exchmemberid]'} value='$a3[exchmemberid]'></font></td>\n";

and got the same results.