Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.