iammudassir Posted November 25, 2012 Share Posted November 25, 2012 how to use array to echo result in specific field <?php //$conn=mysql_connect("localhost", "root", "");//$db=mysql_select_db ('geotex', $conn) or die (" error "); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title></head> <body> i have "perosn" table in database "geotex" with 3 fields ###### age and person date in table like tis ###### name age </br>male abc 33</br>male xyz 2</br>female 123 25 <form action="person.php" method="post"> <table width="299" border="0" cellpadding="3"> <tr> <td width="109"><div align="center">######</div></td> <td width="107"><div align="center">Name</div></td> <td width="57"><div align="center">Age</div></td> </tr></table> <table width="200" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="62"><div align="center"> <input name="######[]" type="text" size="20 " /> </div></td> <td width="62"><div align="center"> <input name="name[]" type="text" value= "<?php // i set value here for this field if (isset ($_POST['select'])) {$sql = "select * from person where ###### = 'male' "; $run = mysql_query($sql); while ($result = mysql_fetch_array($run)) {echo $name = $result ['name'];} // echo shows both abc and xyz but i wanna show abc name using array position }?>" size="20 " /> </div></td> <td width="88"><div align="center"> <input name="age[]" type="text" value= "<?php if (isset ($_POST['select'])) {$sql = "select * from person"; $run = mysql_query($sql); while ($result = mysql_fetch_array($run)) {echo $age = $result ['age'];} // echo shows both 33 and 2 but i wanna show abc age using array position }?>" size="10" /> </div></td> </tr> <tr> <td width="62"><div align="center"> <input name="######[]" type="text" size="20 " /> </div></td> <td width="62"><div align="center"> <input name="name[]" type="text" size="20 " /> </div></td> <td width="88"><div align="center"> <input name="age[]" type="text" size="10" /> </div></td> </tr> </table> <table width="200" border="0" cellpadding="3"> <td width="134"><div align="center"> <input name="select" type="submit" value="SELECT" /> </div></td> </table> </form>i wanna show result with array position mean in first row i need abc in name filed 33 in age field and male in ###### field and xyz in second row as well </body></html> Link to comment Share on other sites More sharing options...
JamesB Posted November 25, 2012 Share Posted November 25, 2012 to load all returned sql rows into an array: $sql = "select * from person where ###### = 'male' ";$result = mysql_query($sql); $rows = array();while ($row = mysql_fetch_array($result)) { $rows[] = $row;} then to read the array: echo $rows[0]['name'];echo $rows[0]['age']; 1 Link to comment Share on other sites More sharing options...
iammudassir Posted November 26, 2012 Author Share Posted November 26, 2012 thank you soooooooooooooooooooooooooooooooo much i spent almost 2 weeks in this issue acutally i m new in php and also no idea about array thank you sooo much again Link to comment Share on other sites More sharing options...
iammudassir Posted November 26, 2012 Author Share Posted November 26, 2012 one more problem let suppose i have 5 rows in my table like this and a button name submit field1[name] i set value for filed1 value = if (isset=$_POST['submit'])) { echo $rows[0]['name'];} field1[name] i set value for filed1 value = if (isset=$_POST['submit'])) { echo $rows[1]['name'];} field1[name] i set value for filed1 value = if (isset=$_POST['submit'])) { echo $rows[2]['name'];} field1[name] i set value for filed1 value = if (isset=$_POST['submit'])) { echo $rows[3]['name'];} field1[name] i set value for filed1 value = if (isset=$_POST['submit'])) { echo $rows[4]['name'];} problem is this that if $rows[3] or [4] is empty means my query has only three rows then last 2 fields generating errors because i specify value there array [3] and [4]i want if my query has only 2 rows then it will echo first two array [0] [1] but other fields remains empty dont give any error. Link to comment Share on other sites More sharing options...
iammudassir Posted November 26, 2012 Author Share Posted November 26, 2012 to load all returned sql rows into an array: $sql = "select * from person where ###### = 'male' ";$result = mysql_query($sql); $rows = array();while ($row = mysql_fetch_array($result)) { $rows[] = $row;} then to read the array: echo $rows[0]['name'];echo $rows[0]['age']; one more problemlet suppose i have 5 rows in my table like this and a button name submitfield1[name] i set value for filed1 value = if (isset=$_POST['submit'])) { echo $rows[0]['name'];} field1[name] i set value for filed1 value = if (isset=$_POST['submit'])) { echo $rows[1]['name'];} field1[name] i set value for filed1 value = if (isset=$_POST['submit'])) { echo $rows[2]['name'];} field1[name] i set value for filed1 value = if (isset=$_POST['submit'])) { echo $rows[3]['name'];} field1[name] i set value for filed1 value = if (isset=$_POST['submit'])) { echo $rows[4]['name'];} problem is this that if $rows[3] or [4] is empty means my query has only three rows then last 2 fields generating errors because i specify value there array [3] and [4]i want if my query has only 2 rows then it will echo first two array [0] [1] but other fields remains empty dont give any error. Link to comment Share on other sites More sharing options...
JamesB Posted November 27, 2012 Share Posted November 27, 2012 (edited) one option would be to use: echo empty($rows[0]['name']) ? '' : $rows[0]['name']; or you could create array elements with blank values initially: $fieldCount = 5;for ($i=0; $i<$fieldCount; $i++) { $rows[$i] = array( 'name' => '' );} but i think calling a function with the first option would be better. Edited November 27, 2012 by JamesB 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now