Jump to content
Sign in to follow this  
iammudassir

how to set array position

Recommended Posts

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>

Share this post


Link to post
Share on other sites

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'];

  • Like 1

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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 by JamesB
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...