I have this code which produces me a number which is equal to the number of id's i've got in my database of star rating system.
This code generates me a five star voting for each id i've got, but the problem is, it generates them all in a div, while i need them specifically in different div's. let's suppose i print out in a div information for each hostess i've got, i print out their photo and name with the following code:
$sql =" select * from hostess";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
echo "<div id='photo'>";
echo "<div id='picture'>";
echo "<div id='scotch'><img src='images/Scotch.png'></div>";
echo "<td> <img src=foto/photo1/".$row['photo'] . "></td>";
echo "</div>";
echo "<div id='text'>";
echo '<td>'. $row['first_name_en']." ". $row['family_name_en']."</td>";
echo "</div>";
echo "</div>";
echo "<div id='photo2'>";
echo "<div id='picture'>";
echo "<div id='notes'>";
echo '<form action="index.php" method="post" >';
echo "<label>Notes</label></br><textarea>".$row['courrent_occupation'] . "</textarea></br>";
echo '<input type="submit" value="edit" name="edit"></div>';
echo "</div>";
echo "<div id='notes'>";
echo "<label>profile</label></br><textarea>".$row['profile_en'] . "</textarea>";
echo "</div>";
echo "</div>";
}
?>
</div>
Now, i've got this other php which generates me all the star ratings for all hostess id's
<?php
// include update.php
include_once 'update.php';
// get all data from tabel
$arr_star = fetchStar();
?>
<?php
// start looping datas
foreach($arr_star as $star){ ?>
<h2>Star Rater - <?php echo $star['id'];?></h2>
<ul class='star-rating' id="star-rating-<?php echo $star['id'];?>">
<?php /* getRating($id) is to generate current rating */?>
<li class="current-rating" id="current-rating-<?php echo $star['id'];?>" style="width:<?php echo getRating($star['id'])?>%"><!-- will show current rating --></li>
<?php
/* we need to generate 'id' for star rating.. this 'id' will identify which data to execute */
/* we will pass it in ajax later */
?>
<span class="ratelinks" id="<?php echo $star['id'];?>">
<li>1</li>
<li>1.5</li>
<li>2</li>
<li>2.5</li>
<li>3</li>
<li>3.5</li>
<li>4</li>
<li>4.5</li>
<li>5</li>
</span>
</ul>
<?php } ?>
What i need is to assign each hostess profile i print their system rating.
I try to insert the foreach inside the first script but it then shows me just one profile, not all profiles.
The fetchstar() code is:
function fetchStar(){
$sql = "select * from `hostess`";
$result=#mysql_query($sql);
while($rs = #mysql_fetch_array($result,MYSQL_ASSOC)){
$arr_data[] = $rs;
}
return $arr_data;
}
First, you probably shouldn't use SELECT *. That aside I would combine the two queries you have to return a multidimensional array with MySQL and then use nested for each loops to echo out the data you want.
Someone answered a similar question for me here.
Looping through MySQL left join in php vs. 2 separate queries
$sql =" select * from hostess";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
if ($lastID <> $row['id']) {
$lastID = $row['id'];
$hostess[$lastID] = array('id' => $row['id'],
'first_name_en' => $row['first_name_en'],
etc
'arr_star' => array() );
}
$hostess[$lastID]['arr_star'][] = array('star_id' => $row['star_id'] etc);
}
Then you would use nested for each statements
for each($row as $rows){
//echo your hostess information
for each ($arr_star as $star){
//echo your star rating information
}
}
Related
I'm echoing this list from my mysql database, but I don't want bullets so I'm using bootstrap list-group-item. I feel like I'm making a really dumb mistake somewhere with my list tags but I'm not sure. I'm still getting the bullets next to my list. I'm not including all of my connecting to my database php because that's not the problem.
Here is what I have,
<div class="panel panel-info">
<div class="panel-heading">Contents</div>
<ul class="list-group">
<li class="list-group-item">
<?php
basic connect to mysql database stuff here
}
$query = mysqli_query($dat, "SELECT * FROM Content ORDER BY ContentName") or die(mysqli_error($dat));
while($list = mysqli_fetch_array($query)){
echo"<li>";
echo"<a href = >";
echo $list['ContentName'];
echo"</a>";
echo "</li>";
}
mysqli_close($dat);
?>
</li>
</ul>
</div>
you are adding li inside another li. it should be -
<ul class="list-group">
<?php
}
$query = mysqli_query($dat, "SELECT * FROM Content ORDER BY ContentName") or die(mysqli_error($dat));
while($list = mysqli_fetch_array($query)){
echo"<li class='list-group-item'>";
echo"<a href = >";
echo $list['ContentName'];
echo"</a>";
echo "</li>";
}
mysqli_close($dat);
?>
</ul>
Use echo "<li class='list-group-item'>"; instead of <li> and remove the <li> before php code.
<div class="panel panel-info">
<div class="panel-heading">Contents</div>
<ul class="list-group">
<?php
basic connect to mysql database stuff here
}
$query = mysqli_query($dat, "SELECT * FROM Content ORDER BY ContentName") or die(mysqli_error($dat));
while($list = mysqli_fetch_array($query)){
echo "<li class='list-group-item'>";
echo "<a href ='www.example.com' >";
echo $list['ContentName'];
echo "</a>";
echo "</li>";
}
mysqli_close($dat);
?>
</ul>
</div>
This is just a thought i have for my project is it possible to use a string for example an echo as parameter for select statement in database query?
here is the scenario:
For example when my page loads i automatically display the latest data in database say last five entry, as echo of course. and for each entry there are other details and in order to view those other details i will have an anchor for each echo and i will direct the user to another page..using the echo i want to query the database for other details..
Is this possible?
I know I cant put attribute name to echo so how is this possible?
Any suggestions?
Update
this is the page that will show the echo
<form method="POST" action="crud.php" enctype="multipart/form-data" >
<div class="headerimage"><img src="images/events.png"/></div>
<?php
foreach (LoadEvent() as $value){
echo '<div id="upcomingevent">';
//echo "<a href=\"#\" >".$value['searchresultwhat']."</a>";
echo "".$value['searchresultwhat']."";
echo "<input type='hidden' name=\"eventwhatcontent\" value=".$value['searchresultwhat'].">";
echo "<br/>\n";
echo $value['searchresultwhen'];
echo "<br/>\n";
echo $value['searchresultwhere'];
echo '</div>';
}
?>
</form>
then this is the method and the page where i must display the details of the event
<form method="POST" action="crud.php" enctype="multipart/form-data" >
<?php
echo '<div id="middlecontentupcomingevents">';
LoadSpecificEvent();
echo '<div id="upcomingeventcontentimage">';
echo "<img src=\"admin/".$events[4]."\">";
echo $events[0];
echo $events[1];
echo $events[2];
echo $events[3];
echo '</div>';
echo "<br/>\n";
echo '</div>';
echo '</div>';
?>
</form>
function LoadSpecificEvent(){
global $dbh;
//var_dump($dbh);
if (isset($_POST['eventwhatcontent'])) {
$eventwhat = $_POST['eventwhatcontent'];
$stmt = $dbh->prepare("SELECT * FROM events WHERE event_what = ?");
$stmt->bindValue(1,$eventwhat);
$stmt->execute();
$events = array();
$selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
$events[] = array(
'searchresultwhat' => $selected_row['event_what'],
'searchresultwhere' => $selected_row['event_where'],
'searchresultwhen' => $selected_row['event_when'],
'searchresultwho' => $selected_row['event_who'],
'searchresultimg' => $selected_row['img_url']
);
print_r ($events);
return $events;
}
}
I am having a problem with trying to show different menu options based on UserLevel. I have a mysql database with a users table. The users table contains a UserLevel which will either be set to 0 or 1. But for some reason my php just isn't working. In fact, when I add the php to the menu, it then does not display ANYTHING on the site below the menu. Any advice would be much appreciated.
Code that starts session
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
?>
<?php include "mainNav.php"; ?>
<center>
<h2> Campaign Updates</h2>
</center>
<div id="campaignPostWrap">
<div id="campaignScrollBox">
<?php
$con=mysqli_connect("localhost","dorians","ds2953!b67P$","aldentec");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM campaigns ORDER BY postDate desc");
while($row = mysqli_fetch_array($result))
{
echo "<div id='campaignPostContainer'>";
echo "<ul class='campaignPostBox'>";
echo "<p class='postInfo'>";
echo "Posted on:";
echo "<li>" . $row['postDate'] . "</li>";
echo "</p>";
echo "<p class='postInfo'>";
echo "Posted by:";
echo "<li>" . $row['postName'] . "</li>";
echo "</p>";
echo "<li class='postEntry'>" . $row['postEntry'] . "</li>";
echo "</ul>";
echo "</div>";
echo "<hr>";
}
mysqli_close($con);
?>
</div>
<?php include "campaignPost.php"; ?>
</div>
<?php include "chat.php"; ?>
<?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['EmailAddress'];
$userlevel = $row['UserLevel'];
$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['LoggedIn'] = 1;
$_SESSION['UserLevel'] = $userlevel;
echo "<h1>Success</h1>";
echo "<p>We are now redirecting you to the member area. If you are not automatically redirected <a href='index.php'>Click here</a></p>";
header( "refresh:10;url=index.php" );
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please click here to try again.</p>";
}
}
else
{
?>
Menu code that isn't working
<?php session_start(); ?>
<?php
$userlevel = $_SESSION['UserLevel'];
if($userlevel == 0) {
echo "<ul class="mainNav">
<li> Create Character</li>
<li> Create Quest</li>
<li> View Characters</li>
<li> View Quests</li>
<li> Book List</li>
</ul>";
} elseif($userlevel == 1) {
echo "<li> DM Tools</li>";
}
?>
<?php include "greeter.php"; ?>
Your quotes are undoubtedly the problem here:
if($userlevel == 0) {
echo "<ul class="mainNav">
<li> Create Character</li>
<li> Create Quest</li>
<li> View Characters</li>
<li> View Quests</li>
<li> Book List</li>
</ul>";
} elseif($userlevel == 1) {
echo "<li> DM Tools</li>";
}
Notice the syntax highlighting above shows the issue in your string. See how it turns black when it gets to mainNav? That's because mainNav is no longer part of the string. That's a bad thing here.
Look at the first line of your echo:
echo "<ul class="mainNav">
You open a quote and then close it at class=". Now, it's trying to evaluate mainNav as a constant or some other language construct. On top of that, it doesn't know what to do with mainNav as you haven't provided any kind of operators.
Instead, you should do something like:
if($userlevel == 0) {
echo '<ul class="mainNav">
<li> Create Character</li>
<li> Create Quest</li>
<li> View Characters</li>
<li> View Quests</li>
<li> Book List</li>
</ul>';
} elseif($userlevel == 1) {
echo '<li> DM Tools</li>';
}
Alternatively, you could escape every location where there is a non-string-terminating quote like \".
Another option would be to use Heredoc syntax.
I have a products page where 8 product images are in a list which is being populated by images stored in a MySQL database. The images all have associated ID's in which a price, product name, and description is also associated with the same ID.
The idea behind what I am trying to do is; When a user clicks on one of the 8 product images, they will be redirected to a "checkout" page which will display the same image, plus all the information that is also stored under that ID in the database.
As of right now, I have the checkout page URL including the ID of the image (url.com/checkout.php?id=1) and I was hoping to find a way to get all the information stored under that ID in the URL to be displayed where called on the page.
Here is my php code that displays the images in the list on the products page:
// Grab the data from our template table
$sql = "select * from templates";
$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result))
{
echo "<li>";
echo "<a class=\"caption\" href=\"purchase.php?id=\"" . $row['id'] . ">";
// Note that we are building our src string using the ID from the database
echo "<img src=\"http://URL-REMOVED.com/file_display.php?id=" . $row['id'] . "\" />";
echo "<span>";
echo "<big>" . $row['name'] . "</big>";
echo $row['description'];
echo "</span>";
echo "</a>";
echo "</li>";
}
Here is the php code that is supposed to gather the information of the clicked product (but doesn't):
if (isset($_GET['id']))
$id=$_GET['id'];
else
$id=1;
if (isset($_GET['action']))
$action=$_GET['action'];
else
$action="empty";
switch($action){
case "add":
if($_SESSION['cart'][$id])
$_SESSION['cart'][$id]++;
else
$_SESSION['cart'][$id]=1;
break;
case "remove":
if($_SESSION['cart'][$id])
{
$_SESSION['cart'][$id]--;
if($_SESSION['cart'][$id]==0)
unset($_SESSION['cart'][$id]);
}
break;
case "empty":
unset($_SESSION['cart']);
break;
}
//Display Cart
if(isset($_SESSION['cart'])) {
$total=0;
foreach($_SESSION['cart'] as $id => $x) {
$result=mysql_query("select image,name,price,description from templates WHERE id=$id");
$myrow=mysql_fetch_array($result);
$image=$myrow['image'];
$name=$myrow['name'];
$price=$myrow['price'];
$description=$myrow['description'];
}
}
And here is the actual HTML/PHP where the information is supposed to be displayed:
<a class="caption" href="checkout.php">
<img src="http://URL-REMOVED.com/file_display.php?id=<?php $myrow['id'] ?>"/>
<span>
<big>
<strong><?php $myrow['name'] ?></strong>
</big>
<div class="price"><?php $myrow['price'] ?></div>
</span>
</a>
</div>
<div id="info_form_container">
<div class="product_info">
<div class="control-group">
<strong>Template Name:</strong>
<?php $myrow['name'] ?>
</div>
<div class="control-group">
<strong>Template Description:</strong>
<?php $myrow['description'] ?>
</div>
<div class="control-group">
<strong>Template Price: </strong>
<?php $myrow['price'] ?>
</div>
</div>
I guess I'm not really sure if this is even the best method to take? But I definitely want to have the images stored in the database and I definitely want to call them using the ID...
How can I achieve this? Where am I wrong in my code?
EDIT: I do not think I understood your question the first time. Try using this to generate your list of products, updating the connection information. If this works, use the methods below to sanitize your variables and store your connection information elsewhere
<?php
$mysqli_connection = new mysqli("localhost", "username", "password", "database");
$sql = "SELECT * FROM templates";
$result = $mysqli_connection->query($sql);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
echo '<li>';
echo '<a href="purchase.php?id='.$id.'" class="caption">';
echo '<img src="http://URL-REMOVED.com/file_display.php?id='.$id.'" />';
echo '<span>';
echo '<big>'.$name.'</big>';
echo $description;
echo '</span>';
echo '</a>';
echo '</li>';
}
?>
OG Answer:
I think what you are doing is just fine.. It is the method I use (although mine seems a little neater). Run your PHP functions to query your MySQL database using the ID you get, and just start dumping the information you get into your site. To help with readability and cut down on spelling confusions, it might help to assign your $myrow['whatever'] results into variables to echo out, but that is more cosmetic to me than anything.
To fix up your MySQL things and use mysqli, try out the following:
$sql = "SELECT * FROM templates";
$result = $mysqli_connection->query($sql);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
echo "<li>";
echo "<a class=\"caption\" href=\"purchase.php?id=\"" . $row['id'] . ">";
// Note that we are building our src string using the ID from the database
echo "<img src=\"http://URL-REMOVED.com/file_display.php?id=" . $row['id'] . "\" />";
echo "<span>";
echo "<big>" . $row['name'] . "</big>";
echo $row['description'];
echo "</span>";
echo "</a>";
echo "</li>";
}
And try to sanitize your information using:
$my_stuff = mysqli_connection->real_escape_string($row['that_stuff']);
Also, you know you can use single quotes ('') around your echo statements if you want to, right? May make it easier that escaping all of the double quotes..
So in full, this is probably a rough example of what I would do but I would split it up into functions and maybe create some global variables (such as the connection itself):
<?php
$mysqli_connection = new mysqli("localhost", "username", "password", "database");
$sql = "SELECT * FROM templates WHERE id=$id";
$result = $mysqli_connection->query($sql);
// I'm assuming there is only one entry, so no while loop for me
$row = $result->fetch_array(MYSQLI_ASSOC);
$your_title = $mysqli_connection->real_escape_string($row['title']);
$path_to_image = $mysqli_connection->real_escape_string($row['image']);
$description = $mysqli_connection->real_escape_string($row['description']);
$price = $mysqli_connection->real_escape_string($row['price']);
?>
<html>
<head></head>
<body>
<h3><?php echo $your_title; ?></h3>
<img src="<?php echo $path_to_image; ?>" />
<ul>
<li>Description: <?php echo $description; ?></li>
<li>Price: <?php echo $price; ?></li>
<!-- etc..-->
I need to do the following - get the list of all artists from the database and print them on a page. But besides of that, I need to make another query to the "albums" table and get the albums of each artist and print the images under each artist description. The code I've written is as follows:
require('db.php');
$query = "SELECT * FROM artists";
$result = mysql_query($query);
$artist_id = $result[id];
$artist_name = $result[name];
$artist_surname = $result[surname];
$artist_email = $result[email];
$artist_about = $result[about];
$artist_photo = $result[photo];
while(list($artist_id, $artist_name, $artist_surname, $artist_email, $artist_about, $artist_photo) = mysql_fetch_row($result)) :
print "<div class='row'>";
print "<div class='artists_left'>";
print "<div class='gallery_cont'><a href='#'><img src='timthumb.php?src=images/artists/$artist_photo&w=240&h=318' alt='$artist_name' /></a></div>";
print "</div>";
print "<div class='artists_right'>";
print "<div class='artist_title_cont'><span class='model'>Художник:</span><span class='name'>$artist_name $artist_surname</span><span class='mypage'>Личная почта:</span><a href='#' class='mypage'>$artist_email</a></div>";
print "<div class='artist_title_cont' style='margin-top:20px;'><span class='name'>$artist_about</span></div>";
print "<ul class='artists'>";
$albums_query = "SELECT id, title_photo, dirname FROM albums WHERE master = $artist_id";
$albums_result = mysql_query($albums_query);
$album_id = $albums_result[id];
$album_photo = $albums_result[title_photo];
$album_dirname = $albums_result[dirname];
while(list($album_id, $album_photo, $album_dirname) = mysql_fetch_row($result)) :
print "<li><a href='#'><img src='galleries/$album_dirname/$album_photo' alt='image' /></a></li>";
endwhile;
print "</ul>";
print "<a href='#' class='seemore_portfolio'>все работы мастера ></a>";
print "</div>";
print "</div>";
endwhile;
mysql_close($connect);
The outer query works fine, but the inner one - does not. Could anybody help me to figure this out?
Thanks in advance.
Change the second $result to $albums_result
You can also consider writing the whole thing like this...
<?
require('db.php');
$query = "SELECT * FROM artists";
$result = mysql_query($query);
while(list($artist_id, $artist_name, $artist_surname, $artist_email, $artist_about, $artist_photo) = mysql_fetch_row($result))
{
$li = '';
$albums_query = "SELECT id, title_photo, dirname FROM albums WHERE master = $artist_id";
$albums_result = mysql_query($albums_query);
while(list($album_id, $album_photo, $album_dirname) = mysql_fetch_row($albums_result))
{
$li .= "<li><a href='#'><img src='galleries/$album_dirname/$album_photo' alt='image' /></a></li>";
}
echo "<div class='row'>
<div class='artists_left'>
<div class='gallery_cont'><a href='#'><img src='timthumb.php?src=images/artists/$artist_photo&w=240&h=318' alt='$artist_name' /></a></div>
</div>
<div class='artists_right'>
<div class='artist_title_cont'><span class='model'>Художник:</span><span class='name'>$artist_name $artist_surname</span><span class='mypage'>Личная почта:</span><a href='#' class='mypage'>$artist_email</a></div>
<div class='artist_title_cont' style='margin-top:20px;'><span class='name'>$artist_about</span></div>
<ul class='artists'>
$li
</ul>
<a href='#' class='seemore_portfolio'>все работы мастера ></a>
</div>
</div>";
}