Jump to content

Recommended Posts

I'm trying to learn more about using the foreach loop to get and retrieve information from a mysql table, and to display the code into a usable table.  I've gotten the below working, but I need to take a step further where I can assign these values into a form for future use.

 

I hope the above made sense.  Below is the code I've put together thus far.  But how would I use the values of the array in an html form?.  ie value=" " How you would identify each value for use?

 

//Query the Additional Tables to add to
$query = mysql_query("SELECT * FROM mydatabases ");
while ($data = mysql_fetch_assoc($query)) {
	$dataArray[] = $data;
	$rows = count($dataArray);
}

//Create a Table of this information
if($rows == 0)
{
	echo 'There are no additional tables';
} else {
	echo '<table border=1>
		<tr>
			<td>ID</td>	
			<td>Database</td>
			<td>Table</td>
			<td>User Field</td>
			<td>Password Field</td>
			<td>Email Field</td>
		</tr>';
	for($x=0; $x < $rows; $x++){
		echo '<tr>';
		foreach($dataArray[$x] as $key => $value){
	 		echo '<td>'.$value.'</td>';
 		}
 		echo '</tr>';
		}
	 echo '</table>';

 }

Link to comment
https://forums.phpfreaks.com/topic/129861-using-foreach/
Share on other sites

You have a while loop that assigns results to a var and you have for loop that displays it.  While you will need to assign the results to a var if you want to use it somewhere else, there's no reason not to get rid of the for loop and echo out the data there. 

 

As far as accessing the data in your $dataArray array: since you used a mysql_fetch_assoc, you would use the column names from your db table as the keys in your array.  For example, you would do this:

 

echo $dataArray[0]['User']; // user in first row
echo $dataArray[0]['Email']; // email in first row
echo $dataArray[1]['User']; // user in 2nd row
echo $dataArray[1]['Email']; // email in 2nd row

 

Or you could loop through it or whatever.  What needs to be put in your value='' in your form really depends on how you run your loop and what exactly you're wanting to put in your form. 

Link to comment
https://forums.phpfreaks.com/topic/129861-using-foreach/#findComment-673281
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.