Omzy Posted January 30, 2010 Share Posted January 30, 2010 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 https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/ Share on other sites More sharing options...
The Little Guy Posted January 30, 2010 Share Posted January 30, 2010 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 https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004368 Share on other sites More sharing options...
Omzy Posted January 30, 2010 Author Share Posted January 30, 2010 No, like I said I'm wanting to output the data in a separate view file. Link to comment https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004371 Share on other sites More sharing options...
The Little Guy Posted January 30, 2010 Share Posted January 30, 2010 Then put it in your view file.... Link to comment https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004372 Share on other sites More sharing options...
Omzy Posted January 30, 2010 Author Share Posted January 30, 2010 I suggest you re-read my original post... 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 https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004374 Share on other sites More sharing options...
The Little Guy Posted January 30, 2010 Share Posted January 30, 2010 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 https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004377 Share on other sites More sharing options...
Omzy Posted January 30, 2010 Author Share Posted January 30, 2010 while($row=mysql_fetch_assoc($results)){ That's performing the query in the view file - ideally I would like the resultset to be available to the view file beforehand. Link to comment https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004380 Share on other sites More sharing options...
The Little Guy Posted January 30, 2010 Share Posted January 30, 2010 <?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>'; ?> Link to comment https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004385 Share on other sites More sharing options...
Omzy Posted January 30, 2010 Author Share Posted January 30, 2010 I'm not too conviced that's the correct way of doing it, but it won't work for me anyway because I'm including the controller file in the view file, not the other way round. Link to comment https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004387 Share on other sites More sharing options...
The Little Guy Posted January 30, 2010 Share Posted January 30, 2010 Do you have a better example of what you have? Link to comment https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004388 Share on other sites More sharing options...
Omzy Posted January 30, 2010 Author Share Posted January 30, 2010 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 https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004396 Share on other sites More sharing options...
The Little Guy Posted January 30, 2010 Share Posted January 30, 2010 insead of this: <?php echo '<p>'; echo $row['name'].'<br>'; echo $row['location'].'<br>'; // etc. echo '</p>'; ?> you could do this: <?php $name = $row['name']; $location = $row['location']; // etc. ?> Link to comment https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004403 Share on other sites More sharing options...
Omzy Posted January 30, 2010 Author Share Posted January 30, 2010 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 https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004411 Share on other sites More sharing options...
Omzy Posted January 30, 2010 Author Share Posted January 30, 2010 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 https://forums.phpfreaks.com/topic/190397-getting-list-of-product-reviews-from-the-database/#findComment-1004417 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.