Jump to content

Getting list of product reviews from the database


Omzy

Recommended Posts

I'm trying to output the list of product reviews from a table but I'm a bit stumped at the moment.

 

Table Reviews:

 

id

prod_id

comments

name

location

date

 

Obviously a product can have more than one review. I check that $prod_id matches prod_id field in the Reviews table.

 

Here is the code I have at the moment:

 

$query="SELECT * FROM reviews WHERE prod_id='$prod_id'";
$results=mysql_query($query);
$numrows=mysql_num_rows($results);
$row=mysql_fetch_assoc($results);

 

This code is in my controller file. So in my view file I need to output this data but I'm not sure if I should be using a while() loop or a foreach loop(). Ideally I would like to do all the processing in the controller file and then be able to loop through the results in my view file.

I would do it something like this:

 

$query="SELECT * FROM reviews WHERE prod_id='$prod_id'";
$results=mysql_query($query);
$numrows=mysql_num_rows($results);
while($row=mysql_fetch_assoc($results)){
     echo '<p>';
     echo $row['name'].'<br>';
     echo $row['location'].'<br>';
     // etc.
     echo '</p>';
}

You just need to spit it up into to files, otherwise if this is incorrect you should re-read your original post.

 

Controller:

<?php
$query="SELECT * FROM reviews WHERE prod_id='$prod_id'";
$results=mysql_query($query);
$numrows=mysql_num_rows($results);

include 'view.php';
?>

 

view.php:

<?php
while($row=mysql_fetch_assoc($results)){
     echo '<p>';
     echo $row['name'].'<br>';
     echo $row['location'].'<br>';
     // etc.
     echo '</p>';
}
?>

<?php
$query="SELECT * FROM reviews WHERE prod_id='$prod_id'";
$results=mysql_query($query);
$numrows=mysql_num_rows($results);
while($row=mysql_fetch_assoc($results)){
     include 'view.php';
}
?>

 

<?php
echo '<p>';
echo $row['name'].'<br>';
echo $row['location'].'<br>';
// etc.
echo '</p>';
?>

Not yet. Basically this is how it should work:

 

Controller:

 

-Run the query

-Get the resultset

-Put the resultset into an array

 

View:

 

-Loop through the array and output the data

 

Ideally I want to assign friendly variables to the $row array so they can be easily referenced in the view, e.g. $name instead of $row['name'].

 

Surely someone must have a solution to this simple scenario.

Yes I know how to assign variables.

 

I want to have the variables defined in the Controller file, ready to be used in the view file.

 

View file should only have to loop through the resultset and echo out the variables, eg. $name or $location.

Here, I managed to figure this out for myself:

 

Controller:

 

$query="SELECT name, location, comments, date FROM reviews WHERE prod_id='$prod_id'";
$results=mysql_query($query);
$numrows=mysql_num_rows($results);

while($row=mysql_fetch_assoc($results))
{
  $name[]=$row['name'];
  $loc[]=$row['location'];
  $com[]=$row['comments'];
  $date[]=$row['date'];
}

 

View:

 

 
for($i=0; $i<$numrows; $i++)
{
echo $name[$i];
echo $loc[$i];
echo $com[$i];
echo $date[$i];
}

 

I cannot beleive nobody here was able to tell me this, it would have saved me a lot of time.

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.