Jump to content

problem with foreach loop


Lassie

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/51341-problem-with-foreach-loop/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.