Print every review stored in the database - php

I am in the process of building a institute review system and i just completed inserting reviews to the database. However i am in a fix as to how to display to the user all the reviews related to the particular institute.
Below is my code to retrieve review information along with the name of the person who has left the review:
$get_review_query = "SELECT * FROM reviews WHERE institute_id = {$id}";
$result = mysqli_query($connection, $get_review_query);
if(!$result) {
die("Database query failed.");
}
$count = mysqli_num_rows($result);
if($count == 0) {
$output = "There're no reviews for this institute.";
} else {
while($row = mysqli_fetch_assoc($result)) {
$review_contents = $row['content'];
$institute_id = $row['institute_id'];
$student_id = $row['student_id'];
$date = $row['created_on'];
$query_student_name = "SELECT f_name, l_name FROM students
WHERE student_id = {$student_id}";
$result1 = mysqli_query($connection, $query_student_name);
if(!$result1) {
die("Database query failed.");
} else {
$row1 = mysqli_fetch_assoc($result1);
$student_f_name = $row1['f_name'];
$student_l_name = $row1['l_name'];
}
}
}
Now i want to display the reviews somewhere down the page:
<!-- display reviews -->
<fieldset>
<legend>Reviews for <?php echo $name; ?></legend>
<br>
<?php
foreach($row as $value) {
?>
<legend><?php echo $student_f_name . " ". $student_l_name; ?> said:</legend>
<div id = "items" class="">
<?php echo $review_contents; ?>
</div>
<br>
<div>
<?php echo $date; ?>
</div>
<?php } ?>
</fieldset>
The above foreach loop doesn't work. It probably creates an infinite loop. I have tried using a for loop with $count as the maximum counter but that doesn't work either. Please suggest a for loop that prints all the reviews from the database.

try this
$get_review_query = "SELECT * FROM reviews WHERE institute_id = {$id}";
$result = mysqli_query($connection, $get_review_query);
if(!$result) {
die("Database query failed.");
}
$reviews = array();
$count = mysqli_num_rows($result);
if($count == 0) {
$output = "There're no reviews for this institute.";
} else {
while($row = mysqli_fetch_assoc($result)) {
$query_student_name = "SELECT f_name, l_name FROM students
WHERE student_id = {$student_id}";
$result1 = mysqli_query($connection, $query_student_name);
if(!$result1) {
die("Database query failed.");
} else {
$row1 = mysqli_fetch_assoc($result1);
$row['f_name'] = $row1['f_name'];
$row['l_name'] = $row1['l_name'];
}
$reviews[] = $row;
}
}
if( $reviews ){
?>
<fieldset>
<legend>Reviews for <?php echo $name; ?></legend>
<br>
<?php
foreach($reviews as $review) {
?>
<legend><?php echo $review['f_name'] . " ". $review['l_name']; ?> said:</legend>
<div id = "items" class="">
<?php echo $review['content']; ?>
</div>
<br>
<div>
<?php echo $review['created_on']; ?>
</div>
<?php } ?>
</fieldset>
<?php
}

Related

php pagination with search not working together?

This is the small minor project from college. It ranks the students as per the no of votes. It does rank the student in the pagination but when the student profile is clicked the rank is not shown.
Ok so the below code works but the last part of the code does not display the rank in the output. how can I $num in both codes?
HOME
<br><br>
<form>
<input type="text" name="id" autocomplete="off" placeholder="enter student id">
</form>
<br><br>
<?php include 'conn.php'; ?>
<?php
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);
$result_per_page = 5;
$no_of_result = mysqli_num_rows($result);
$no_of_page = ceil($no_of_result/$result_per_page);
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}
$page_first = ($page-1)*$result_per_page;
$sql = 'SELECT * FROM table ORDER BY votes DESC LIMIT ' . $page_first . ',' . $result_per_page;
$result = mysqli_query($conn, $sql);
$num = $page * $result_per_page -4;
while($row = mysqli_fetch_assoc($result)) {
echo '<div class="x">';
echo '#';
echo $num++;
echo ' ';
echo '<a href="?id='.$row['id'].'">';
echo $row['name'];
echo '</a>';
echo '</div>';
}
echo '<br>';
for($page=1;$page<=$no_of_page;$page++){
echo ''.$page.'';
echo ' ';
}
echo '<br>';
?>
<?php
$name = $_GET['id'];
$sql = "SELECT * FROM table WHERE id=$name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<br>';
echo 'RANK = ';
echo '?';//display rank here?
echo '<br>';
echo 'NAME = ';
echo $row['name'];
echo '<br>';
echo 'VOTES = ';
echo $row['votes'];
echo '<br>';
}
} else {
//echo "0 results";
}
?>
The output of the above code.. I want to output the rank in the place of question mark.
current output
i want something like this
desired output

Display row that is not repeated linking with database

I am new in php. I make a quiz app and I want to show questions that is not repeated again . here's my code.
Please help me to show require result.
<?php
include('connect.php');
$sql = "SELECT * FROM quiz_question WHERE theme_id= 2 ORDER BY RAND ()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
echo "
<h2>" . $row["question"]. "</h2>";
break;
}
}
$check_id = array ($row['id']);
echo $check_id['0'];
if(array ($row['id']) == $check_id){
echo "no question ";
}
else{
echo "
<h2>" . $row["question"]. "</h2>";
}
?>
Your question is not clear. But I guess, you can solve it by array_unique($array).
array_unique($array);
<?php
include('connect.php');
$sql = "SELECT * FROM quiz_question WHERE theme_id= 2 ORDER BY RAND ()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$question = $row['question'];
echo "
<h2>" . $row["question"]. "</h2>";
$check_id = array($id);
}
}
$check_id_unique=array_unique($check_id);
?>

PHP - While loop not showing table records

In below code it shows table row value outside while loop but not shows inside while loop. While loop not working; Please let me know whats wrong in it?
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id;";
$res = q($sql) or die(mysql_error());
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res));
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
^^^^
$res = mysql_query($sql) or die(mysql_error());
^^^^^^^^^^
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res));
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>
Try this:
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
$res = mysql_query($sql) or die(mysql_error());
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res))
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>

PHP/MySQL double loop

I am working on a directory where some of the listings have a images associated with them and others do not. I am wondering how I can write a loop within a loop to get my results.
Example, User selects state they want results from, query goes to DB requesting all listings in that state.
<?php
if (isset($_POST['searchButton'])) {
$state = $_POST['state'];
$query = "SELECT * FROM directory LEFT JOIN directory_images ON directory.id = directory_images.user_id WHERE directory.state = '$state' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo "<p>Sorry, there are no listings in '$state', check back soon!</p>\n";
}
else
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['id'];
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$has_support_pics = $row['file_name'];
?>
<h4><?php echo $name ?></h4>
<p><?php echo $address ?><br/>
<?php echo $city . ' ' . $state . ', ' . $zip; ?><br/>
</p>
<?php
// check to see if ID has extra images
if (isset($has_support_pics)) {
$query2 = "SELECT file_name FROM directory_images WHERE user_id = '$id'";
$result2 = mysql_query($query2) or die(mysql_error());
echo $query2.'<br/>';
?>
<ul class="support_images">
<?php
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
$support_image = $row['file_name'];
echo $support_image.'<br/>';
}
?>
</ul>
</div>
<br/>
</div>
<?php
}
echo "<hr/>";
}
}
?>
Do NOT run queries in loops - use a join.
Here is a tutorial: http://thewebmason.com/tutorial-parent-child-lists/

Filter results by Product Type and Sort List by Price (PHP/SQL)

I'm in the process of learning how to create an e-commerce site. I'm trying to figure out how to order product results by price. Code below is a menu list to either filter a specific product category or the option to view all products--the code for that works.
I'm not sure how to add code to "sort by price" (low to high, high to low).
I've tried creating a function called priceList and calling inside the function categoryList (which queries the database for which product type or to view all products), but does not work.
function priceList() {
$con = getConnection();
if($pUserCat == "lowToHigh") {
$sqlQuery = "SELECT Price from Products
ORDER BY Price ASC";
} elseif($pUserCat == "highToLow") {
$sqlQuery = "SELECT Price from Products
ORDER BY Price DESC";
// Execute Query -----------------------------
$result = mysqli_query($con, $sqlQuery);
if(!$result) {
echo "Cannot do query" . "<br/>";
exit;
}
$row = mysqli_fetch_row($result);
$count = $row[0];
if ($count > 0) {
echo "Query works" . "<br/>";
} else {
echo "Query doesn't work" ."<br/>";
}
// Display Results -----------------------------
$num_results = mysqli_num_rows($result);
for ($i=0; $i<$num_results; $i++) {
$row = mysqli_fetch_assoc ($result);
// print_r($row);
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['Image']).'" />';
echo "Price: " . stripslashes($row['Price']);
}
// Close connection
closeConnection($con);
}
Form
<!--category and price form ------------------------- -->
<form action="register_script.php" name="frm" method="post">
<select name="category" id="category">
<option value="viewall">View All</option>
<option value="dress">Dress</option>
<option value="athletic">Athletic</option>
<option value="sandals">Sandals</option>
</select>
<input type="submit" value="Go" />
</form>
<form action="register_script.php" name="frm" method="post">
<select name="price" id="price">
<option value="lowToHigh">Low to High</option>
<option value="highToLow">High to Low</option>
</select>
<input type="submit" name="orderPrice" value="orderPrice" />
</form>
</div>
PHP
<?php
function categoryList($pUserCat=false) {
echo "I am in category list" . "<br/>";
$con = getConnection();
$sqlQuery = "SELECT * from Products";
if($pUserCat == "athletic") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='athletic'";
} elseif ($pUserCat == "dress") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='dress'";
} elseif ($pUserCat == "sandals") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='sandals'";
} elseif ($pUserCat == "viewall") {
$sqlQuery = "SELECT * from Products";
}
// Execute Query -----------------------------
$result = mysqli_query($con, $sqlQuery);
if(!$result) {
echo "Cannot do query" . "<br/>";
exit;
}
$row = mysqli_fetch_row($result);
$count = $row[0];
if ($count > 0) {
echo "Query works" . "<br/>";
} else {
echo "Query doesn't work" ."<br/>";
}
// Display Results -----------------------------
$num_results = mysqli_num_rows($result);
for ($i=0; $i<$num_results; $i++) {
$row = mysqli_fetch_assoc ($result);
// print_r($row);
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['Image']).'" />';
echo "Price: " . stripslashes($row['Price']);
}
// priceList();
$priceOrder = getPriceBtn(); //$priceOrder holds value of option selected in
//price order menu
priceList($priceOrder);
// Close connection
closeConnection($con);
}
function getPriceBtn() {
//echo "<br/>"."I am calling getPriceBtn function" . "<br/>";
$priceBtn = $_POST["orderPrice"]; // price button
return $price;
//echo $priceBtn;
}
?>
I think you need to pass the $pUserCat to the function priceList($pUserCat);
A tip, you should have a default query like from high to low and if the other option is selected then run that query:
$sqlQuery = "SELECT Price FROM Products ORDER BY Price DESC";
if($pUserCat == "lowToHigh") {
$sqlQuery = "SELECT Price FROM Products ORDER BY Price ASC";
}
Also you could re-write this:
$sqlQuery = "SELECT * from Products"; // is this needed?
if($pUserCat == "athletic") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='athletic'";
} elseif ($pUserCat == "dress") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='dress'";
} elseif ($pUserCat == "sandals") {
$sqlQuery = "SELECT * from Products
WHERE ProductType='sandals'";
} elseif ($pUserCat == "viewall") {
$sqlQuery = "SELECT * from Products";
}
like this categoryList($pUserCat):
if($pUserCat != 'viewall') {
$sqlQuery = "SELECT * FROM Products WHERE ProductType='%s'";
$sqlQuery = sprintf($sqlQuery, $pUserCat);
} else {
$sqlQuery = "SELECT * from Products";
}
Local function variables are not shared. If a function variable is defined within a function, then a function that you call from within that function does not have access to the parent's local variables, which is the very reason they are called "local". You need to pass the variable as a parameter:
When defining:
function priceList( $pUserCat ) {
...
}
When calling:
priceList( $pUserCat );

Categories