Lassie Posted May 14, 2007 Share Posted May 14, 2007 I have a simple programme to retrieve book reviews. My query on the db works but I have an invaliid foreach arguement. The code is <?php include ('book_sc_fns.php'); // The shopping cart needs sessions, so start one session_start(); require_once ('./reg/includes/config.inc.php'); $product_id = $_GET['product_id']; $review= get_book_reviews($product_id); if (!is_array($review)) { echo '<br />No reviews currently available for this book<br />'; } else { //display //create table echo '<table width = \"700\" border = 0 align="center">'; foreach ($review as $row) { echo '<tr><td>'; echo $row['product_desc']; echo '</td>'; echo '<tr><td>'; echo $row['review']; echo '</td>'; echo '</tr>'; } echo '</table>'; } ?> the functions called are function get_book_reviews($product_id) { // query database for all reviews for a particular book if (!$product_id || $product_id=='') return false; $connection = db_connect(); $query= "SELECT * FROM book_reviews WHERE product_id= '$product_id'"; $result = mysql_query($query); $result = db_result_to_array($result); /* Debug to check query echo '<pre>'; print_r ($result); echo '</pre>'; */ if (!$result) return false; $review=$result; return $review; } function db_result_to_array($result) { $res_array = array(); for ($count=0; $row = mysql_fetch_assoc($result); $count++) $res_array[$count]=$row; return $res_array; } Any help appreciated Quote Link to comment https://forums.phpfreaks.com/topic/51341-problem-with-foreach-loop/ Share on other sites More sharing options...
chigley Posted May 14, 2007 Share Posted May 14, 2007 Which errors are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/51341-problem-with-foreach-loop/#findComment-252835 Share on other sites More sharing options...
Lassie Posted May 14, 2007 Author Share Posted May 14, 2007 The problem is that when I test for the array as follows $review= get_book_reviews($product_id); if (!is_array($review)) { echo '<br />No reviews currently available for this book<br />'; } It returns negative, so no array. I am also not sure I have coded my foreach loop properly. Quote Link to comment https://forums.phpfreaks.com/topic/51341-problem-with-foreach-loop/#findComment-252842 Share on other sites More sharing options...
chigley Posted May 14, 2007 Share Posted May 14, 2007 Foreach looks fine to me. <?php /* Debug to check query echo '<pre>'; print_r ($result); echo '</pre>'; */ ?> Uncomment that and let's check your function is working Quote Link to comment https://forums.phpfreaks.com/topic/51341-problem-with-foreach-loop/#findComment-252850 Share on other sites More sharing options...
Lassie Posted May 14, 2007 Author Share Posted May 14, 2007 Thanks for coming back. It is now working. I'm not sure what was wrong but I may not have saved the changes I made to the code. Sorry to bother everyone. Quote Link to comment https://forums.phpfreaks.com/topic/51341-problem-with-foreach-loop/#findComment-252855 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.