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.

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

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>';
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.