Current location: Hot Scripts Forums » Programming Languages » PHP » foreach function acting up


foreach function acting up

Reply
  #1 (permalink)  
Old 08-06-07, 12:49 PM
soloWebDev soloWebDev is offline
Wannabe Coder
 
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
foreach function acting up

the code:
PHP Code:

$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.
__________________
We will see what happens next.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-06-07, 01:40 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
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???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-06-07, 02:00 PM
soloWebDev soloWebDev is offline
Wannabe Coder
 
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
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>
Thanks agian for the quick responce mab.
__________________
We will see what happens next.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-06-07, 02:21 PM
soloWebDev soloWebDev is offline
Wannabe Coder
 
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
SOLVED---
PHP Code:

<?
$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.
__________________
We will see what happens next.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 08-06-07, 05:11 PM
soloWebDev soloWebDev is offline
Wannabe Coder
 
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
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.

I am wondering if any one has any thoughts?
__________________
We will see what happens next.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 08-07-07, 11:20 AM
soloWebDev soloWebDev is offline
Wannabe Coder
 
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Same code with things added, but new problem

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());
                                        }
                                    } 
__________________
We will see what happens next.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Probelm installaing PHProxy mo_mughrabi PHP 3 08-04-06 12:25 AM
Classified Ads skipper23 Perl 3 11-22-05 03:22 AM
ASP upload prob minority ASP 1 06-27-05 09:35 AM
PHP Error Fairnie PHP 8 06-26-04 08:15 AM
Classified Ads skipper23 Perl 2 12-30-03 04:43 AM


All times are GMT -5. The time now is 01:04 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.