Harlequin Posted March 19, 2011 Share Posted March 19, 2011 Hi. The idea: I want to pull a client record and simply place all the row columns into editable form field text boxes. Once the user reviews and edits a submit button will update the record. What I have: $Query01 = "SELECT * FROM `CustomerSignups` WHERE `Id` = '$_GET[id]'"; $Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error()); while ($get_info = mysql_fetch_row($Result01)) { foreach ($get_info as $field) echo "<p>$field</p>"; It displays as expected, a line for each field. How can I get this to pull the field names also and then I can use those as text box input fields...? Quote Link to comment Share on other sites More sharing options...
Eyewash01 Posted March 19, 2011 Share Posted March 19, 2011 <?php foreach ($example as $key => $value){ } ?> The variable $key will be equal to the field name Quote Link to comment Share on other sites More sharing options...
creata.physics Posted March 19, 2011 Share Posted March 19, 2011 Well, he'll need to change a few things to make this work properly, if he wants to use the foreach method. <?php $Query01 = "SELECT * FROM `CustomerSignups` WHERE `Id` = '$_GET[id]'"; $Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error()); while ($get_info = mysql_fetch_assoc($Result01)) { // you need to turn get info into an array, since it's already been looped. $get_info[] = $get_info; } // we need to end the while loop before foreach loop, if you didn't you'd be looping the same data twice. foreach ($get_info as $key => $value) { echo "<p>$key - $value</p>"; } ?> I changed mysql_fetch_row to mysql_fetch_assoc. I ended the while loop before the foreach loop. I printed your key and value for you, so you can have a good idea on how to go about doing the rest of this on your own. If you need any in-depth explanations, feel free to ask. Quote Link to comment Share on other sites More sharing options...
Harlequin Posted March 20, 2011 Author Share Posted March 20, 2011 Mmmm... OK, I get what you're doing and inserted the code like so: $Query01 = "SELECT * FROM `CustomerSignups` WHERE `Id` = '$_GET[id]'"; $Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error()); while ($get_info = mysql_fetch_assoc($Result01)) { $get_info[] = $get_info; } foreach ($get_info as $key => $value) { echo "<p>$key - $value</p>"; } } But I get this: Warning: Invalid argument supplied for foreach() in /home/... : eval()'d code on line 18 This is line 18: foreach ($get_info as $key => $value) But I can't see where the problem is. Obviously I'm going to perservere with this as my old way of doing it, calling out each table field individually is simply a waste of time and won't include new table fields. Quote Link to comment Share on other sites More sharing options...
creata.physics Posted March 20, 2011 Share Posted March 20, 2011 I see what you're saying, I tried with code with my own script and it works 100%. <?php $Query01 = "SELECT * FROM `CustomerSignups` WHERE `Id` = '$_GET[id]'"; $Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error()); while ($get_info = mysql_fetch_assoc($Result01)) { $info[] = $get_info; } foreach ($info as $field) { foreach ($field AS $key=>$value) { echo "{$key} - {$value}<br/>"; } } ?> It will show the field NAME then it's VALUE next to it. Hope this is what you wanted. Quote Link to comment Share on other sites More sharing options...
Harlequin Posted March 20, 2011 Author Share Posted March 20, 2011 Hi creata.physics Thanks for that, I think I can work with it. Will get back to you if I need any more help. Again, thanks... Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 20, 2011 Share Posted March 20, 2011 By storing the data in an intermediate array and looping over that array, when you are just going to output all the results anyway, you are causing that portion of the code to use twice as much memory (the data is already stored in memory in the result set) and you are causing that portion of the code to take twice as long to execute. <?php $Query01 = "SELECT * FROM `CustomerSignups` WHERE `Id` = '$_GET[id]'"; $Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error()); while ($get_info = mysql_fetch_assoc($Result01)) { foreach ($get_info AS $key=>$value) { echo "{$key} - {$value}<br/>"; } } ?> Quote Link to comment Share on other sites More sharing options...
creata.physics Posted March 20, 2011 Share Posted March 20, 2011 PFMaBiSmAd is correct, use his code. I thought I had tried his method, but I assume I was using mysql_fetch_row instead of assoc, because for the key i was getting numbers, 0-32. So again, use PFMaBiSmAd's code provided, it does the same thing but with less resources. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.