What I want is to be able to click on the link (review.php) and then to display the review corresponding to that film only, on a new page.
Not sure if my MySQL query is wrong or if my A href link is not formatted correctly.
Any help would be appreciated.
require_once('./includes/mysql_connect.php');
$query = "SELECT films.movie_title, films.rating, films.actor, reviewed.review
FROM films
INNER JOIN reviewed
ON films.movie_id=reviewed.review_id";
$result = mysql_query($query) or die ("Could not execute mysql" . mysql_error()); // Run Query
$num = mysql_numrows($result);
if ($num > 0) { // If it ran ok, display records.
echo "<p> There are curently $num records.</p>";
// Table header.
echo '<table border="1" align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b>Movie Title</b></td>
<td align="left"><b>Leading Actor</b></td>
<td align="left"><b>Rating</b></td>
<td align="left"><b>Review</b></td>
</tr>';
// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['movie_title'] . '</td>
<td align="left">' . $row['actor'] . '</td>
<td align="left">' . $row['rating'] . '</td>
<td align="left"><a href="review.php?id='. $row['review'] . '> Read Review </a>
</tr> ';
}
}
?>
Related
So i have this code:
<?php
require_once('../../ccconnect.php');
$query = "SELECT * FROM orgs";
$response = #mysqli_query($dbc, $query);
if($response){
echo '<table align="left" cellspacing="5" cellpadding="8">
<tr>
<td align="left"> <b>Logga</b> </td>
<td align="left"> <b>Namn</b> </td>
<td align="left"> <b>Andel till ändamål</b> </td>
<td align="left"> <b>Vinst</b> </td> </tr>';
while($row = mysqli_fetch_array($response))){
echo '<tr>
<td align="left">' . $row['logo'] . '</td>
<td align ="left">' . $row['name'] . '</td>
<td align ="left">' . $row['chare'] . '</td>
<td align ="left">' . $row['profit'] . '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "Kunde inte genomföra databaskomandot<br>";
echo mysqli_error($dbc);
}
?>
The code will print out a table of results from my database, but i want to limit the results i get to only show 5 results for example. How would i do this?
Use the LIMIT clause in SQL.
SELECT * FROM my_table
LIMIT 5;
https://dev.mysql.com/doc/refman/5.5/en/select.html
http://www.w3schools.com/php/php_mysql_select_limit.asp
You need to limit the query or limit the loop. (I like the first option better).
You have this query:
$query = "SELECT * FROM orgs";
Wanna get 5 results only?
$query = "SELECT * FROM orgs LIMIT 5";
page1, the code, below, works fine and lists recipe names i.e. BBQ Pork etc
<form action="recipe_show.php" method="post">
<?php
$result = mysql_query("SELECT recipe_name FROM recipes ORDER BY recipe_name ASC"); // Run the query
if ($result) { // If it ran OK, display the records
// Table header
echo '<table>
<td align="left"><b>Recipe Name</b></td>
</tr>';
// Fetch and print all the records
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td>'. $row['recipe_name'] .'</td>
</tr>';
}
echo '</table>'; // Close the table
mysql_free_result ($result); // Free up the resources
} else { // If it did not run OK
// Message
echo '<p class="error">The current recipe_name could not be retrieved. We apologize for any inconvenience.</p>';
// Debugging message
echo '<p>' . mysql_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
} // End of if ($result)
mysql_close($dbcon); // Close the database connection.
?></p>
</form>
</div>
When I click the link, from page1, it opens page2. I get the headings but not the data for the BBQ Pork link. Below is the code on page2.
<form action="" method="post">
<?php
$recipe = mysql_real_escape_string($_GET['recipe_name']); //if using mysql
$myresult = "SELECT * FROM recipes WHERE recipe_name = '".$recipe. "'";
$num = mysql_num_rows($myresult);
for ($i = 0; $i < $num; $i++){
$row = mysql_fetch_array($myresult, MYSQL_ASSOC);
$row = (($i % 2) == 0) ? "table_odd_row" : "table_even_row";
echo "<tr row=".$row.">";
}
if ($myresult) { // If it ran OK, display the records
echo '<table>
<td width="250" align="center"><b>Recipe Name</b></td>
<td width="250" align="left"><b>Instructions</b></td>
<td width="250" align="left"><b>Directions</b></td>
<td width="250" align="left"><b>Notes</b></td>
</tr>';
while ($row = mysql_fetch_array($myresult, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['recipe_name'] . '</td>
<td align="left">' . $row['ingredients'] . '</td>
<td align="left">' . $row['directions'] . '</td>
<td align="left">' . $row['notes'] . '</td>
</tr>';
}
echo '</table>'; // Close the table
mysql_free_result ($myresult); // Free up the resources
} else {
echo '<p row="error">The current Recipe could not be retrieved. We apologize for any inconvenience.</p>';
echo '<p>' . mysql_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
} ($myresult)
mysql_close($dbcon); // Close the database connection.
?>
Hope I have correctly set this out? Many thanks, in advance, for help and guidence
I am trying to print out on my sites home page a horizontal table which includes a picture of a car and then below the make, model & price of the car.
Here's roughly what I have done:
<?php
$List = "";
$sql = mysql_query("SELECT * FROM Car ORDER BY listed DESC LIMIT 5");
$Count = mysql_num_rows($sql);
if ($Count > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$make = $row["make"];
$model = $row["model"];
$price = $row["price"];
$List .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="20%" valign="top"><a href="/motors/cars.php?id=' . $id . '"><img style="border:#666 1px solid;"
src="/../../motors-' . $id . '.jpg" alt="' . $status . ' ' . $title . '" width="100" height="80" border="1" /></a></td>
</tr>
<tr>
<td width="80%" valign="top" align="left"><font face="Arial" color="#000080"><B>
' . $make . ' ' . $model . '</B></font><br />' . $price . '
view car details</align></td>
</tr>
</table>';
}
} else {
$List = "No cars at present";
}
mysql_close();
?>
Can anyone help me sort this code to print out horizontally? Many Thanks!
The src of the image is most likely wrong, /../../motors... is still /motors...
you can move the images into your webapps /motors/images folder and set src as /motors/images/motors...
The table you create must be nested in another table.
<table>
<tr>
<td>
<table of a car>
</td>
<td>
<table of a car>
</td>
<td>
<table of a car>
</td>
.... etc.
</tr>
</table>
It will make sense to emit a tr every few cars to wrap to a new line.
table of a car is the html you collect in $List.
My idea is to create a Q&A section under a product profile, just like on eBay or Amazon or whatever. The idea is to send a question and then get the owner of the article to reply.
The table has these columns: pid (product ID), id (question ID), question, answer, date (date posted), username.
So if I post a question, I get the ID of the product in which I'm posting and create a question. The the owner just sends the answer to the row that matches the question.
Here's my PHP code to retrieve all the info from that table:
$qanda = '';
$link = mysql_connect("localhost", "youknowwhat", "youknowwhat");
mysql_select_db("youknowwhat", $link);
$qandaq = mysql_query("SELECT * FROM questions WHERE id='$id2' ORDER BY date", $link);
$count = mysql_num_rows($qandaq);
if($count >= 1){
while($rows = mysql_fetch_array($qandaq)){
$date = $rows['date'];
$q = $rows['question'];
$a = $rows['answer'];
$usrname = $rows['username'];
}
$qanda .= '<div id="answers" align="center">
<table cellspacing="0" align="center">
<tr align="center">
<td width="200">' . $date . '</td>
<td rowspan="2" width="400"><strong>' . $q . '</strong><br>' . $a . '</td>
<td width="200">Delete</td>
</tr>
<tr align="center">
<td>' . $usrname . '</td>
<td>Report</td>
</tr>
</table>
</div>';
} else {
$qanda = '<div id="answers" align="center">
No questions for this product.
</div>';
}
Now... what you see as a table in the variable $qanda I want to repeat it over and over again but displaying different row data but the concatenation isn't working and I can only get the last row to be displayed. I just can't seem to find out why this isn't working! Am I missing something?
All you have to do is append your divs (.=) while you're inside the while loop that mysql_fetch_array() rows.
Then you'll have a new div for each row your database returns, and you can populate it easily.
$qanda = '';
while($rows = mysql_fetch_array($qandaq)){
$date = $rows['date'];
$q = $rows['question'];
$a = $rows['answer'];
$usrname = $rows['username'];
$qanda .= '<div id="answers" align="center">
<table cellspacing="0" align="center">
<tr align="center">
<td width="200">' . $date . '</td>
<td rowspan="2" width="400"><strong>' . $q . '</strong><br>' . $a . '</td>
<td width="200">Delete</td>
</tr>
<tr align="center">
<td>' . $usrname . '</td>
<td>Report</td>
</tr>
</table>
</div>';
}
I have a page where it displays a movie review, that is displayed by a link Read Review on this same page I would also like to display the movie title. So do I have to use a join to display the information or is there a simpler way of doing it ? I have tried using a few queries but I end up getting all the film titles for the one review.
This is where the query first starts on the film.php
$query = "SELECT films.movie_title, films.rating, films.actor, reviewed.review,
users.username
FROM films
LEFT JOIN reviewed ON films.movie_id=reviewed.movie_id
LEFT JOIN users ON films.user_id=users.user_id";
$result = mysql_query($query) or die ("Could not execute mysql" . mysql_error());
$num = mysql_num_rows($result);
if ($num > 0) { // If it ran ok, display records.
echo "<p> There are curently $num records.</p>";
// Table header.
echo '<table border="1" align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b>Movie Title</b></td>
<td align="left"><b>Leading Actor</b></td>
<td align="left"><b>Rating</b></td>
<td align="left"><b>Author</b></td>
<td align="left"><b>Review</b></td>
</tr>';
// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['movie_title'] . '</td>
<td align="left">' . $row['actor'] . '</td>
<td align="left">' . $row['rating'] . '</td>
<td align="left">' . $row['username'] . '</td>
<td align="left"> Read Review
</tr> ';
}
}
?>
Now on this page is displays the review but I would like to display the movie_title within the sub_review container.
<body>
<div id="sub_review_container">
<?php
?>
</div>
<div id="Review_container">
<?php
$review = $_GET['id'];
echo $review;
?>
</div>
</body>
You are missing the id of the review in the query of which you want to show the review.
Also because of left join. It is showing all records. You should use inner join for reviews and films. It should be like:
$query = "SELECT films.movie_title as movie_title, films.rating, films.actor, reviewed.review, users.username FROM films INNER JOIN reviewed ON films.movie_id=reviewed.movie_id INNER JOIN users ON films.user_id=users.user_id where reviewed.id = ... "
...reviewed.id will be the Id you pass from page.