PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
reyes99
Forum Commoner
Posts: 38 Joined: Tue May 22, 2007 10:35 pm
Post
by reyes99 » Mon Jan 05, 2009 12:34 pm
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";
?>
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Mon Jan 05, 2009 12:44 pm
what does print_r reveal about your array that you are trying to loop through? e.g.
Do that before your foreach and post the output here and I can help you troubleshoot a bit better.
-Andy
reyes99
Forum Commoner
Posts: 38 Joined: Tue May 22, 2007 10:35 pm
Post
by reyes99 » Mon Jan 05, 2009 12:56 pm
it shows 1 even though I am inputting more that one.
Thanks
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Mon Jan 05, 2009 1:28 pm
I don't see where you are populating the $_SESSION with the value of $exchmemberid.
reyes99
Forum Commoner
Posts: 38 Joined: Tue May 22, 2007 10:35 pm
Post
by reyes99 » Mon Jan 05, 2009 1:36 pm
sorry I had to add it to test the print_r. I put it under the last $output after I set $exchmemberid = $ad[exchmemberid];
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Mon Jan 05, 2009 1:49 pm
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?
Last edited by
andyhoneycutt on Mon Jan 05, 2009 1:57 pm, edited 1 time in total.
reyes99
Forum Commoner
Posts: 38 Joined: Tue May 22, 2007 10:35 pm
Post
by reyes99 » Mon Jan 05, 2009 1:55 pm
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;
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Mon Jan 05, 2009 1:59 pm
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?
reyes99
Forum Commoner
Posts: 38 Joined: Tue May 22, 2007 10:35 pm
Post
by reyes99 » Mon Jan 05, 2009 2:08 pm
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
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Mon Jan 05, 2009 3:07 pm
change this:
to this:
and let me know what your results are.
reyes99
Forum Commoner
Posts: 38 Joined: Tue May 22, 2007 10:35 pm
Post
by reyes99 » Mon Jan 05, 2009 3:16 pm
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
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Mon Jan 05, 2009 3:20 pm
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.
reyes99
Forum Commoner
Posts: 38 Joined: Tue May 22, 2007 10:35 pm
Post
by reyes99 » Mon Jan 05, 2009 4:59 pm
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
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Mon Jan 05, 2009 5:01 pm
Oh, try enclosing $a3[exchmemberid] in { } in that line.
reyes99
Forum Commoner
Posts: 38 Joined: Tue May 22, 2007 10:35 pm
Post
by reyes99 » Mon Jan 05, 2009 5:16 pm
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.