I'm designing a website for a neighbor for a potential restaurant he wants to open. I need to create a page for testimonials/review. I'd like to code it so that the number of stars is in a SQL, I'm just not sure how to do it.
Essentially what I'm looking for is a way to determine the integer of the ratings field (1-5) and for each number, display a star image.
So:
if rating = 1, display star.png once
if rating = 2, display star.png twice
...
if rating = 5, display star.png five times
I'm just not sure how to write the code to do so.
The website is being written in PHP and CSS. I'd like to avoid JQuery, Javascript, .NET, and so forth, as I'm not as familiar with them and will be the one keeping the site up to date.
Here's what I've got so far, but it's not working right, and I get a syntax error:
$result = mysqli_query($con,"SELECT * FROM Reviews");
while($row = mysqli_fetch_array($result))
{
IF (Rating = "1"()){
echo '<img src="star.png">' . }
ELSE IF (Rating = "2"()){
echo '<img src="star.png"><img src="images/star.png">' . }
Else IF (Rating = "3"()){
echo '<img src="star.png">star.png"><img src="images/star.png">' . }
ELSE IF (Rating = "4"()){
echo '<img src="star.png"><img src="images/star.png">star.png"><img src="images/star.png">' . }
ELSE
echo '<img src="star.png"><img src="images/star.png">star.png"><img src="images/star.png"><img src="images/star.png">' .
"<br/> <b>" .
$row['Name'] .
"</b> <em>" .
$row['City'] . $row['State'] . $row['Country'] . "</em><br/>" .
$row['Review'] . "<br/> <hr> <br/>"
}
?>
Use a select statement to get the ratings for a place from your database.
Store the result in a php variable (lets call it $rating)
Use php logic to output number of stars (in html obviously) based on value of $rating.
Hope that helps :)
I would recommend 3 tables for this idea.
Users Table
UserRatings Table
Dish Table
Users table is used to store just that. User information. Possibly a username, password, first name, last name for example. The table should have a primary key. Call it UsersID. It should auto increment itself and be unique for every row.
The Dish table is next. Put a dish name in it. It should have a primary key as well. Call it DishID.
Lastly is the UserRatings table will store UserRatingsId, Rating, InsertTimeStamp, UpdateTimeStamp.
Use a loop to output your HTML based on your rating.
$rating = 4; //Figure this out in your script and set accordingly
for($i = 0; $i < $rating; $i++) {
echo '<img src="star.png" />';
}
Should print out four stars for you.
Help from a friend:
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$starCount = 0;
$result = mysqli_query($con,"SELECT * FROM Reviews");
while($row = mysqli_fetch_array($result)) {
$starCount = $row['Rating'] ;
while ($starCount > 0) {
echo '<img src="images/star.png">';
$starCount -- ;
}
$starCount = 0;
echo "<br/> <b>" . $row['Name'] . "</b> - <em>" .
$row['City'] .", ". $row['State'] ." ". $row['Country'] . "</em><br/>" .
$row['Review'] . "<br/> <hr> <br/>" ;
}
?>
$number=$row->rating ;
$middle="";
$first="<td width='200' align='left'>";
for($x=1;$x<=$number;$x++) {
$middle=$middle.img($fullimage_properties);
}
if (strpos($number,'.')) {
$middle=$middle.img($halfimage_properties);
$x++;
}
while ($x<=5) {
$middle=$middle.img($blankimage_properties); ;
$x++;
}
echo $last=$first.$middle."</td>";
Related
I want to display 9 different tables from my sql database in 9 different html created tables on the website.
In detail: I have 9 tables ("dt_bookmarks_01", "dt_bookmarks_02" etc.) with 4 columns "id" (which is primary and auto increment), icon (for favicon), link (url) and text (for the display text).
I've created 9 different html tables with bootstrap and want to output the content of each table in a different bootstrap table of my site.
My problem is that i have no idea how to get different "foreaches" or counter for each different table.
To automaticaly add new rows to the bootstrap table I use the count and foreach function. problem here is: I dont know how to seperate them from each other. If i have 4 entries in sql table 1 it multiplies the one and only entrie of sql table 2 to match the current count of 4.
I am very new to sql and php so I guess I just miss some fundamental functions or something.
document header:
php
$sql = "
SELECT *
FROM dt_bookmarks_01, dt_bookmarks_02";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
$conn->close();
and for the html table I use:
php
<tbody>
<!--begin: SQL Selection -->
<?PHP
$count = 0;
foreach($rows as $item){
if (!empty($item['icon'])) {
$icon = '<img src="assets/media/bm-icons/'. $item['icon'] . '">';
}else{
$icon = '<img src="assets/media/bm-icons/default.png">';
}
$count++;
echo "<tr>";
/*echo "<td>" . $count . "</td>";*/
echo "<td> " . $icon . "</td>";
echo "<td> <a href=\"" . $item['link'] . "\"'>" . $item['text'] . "</a> </td>";
echo "<td></i> ";
echo "</i></td>";
echo "</tr>";
}
?>
<!--end: SQL Selection -->
</tbody>
I do not have a database on hand to give you an answer with complete code, but here is the idea:
<?php
for ($i = 1; $i <= 9; $i++)
{
$query = "SELECT index1,index2 FROM dt_bookmarks_0$i";
echo "<h1>This is the content of table $i</h1>";
# RUN THE QUERY HERE !!!
echo "<table>";
# EXTRACT THE RESULTS
foreach $rows as $item
{
echo "<tr><td>$item[index1]</td><td>$item[index2]</td></tr>"
}
echo "</table>";
echo "<br><br>";
}
?>
Loop on your tables.
In each table loop, you output the HTML code to display it's content.
Avoid SELECT *, specify your indexes (research "sql why avoid SELECT *")
So you loop twice. One time to go through the tables, the other to loop on the results.
so here is the new working code.
header:
<?PHP
require_once('/htdocs/_nt/mysql/data.php');
$sql = "
SELECT *
FROM dt_bookmarks";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
$conn->close();
?>
and for the table output:
<?PHP
$count = 0;
foreach($rows as $item){
if ($item['category'] == talk) {
$count++;
echo "<tr>";
echo "<td> " . $icontalk . "</td>";
echo "<td> <a href=\"" . $item['url'] . "\"'>" . $item['text'] . "</a> </td>";
echo "</tr>";
}else{
echo "";
}
}
?>
I have this piece of code below. It displays the image and name of all the entries in a table in my database. The name is set up to become a hyperlink.Is it possible to make it so when one specific name is clicked that data for only that specific name will be displayed on the page you are sent to?
So for example if I select the first entry that is displayed back "mealname1" and it takes me to the showrecipe.php page, can I make it so I can display all the data I have for "mealname1" and only "mealname1". I'm really lost, I have scoured the internet and my php books but can't find anything to that is relevant.
If there is no way of doing it is there an obvious solution that I am missing?... I am very much a novice to this... thanks for your help guys.
<?php
require("db.php");
$prodcatsql = "SELECT * FROM recipes";
$prodcatres = mysql_query($prodcatsql);
$numrows = mysql_num_rows($prodcatres);
if($numrows == 0)
{
echo "<h1>No Products</h1>";
echo "There are no recipes available right now.";
}
else
{
echo "<table id='recipetable'>";
while($prodrow = mysql_fetch_assoc($prodcatres))
{
echo "<tr>";
if(empty($prodrow['image'])){
echo "<td><img
src='./images/No_image.png' alt='"
. $prodrow['mealname'] . "'></td>";
}
else {
echo "<td><img src='./images/".$prodrow['image']
. "' alt='"
. $prodrow['mealname'] . "'></td>";
}
echo "<td>";
echo ''.$prodrow['mealname'].'';
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Change the query to
SELECT * FROM recipes WHERE mealname='$mealname' LIMIT 1;
You can remove "LIMIT 1" if you want but this makes sure you will only get 1 or 0 row back. Don't forget to escape the string.
Basically I have a ranking-page that I'm working on for my CS:GO server. So far it's working fine, and it's looking like this:
https://puu.sh/aJlTj/b6fb5a7a06.png
And the next-page arrow(s) works fine, but the problem is, at page 3+, it's empty because there haven't been more players on the server yet, and it looks like this:
https://puu.sh/aJlXH/7e69071a2b.png
I am using the answer from Make multiple pages out of a mysql query in order to create the pages for all the players.
My table-printing-part looks like this:
echo "<div class='TableGen'><table border='1'><tr><td>Name</td><td>Wins</td><td>Losses</td><td>ELO</td></tr>";
$query = mysqli_query($db, "SELECT * FROM multi1v1_stats ORDER BY wins DESC LIMIT $offset,15");
while($row = mysqli_fetch_array($query)) {
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['wins'] . "</td><td>" . $row['losses'] . "</td><td>" . $row['rating'] . "</td></tr>";
}
?>
</table>
$offset is declared by using $offset = 15 * intval($_GET['page']); unless it's the first page, then it's set to 0.
I understand that this code will only echo out <tr><td></td></tr>'s as long as there's data to post, so how would I go about checking if the page's cells are empty, then just echo out empty cells? (due to this, I can't use empty-cells: show; either as there are no cells to show, not even empty ones)
Thanks in advance.
I think this is what you want... It basically finds the number of rows that were returned and then continues with blanks until the limit is reached.
$limit = 15;
echo "<div class='TableGen'><table border='1'><tr><td>Name</td><td>Wins</td><td>Losses</td><td>ELO</td></tr>";
$query = mysqli_query($db, "SELECT * FROM multi1v1_stats ORDER BY wins DESC LIMIT $offset,$limit");
$i=0;
while($row = mysqli_fetch_array($query)) {
$i = $i+1;
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['wins'] . "</td><td>" . $row['losses'] . "</td><td>" . $row['rating'] . "</td></tr>";
}
for($i=$i;$i<=$limit;$i++)[
echo "<tr><td> </td><td> </td><td> </td><td> </td></tr>";
}
?>
</table>
I'm executing this type of code inside a table.
while($row = mysql_fetch_array($result))
{
echo "" . $row['id'] . "";
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
}
Which works great and gives me a numbered list (1 Mike Volvo, 2 Mike Ford) for example. The problem is that I now have multiple users in the same table so there are gaps in the list where someone else's entry is since i'm using auto incremented ID for numbers.
So assuming I have 5 entries I obviously want it to be 1-5 but right now its 5-30-45-65 or whatever.
Anyway I looked around for a solution and I found $number = 1; $number++; to be effective and it kind of works and gives me a 1-5 list independent of whatever the ID's are.
The problem is that when I reverse the list using ORDER BY xxx desc the last entry becomes 1 and the first entry becomes 5. But I always want the first entry to remain 1 and the last entry to be 5.
Do you have any ideas on how to create this function using PHP?
You need to simple user $number as you mentioned as in the following code
$number = 1;
while($row = mysql_fetch_array($result))
{
echo $number;
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
++$number;
}
Even if in your SQL you have order by it does not matter in that case.
From what I understand of your question... You are on the right track using the following code...
$LastId ='';
$number = 0;
while($row = mysql_fetch_array($result))
{
if($LastId != $row['id']){
$number++;
}
echo $number;
echo "" . $LastId = $row['id'] . "";
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
}
So...
If the 'id' isn't the same as the last 'id' (not the same user) it will increment it by 1. Otherwise it will stay the same.
Hope this is what you looking for and helps.
Although this is pretty trivial in php, I would not use php for it. You can easily do this in html:
echo "<ol>";
while($row = mysql_fetch_array($result))
{
echo "<li>" . $row['name'] . $row['car'] . "</li>";
}
echo "</ol>";
I have a drop down menu, which is filled from a database. When I select a value in the menu it displays a table of the data selected from the database. I'd like to change this to an HTML5 range slider. So far with no luck. I also want to show the values (dates) beside the range as I move along it.
This is the code to the drop down menu:
// Set SQL string
$query = "SELECT * FROM Test";
// Execute SQL
$result = mysql_query($query);
// Find number of rows in the resulting recordset array
$num = mysql_numrows($result);
// Initialise loop counter
$i = 0;
echo ("<form><select name='users' onchange='showUser(this.value)'>");
// Loop through recordset until end
while ($i < $num) {
// Associate variables for result at position i at table location specified
$Time = mysql_result($result, $i, "Time");
// Echo each entry as an OPTION for the Select List
echo ("<option value=\"$Time\">$Time</option>");
// Increment Loop Counter
$i++;
}
echo ("</select></form><br>");
gettime.php:
$sql = "SELECT * FROM Test WHERE Time = '" . $q . "'";
$resultb = mysql_query($sql);
if (!$resultb) {
echo "<p>The following SQL failed</p><p>" . $sql . "</p>";
}
echo "<table border='1'>
<tr>
<th>Time</th><th>First PC Room</th>
<th>First Group Study Room 1</th>
<th>First Group Study Room 2</th>
<th>First Main Room</th>
</tr>";
while ($rowb = mysql_fetch_array($resultb)) {
$bmsTime = $rowb['Time'];
//Convert Excel Timestamp of DB to Unix Timestamp
$unixtime=($bmsTime-25569)*86400;
$readable=date('l jS \of F Y h:i:s A',($unixtime));
echo "<tr>";
echo "<td>" . $readable . "</td>";
echo "<td>" . $rowb['firstPCroom'] . "</td>";
echo "<td>" . $rowb['firstGrpStdyRm1'] . "</td>";
echo "<td>" . $rowb['firstGrpStdyRm2'] . "</td>";
echo "<td>" . $rowb['firstmainroom'] . "</td>";
echo "</tr>";
}
echo "</table>";
Below is what I have so far on the "slider":
echo "<input id='slider' type='range' min='0' max=\"$num\" step='any' />
<span id='range'> </span>";
?>
<script>
var selectmenu=document.getElementById("slider");
var colorchange;
selectmenu.onchange=function changecolour(){
if (selectmenu.value<"0.5")
{colorchange=0}
else if (selectmenu.value>="0.5") {colorchange=Math.round(selectmenu.value)}
document.getElementById("range").innerHTML=colorchange;
}
</script>
Any help would be greatly appreciated! Thanks
Here is a jsFiddle that updates a table row using jQuery. Presumably you would replace the data with an AJAX call.
If you were a little more specific about where exactly you were having trouble, someone else may be able to tailor a solution that better fits your needs.