PHP/HTML Div form "checkbox array" foreach

invalid argument supplied for foreach()

Page 1 of 1

11 Replies - 5263 Views - Last Post: 07 October 2010 - 10:58 AM Rate Topic: -----

#1 emetiib  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 06-October 10

PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 04:18 AM

Stumped again ...

I have HTML forms with PHP using foreach to loop through the db data.

My code works on the 1st two forms yet on the 3rd I am adding a checkbox for each category in categories and I get an error.

FORM 1 CODE WORKS
<?php foreach($search_categories as $search_cat): ?>
<option value="<?php echo $search_cat['id'] ?>"><?php echo $search_cat['name'] ?></option>
<?php endforeach; ?>



FORM 2 CODE WORKS
<?php if(!empty($jobs)): 
  
$row_count = 1;
  
foreach($jobs as $job):     
               
$row_style = ($row_count % 2) ? 'jcolor1' : 'jcolor2'; 
?>
            
<div class= "<?php echo $row_style; ?>">
<span class="location"><?php echo safe_output($job['location']); ?></span> <a href="index.php?view=show&id=<?php echo $job['id']; ?>" class="job-link"><?php echo safe_output($job['title']); ?></a> <span class="at"> at </span> <?php echo safe_output($job['company']); ?>
</div>
<?php $row_count++; endforeach; ?>
<?php endif; ?>



Mysql db has 4 categories which 'id' I need to loop through so I can post the 'name' of each on my form next to a checkbox.

FORM 3 CODE HAS an ERROR
warning: invalid argument supplied for foreach()

<label><b>Select Categories</b></label>
<?php foreach( $categories as $category): ?>
<input type="checkbox" name="job[cats][]" value="<?php echo $category['id']; ?>" > <?php echo $category['name']; ?> <br/>
<?php endforeach; ?>



This post has been edited by emetiib: 07 October 2010 - 04:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP/HTML Div form "checkbox array" foreach

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,136
  • Joined: 08-June 10

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 04:26 AM

View Postemetiib, on 07 October 2010 - 11:18 AM, said:

FORM 3 CODE HAS an ERROR
warning: invalid argument supplied for foreach()

that simply means that $categories is neither traversable nor an array. unfortunatly your code is too unreadable/incomplete to spot the source of $categories, which may hint the reason.

This post has been edited by Dormilich: 07 October 2010 - 04:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 04:30 AM

I see $search_categories, but not $categories.
Was This Post Helpful? 0
  • +
  • -

#4 emetiib  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 06-October 10

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 04:49 AM

View PostJackOfAllTrades, on 07 October 2010 - 03:30 AM, said:

I see $search_categories, but not $categories.



My functions are accessed from my controller using ...

include('db_fns.php');


$controller = 'jobs';

$view = empty($_GET['view']) ? 'index' : $_GET['view'];
	
switch ($view) {
	
		
		
case "new":
$categories = find_categories();
break;
}




My functions for find_categories() are
	
 /**
 * selects categories and counts number of jobs in each category.
 * @param type variable
 * @return type
 */
function find_categories()
{
db_connect();
		
$query =  "SELECT 
categories.name, 
categories.id,
COUNT(jobs2categories.job_id) as numjobs
FROM
categories
LEFT JOIN
jobs2categories ON categories.id = jobs2categories.category_id
GROUP BY
categories.id
ORDER BY
categories.name ASC
";

$result = mysql_query($query);
		
$result = db_result_to_array($result);
		
return $result;
	
}
	
	
	
 /**
 * selects one category
 * @param int $id
 * @return array
 */
function find_category($id)
{
db_connect();
		
$query =  sprintf("SELECT 
categories.name, 
categories.id
FROM 
categories
WHERE 
categories.id = '%s'	 
", mysql_real_escape_string($id)
);

$result = mysql_query($query);
		
$row = mysql_fetch_array($result);
		
return $row;
	
}	



Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,136
  • Joined: 08-June 10

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 04:51 AM

the find_categories() function does not necessarily return an array. it may return a boolean if the query fails (which you don’t even check (at least not in the code given))

This post has been edited by Dormilich: 07 October 2010 - 05:14 AM
Reason for edit:: spelling

Was This Post Helpful? 0
  • +
  • -

#6 emetiib  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 06-October 10

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 05:08 AM

View PostDormilich, on 07 October 2010 - 03:51 AM, said:

the find_categories() function does not necessarily returns an array. it may return a boolean if the query fails (which you don’t even check (at least not in the code given))


I have checked and used the arrays.

FORM 1 CODE WORKS and FORM 3 CODE does not work
They both use the same functions/arrays.
That is why I am stumped.

The only real difference in the use of the function find_categories() is Form 3 adds the check box type="checkbox" name="job[cats][]" value= is basically the same in each foreach statement.

This post has been edited by emetiib: 07 October 2010 - 05:17 AM

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,136
  • Joined: 08-June 10

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 05:14 AM

what happens if your 3rd query fails?
Was This Post Helpful? 0
  • +
  • -

#8 emetiib  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 06-October 10

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 09:29 AM

View PostDormilich, on 07 October 2010 - 04:14 AM, said:

what happens if your 3rd query fails?


I check my db 1st to see if it is connected, empty or outdated. Then check code, do a print_r to get all my values
or die("Query failed with error: ".mysql_error());

I thought my issue was with the nested checkbox array within the array.



I discovered the error preventing my foreach loop from getting the array I sent it was that my
form action=? 

was not set correctly.

Thank you for your assistance.

This post has been edited by emetiib: 07 October 2010 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,136
  • Joined: 08-June 10

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 09:32 AM

… PDO in exception mode wouldn’t have let you reach the foreach() from the start.
Was This Post Helpful? 0
  • +
  • -

#10 emetiib  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 06-October 10

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 10:03 AM

View PostDormilich, on 07 October 2010 - 08:32 AM, said:

… PDO in exception mode wouldn’t have let you reach the foreach() from the start.


I am new to PHP and have not used PDO

Is this what you would have used to discover that the source of the array was fine yet the destination path of the array was not being passed to the correct *.php file?

<?php
$categories = new category ;
try {
    $response = $categories->find_categories();
} catch(category  $e) {
    echo $e->getMessage();
}
?>

This post has been edited by Dormilich: 07 October 2010 - 10:19 AM
Reason for edit:: please use [code] [/code] tags when posting code

Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,136
  • Joined: 08-June 10

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 10:24 AM

View Postemetiib, on 07 October 2010 - 05:03 PM, said:

[Is this what you would have used to discover that the source of the array was fine yet the destination path of the array was not being passed to the correct *.php file?

yes and no. in principle yes, but my implementation would look totally different (due to some of PDO’s properties)
Was This Post Helpful? 0
  • +
  • -

#12 emetiib  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 06-October 10

Re: PHP/HTML Div form "checkbox array" foreach

Posted 07 October 2010 - 10:58 AM

View PostDormilich, on 07 October 2010 - 09:24 AM, said:

View Postemetiib, on 07 October 2010 - 05:03 PM, said:

[Is this what you would have used to discover that the source of the array was fine yet the destination path of the array was not being passed to the correct *.php file?

yes and no. in principle yes, but my implementation would look totally different (due to some of PDO’s properties)


Ok, if you have time to show an example of how you would implement it I would certainly appreciate it being new to PHP and PDO.

like
/**
SOME TEST ...
code reached, error_reporting test
*/

echo 'debug: would send location header', $filename, $unsetVariableTriggeringWarning;
flush();

if (headers_sent()) {
  die('cannot send location header (anymore)');
}
else {
  header('Location: '.$filename);
  die();
}



I have learned more from relevant code examples in Forums than I have from any book or class.

That is why I always post my solutions :bigsmile:

This post has been edited by emetiib: 07 October 2010 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1