Strange array problem...
Posted: Wed Feb 26, 2003 7:44 pm
I'm using the following code to pull info from a MySQL DB and print it out:
It pulls the info properly... but the problem I have is that when it gets the information from a single field in the MySQL db, it resets to the first field, prints it, and then prints the next one.. until it gets through all the fields. L
Like this:
I was looking at 'foreach' on PHP.net.. and I guess that's my problem. Any solution for this?
Code: Select all
<?php
//i am assuming all DB connections have been made
$user_query = "SELECT * FROM site_rights WHERE username='$username'";
$table_query = "SHOW FIELDS FROM site_rights";
$user_result = mysql_query($user_query);
$table_result = mysql_query($table_query);
//get the list of fields to check
while ($table = mysql_fetch_array($table_result)) {
//exclude the username field cause we're not checking it
if ($table['Field'] != 'username') {
$fields[] = $table['Field'];
}
}
$user_fields = mysql_fetch_array($user_result);
//loop through each available field
foreach($fields as $key => $field) {
if ($user_fields[$field] != 0) {
//check each fieldname for a non-zero value
$editable_fields[$field] = $user_fields[$field];
print_r($editable_fields);
}
}
?>Like this:
Code: Select all
Array
(
їid] => 1
)
Array
(
їid] => 1
їhw] => 1
)
Array
(
їid] => 1
їhw] => 1
їaom] => 1
)
Array
(
їid] => 1
їhw] => 1
їaom] => 1
їron] => 1
)
Array
(
їid] => 1
їhw] => 1
їaom] => 1
їron] => 1
їee] => 1
)
Array
(
їid] => 1
їhw] => 1
їaom] => 1
їron] => 1
їee] => 1
їaok] => 1
)