$query = "SELECT region.*,
districts.*
FROM region
LEFT JOIN districts ON region.region_code = districts.region_code
WHERE region.region_status = '1' AND
districts.district_status = '1'
ORDER BY region.region_name";
$result = mysql_query($query) or die("Invalid query: $query");
if(mysql_num_rows($result) == 0)
echo "No Files exist";
else
{
while($row = mysql_fetch_array($result))
{
$available_titles[$row["region.region_name"]][] = array('region_id' => $row['region.id'],
'region_code' => $row['region.region_code'],
'district_id' => $row["districts.id"],
'district_region_code' => $row["districts.region_code"],
'district_name' => $row["districts.district_name"]);
}
//grab the titles
foreach($available_regions as $region => $tmp)
{
echo "<h1>".$region."</h1>";
//grab the files
foreach($tmp as $district_name => $data)
{
//this is grabbing the information from the array $available_titles to fill these fields.
echo($data['district_name']);
echo '<br>';
}
echo "<br>";
}
}
the error: Warning: Invalid argument supplied for foreach() in
I am trying to display the districts that are inside of each region with this foreach stmt.
Regions are in one table and districts are in another (as I am sure you can see). I will eventually once I am able to get the info to show, will be making the districts into check box's in order to insert my information into another table, that will allow user access based on region's, and districts assigned to the user.
Any thoughts on why I might be receiving this error, or even better would be any thoughts on how I can get this foreach stmt to function. Thanks in advance.
The array you are making in the while loop is called - $available_titles. The array you are referencing in the foreach loop is called - $available_regions.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Nice, cant believe I missed that.
I now am having a new issue.
All I am seeing is check box's, and it is not displaying any information.
example of what I should see
region1
(box)District1
(box)District2
(box)District3
region2
(box)District1
(box)District2
(box)District3
instead I am recivnging
blank
(box)
(box)
(box)
(box)
(box)
(box)
Any thoughts.
Code below:
PHP Code:
<form name="add_region_district" method="post" action=""> <? $query = "SELECT region.*, districts.* FROM region LEFT JOIN districts ON region.region_code = districts.region_code WHERE region.region_status = '1' AND districts.district_status = '1' ORDER BY region.region_name"; $result = mysql_query($query) or die("Invalid query: $query"); if(mysql_num_rows($result) == 0) echo "No Files exist"; else {
while($row = mysql_fetch_array($result)) { $available_regions[$row["region.region_name"]][] = array('region_id' => $row['region.id'], 'region_code' => $row['region.region_code'], 'district_id' => $row["districts.id"], 'district_region_code' => $row["districts.region_code"], 'district_name' => $row["districts.district_name"]); } //grab the titles foreach($available_regions as $row["region.region_name"] => $tmp) { echo '<h1>'.$row["region.region_name"].'</h1>'; //grab the files foreach($tmp as $row["districts.district_name"] => $data) { //this is grabbing the information from the array $available_titles to fill these fields. echo($data['district_name'].'<input type="checkbox" name="district_name[]" value="district_name[]" />'); echo '<br>'; } echo "<br>"; } } ?> </form>
<? $query = "SELECT region.*, districts.* FROM region LEFT JOIN districts ON region.region_code = districts.region_code WHERE region.region_status = '1' AND districts.district_status = '1' ORDER BY region.region_name"; $result = mysql_query($query) or die("Invalid query: $query"); if(mysql_num_rows($result) == 0) echo "No Files exist"; else{
while($row = mysql_fetch_array($result)) { $available_regions[$row["region_name"]][] = array('region_id' => $row[0], 'region_code' => $row[1], 'district_id' => $row[4], 'district_region_code' => $row[5], 'district_name' => $row[7]); } //grab the titles foreach($available_regions as $reg => $tmp) { echo '<h4>'.$reg.'</h4>'; //grab the files foreach($tmp as $dis => $data) { //this is grabbing the information from the array $available_titles to fill these fields. echo $data['district_name'].('<input type="checkbox" name="district_name[]" value="district_name[]" />'); echo '<br>'; } echo "<br>"; } } ?>
Thanks again Mab, and all for your continued work and support w/ this forum.
Having trouble finishing this off here
the insert code
PHP Code:
if ($_POST['region_code'] > 1){ foreach ($_POST['region_code'] as $foo=>$rgen) { $regionCode = mysql_real_escape_string($_POST['region_code'][$foo]); $district = mysql_real_escape_string($_POST['district_id'][$foo]); $inserto = mysql_query("INSERT INTO access VALUES('".$edit_emp_id."','".$regionCode."','".$district."')") or die(mysql_error()); } }
the form
PHP Code:
$query = "SELECT region.id, region.region_code, region.region_name, districts.id, districts.district_name FROM region LEFT JOIN districts ON region.region_code = districts.region_code WHERE region.region_status = '1' AND districts.district_status = '1' ORDER BY region.region_name"; $result = mysql_query($query) or die("Invalid query: $query"); if(mysql_num_rows($result) == 0) echo "No Files exist"; else{
while($row = mysql_fetch_array($result)) { //row # follows along with how they are called $available_regions[$row[2]][] = array('region_id' => $row[0], 'region_code' => $row[1], 'district_id' => $row[3], 'district_name' => $row[4]); } //grab the regions foreach($available_regions as $reg => $tmp) { echo '<h4>'.('<input type="checkbox" name="region_code[]" value="region_code[]" />').$reg.'</h4>'; //grab the districts foreach($tmp as $dis => $data) { echo '<h5 class="alt2">'.('<input type="checkbox" name="district_id[]" value="district_id[]" />').$data['district_name'].'</h5>'; } } }
It is inserting the (x)amount of rows that pertain to the selection of the checkboxs, and adding the emp_id to them, but it is not adding the region_code, or the district_id.
The array now works, and it displays and logs everything correctly. But when I am running the "See if the checkbox is checked" it works for the regions, but not for the districts.
I have the code that is not working commented out. with this as the lookat point
//THIS CHUNCK DOESNT WORK
PHP Code:
<? $query = "SELECT region.id, region.region_code, region.region_name, districts.id, districts.district_name FROM region LEFT JOIN districts ON region.region_code = districts.region_code WHERE region.region_status = '1' AND districts.district_status = '1' ORDER BY region.region_name"; $result = mysql_query($query) or die("Invalid query: $query"); if(mysql_num_rows($result) == 0) echo "No Files exist"; else{
while($row = mysql_fetch_array($result)) { //row # follows along with how they are called $available_regions[$row[1]][] = array('region_id' => $row[0], 'region_name' => $row[2], 'district_id' => $row[3], 'district_name' => $row[4]); } //grab the regions foreach($available_regions as $region_code => $tmp) { //convert function include('../../_admin/includes/functions/convertRegionCode_regionName.php'); //see if any of the regions are pre selected $selected_region_query = "SELECT region_code FROM access WHERE emp_id = '".$edit_emp_id."'"; $selected_region_Result = mysql_query($selected_region_query) or die('bad query:'.$selected_region_query); $selected_region_num_rows = mysql_num_rows($selected_region_Result); if($selected_region_num_rows == 0){ $selected = ''; }else{ $sR = 0; while ($selected_region_num_rows > $sR) { $selected_region = mysql_result($selected_region_Result,$sR,0); if($region_code == $selected_region){ $selected = 'CHECKED';} ++$sR; } } echo '<h4><input type="checkbox" name="region_code[]" '.$selected.' value="'.$region_code.'" />'.$region_name.'</h4>'; //grab the districts foreach($tmp as $district_id => $data) { /*//THIS CHUNCK DOESNT WORK $selected_district_query = "SELECT district_id FROM access WHERE emp_id = '".$edit_emp_id."'"; $selected_district_Result = mysql_query($selected_district_query) or die('bad query:'.$selected_district_query); $selected_district_num_rows = mysql_num_rows($selected_district_Result); if($selected_district_num_rows == 0){ $selectedD = ''; }else{ $s = 0; while ($selected_district_num_rows > $s) { $selected_district = mysql_result($selected_district_Result,$s,0); if($data['district_id'] == $selected_district){ $selectedD = 'CHECKED';} ++$s; } }*/ echo $selected_district; echo '<h5 class="alt2"><input type="checkbox" name="district_id[]" '.$selectedD.' value="'.$data['district_id'].'" />'.$data['district_name'].'</h5>'; } } } ?>
The logging script, in case any one has a cleaner way of doing this.
PHP Code:
//insert into access table if ($_POST['region_code'] > 1){ foreach ($_POST['region_code'] as $foo=>$assigned_region) { $regionCode = mysql_real_escape_string($assigned_region); $inserto = mysql_query("INSERT INTO access SET emp_id = '".$edit_emp_id."', region_code = '".$regionCode."'") or die(mysql_error()); } } if ($_POST['district_id'] > 1){ foreach ($_POST['district_id'] as $foo=>$assigned_district) { $districtId = mysql_real_escape_string($assigned_district); $inserto = mysql_query("INSERT INTO access SET emp_id = '".$edit_emp_id."', district_id = '".$districtId."'") or die(mysql_error()); } }