mdmartiny Posted April 20, 2012 Share Posted April 20, 2012 I am in the process of writing code for a dynamic form. I have the part of the form written for information to be entered. I am now trying to write code for the modification of the information in the table. I know that I want to get the table data my doing a MySql Select and putting that information into an array. I am not very familiar with arrays and have spent some time looking for a solution online with no solution. When I print_r the results I get what I want but when I do anything else I get Array. What I am actually looking to do is make it so that the table data is in another variable this is the section of code that I am working with. $mod_form = "<form name='insert_table' id='insert_table' action='ad_add.php' method='post' enctype='multipart/form-data'> <fieldset> <legend>table information</legend> <input name='table' TYPE='hidden' VALUE='" . $table . "' />"; $mod_sql = "SELECT * FROM $table WHERE id = '$id'"; $mod_sql_result = mysql_query($mod_sql); $allowed_ext = array('jpg', 'jpeg', 'png', 'gif'); $input_array = array(); while ($row = mysql_fetch_array($mod_sql_result, MYSQL_NUM)) { $input_array[] = $row; } foreach ($input_array as $input => $data) { print_r($data); } while ($i < mysql_num_fields($mod_sql_result)) { $header = str_replace("_", " ", (mysql_field_name($mod_sql_result, $i))); $name = mysql_field_name($mod_sql_result, $i); (mysql_field_name($mod_sql_result, $i) == 'image' ? $mod_form .= "<p>" . $header . ": <input name='" . $name . "' id='" . $name . "' type='file' /></p>" : $mod_form .= "<p>" . $header . ": <input name='" . $name . "' id='" . $name . "' type='text' value='" . $input . "'/></p>"); $i++; } $mod_form .= " <p> <input type='submit' name='submit_mod' id='submit_mod' /> </p> </fieldset> </form> "; echo $mod_form; Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/ Share on other sites More sharing options...
chriscloyd Posted April 20, 2012 Share Posted April 20, 2012 well do this <?php $Get = mysql_query("SELECT....."); $Info = mysql_fetch_array($Get); //that sets all the info as an array //so echoing and choosing the key is simple echo $Info['keyhere/tablename']; /*that will echo the variable for that key*/ Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1338977 Share on other sites More sharing options...
chriscloyd Posted April 20, 2012 Share Posted April 20, 2012 you could also do <?php foreach ($Info as $key => $val) { //so every row it pulled from the database can be printed at least once. echo "{$key}: {$val}"; } Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1338980 Share on other sites More sharing options...
mdmartiny Posted April 20, 2012 Author Share Posted April 20, 2012 Could I put it so that each value is put into a variable of some kind so I can insert it into a form field for editing? Also when I Echo all it says is Array Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1338981 Share on other sites More sharing options...
chriscloyd Posted April 20, 2012 Share Posted April 20, 2012 What does your table look like where your getting your information? Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1338982 Share on other sites More sharing options...
mdmartiny Posted April 20, 2012 Author Share Posted April 20, 2012 This is just the test database that I am working with. Once this is done I will not know what the column names are going to be. The person that this is for will make all of the fields and names themselves. I am just writing the php code Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1338985 Share on other sites More sharing options...
chriscloyd Posted April 20, 2012 Share Posted April 20, 2012 Here you go <?php //get sql $Sql = "SELECT * FROM {$table} WHERE id = '{$id}'"; //run query $Query = mysql_query($Sql); //allowed ext $allowed_ext = array('jpg', 'jpeg', 'png', 'gif'); //input array) $input_array = array(); //$data $info = mysql_fetch_array($Query); foreach ($info as $val) { $input_array[] = $val; } //now start the form $Form = "<form name=\"insert_table\" id=\"insert_table\" action=\"ad_add.php\" method=\"post\" enctype=\"multipart/form-data\"> <fieldset> <legend> Table Information </legend> <input name=\"table\" type=\"hidden\" value=\"{$table}\">"; //run foreach lop now foreach ($input_array as $key => $val) { $header = str_replace("_"," ",$key); $Form .= ($key == 'image' ? "<p>{$header}: <input name=\"{$key}\" id=\"{$key}\" type=\"file\" /></p>" : "<p>{$header}: <input name=\"{$key}\" id=\"{$key}\" type=\"text\" value=\"{$val}\" /></p>" ); } $Form .= "<p> <input type=\"submit\" name=\"submit_mod\" id=\"submit_mod\" /> </p> </fieldset> </form>"; echo $Form; ?> Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1338986 Share on other sites More sharing options...
mdmartiny Posted April 20, 2012 Author Share Posted April 20, 2012 Now it is giving me double the fields on the page http://www.webpagescreenshot.info/img/379433-420201284323AM Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1338987 Share on other sites More sharing options...
chriscloyd Posted April 20, 2012 Share Posted April 20, 2012 try <?php //get sql $Sql = "SELECT * FROM {$table} WHERE id = '{$id}'"; //run query $Query = mysql_query($Sql); //allowed ext $allowed_ext = array('jpg', 'jpeg', 'png', 'gif'); //input array) $input_array = array(); //$data $info = mysql_fetch_array($Query); //now start the form $Form = "<form name=\"insert_table\" id=\"insert_table\" action=\"ad_add.php\" method=\"post\" enctype=\"multipart/form-data\"> <fieldset> <legend> Table Information </legend> <input name=\"table\" type=\"hidden\" value=\"{$table}\">"; //run foreach lop now foreach ($info as $key => $val) { $header = str_replace("_"," ",$key); $Form .= ($key == 'image' ? "<p>{$header}: <input name=\"{$key}\" id=\"{$key}\" type=\"file\" /></p>" : "<p>{$header}: <input name=\"{$key}\" id=\"{$key}\" type=\"text\" value=\"{$val}\" /></p>" ); } $Form .= "<p> <input type=\"submit\" name=\"submit_mod\" id=\"submit_mod\" /> </p> </fieldset> </form>"; echo $Form; ?> Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1338989 Share on other sites More sharing options...
mdmartiny Posted April 20, 2012 Author Share Posted April 20, 2012 It is still giving me the double fields Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1338990 Share on other sites More sharing options...
Barand Posted April 20, 2012 Share Posted April 20, 2012 Try "mysql_fetch_assoc" instead of "mysql_fetch_array" Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1339005 Share on other sites More sharing options...
mdmartiny Posted April 20, 2012 Author Share Posted April 20, 2012 Thank you for all of the help... I have been reading about the unset function to remove things from an array but when I do unset($info[0]) it does not remove the item from my view Quote Link to comment https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/#findComment-1339070 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.