I'm not sure how well i'll be able to explain this, but here goes.
I have a website for attractions. Let's say that one of my categories is Historical villages.
When the user opens the Historical villages page he gets a list of villages displayed from the database. The way I display them is: Name plus a picture of the attraction.
What I want to do is unable the user to click on of the villages (by making the name and picture a clickable link) and the user to be redirected to a page that will run a php script that will display more information from the database about the selected village. That way I will only have one page for all attractions that will display different information every time a user selects something different, instead of hardcoding all the pages.
This is my code displaying the lits of villages:
$sql = "SELECT `Name`, `Location`, `Description`, `Airport`, `imglink`, `pagelink` "
. "FROM `attractions` "
. "WHERE `Category`='HistV'";
$result = mysql_query($sql, $link);
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row['Name'];
echo "<img src='" . $row['imglink'] . "'>";
}
Do any of you have any suggestions on how to make this output a link and the make it run the PHP to show the users selection?
Your while condition changed to like this,
while ($row = mysql_fetch_assoc($result)) {
/* For example ,
$row['pagelink'] must contains the pagelink as belowed here
/viewVillage.php?village_id=1
/viewVillage.php?village_id=2 and so on. */
echo "<a href='" . $row['pagelink'] . "'>"
. $row['Name'] .
. "<img src='" . $row['imglink'] . "'>
</a>";
}
This will generate your list of villages like this,
<a href="/viewVillage.php?village_id=1">
Village name 1
Village Image 1
</a>
<a href="/viewVillage.php?village_id=2">
Village name 2
Village Image 2
</a>
<a href="/viewVillage.php?village_id=3">
Village name 3
Village Image 3
</a>
.....
When you click on any of the link, it will redirected to viewVillage.php page. Now you can get the particular village using $_GET['village_id']
viewVillage.php
if(isset($_GET['village_id']]) && $_SERVER['REQUEST_METHOD'] == 'GET' ) {
$villageId = $_GET['village_id'];
// Then do your stuff over here
}
On your current page
while ($row = mysql_fetch_assoc($result)) {
/* For example ,
$row['pagelink'] should be a village id */
echo "<a href='/attractions.php?village=" . $row['pagelink'] . "'>"
. $row['Name'] .
. "<img src='" . $row['imglink'] . "'>
</a>";
}
Now it would print something like
Vilage Name <img src="urltoimage">
When you click on this link you will be sent to a file called "attractions.php"
Create this file in the same directory and it should have the following php in it
<?php
$villageId = $_GET['village']; //this gets the id of the village from the url and stores
//it in a variable
//now that you have the id of the village, perform your sql lookup here
//of course you will have to fill this is, as I don't know your actual table fields and names
$sql = "SELECT * FROM Attractions WHERE villageID = `$villageID`";
//now perform the query, loop through and print out your results
?>
Does this make sense?
Related
I have a list of teachers & their departments, it's selected randomly in a different part of the code, that all works fine, it genereates a random teacher and the subject they teach (department). However, what I want to do is make it so you click one image of a teacher, then it somehow recognises which teacher you selected, then validates whether or not you clicked a teacher with a higher rating (the rating is stored in .TXT).
So for example, Mr Smith teaches Geography and has rating 3/10. Mr White teaches History and has rating 7/10. You reload the page, and both of those teachers come up, you have to pick who you think has a higher rating, and if you're right, you go to correct.php, if you're wrong you go to incorect.php
Here's the code. (I can't use $_GET[] btw because when I do, it reloads the page)
Who's got a higher rating?
<div class="text-1"><?php echo "Name: " . $teacher1 . "<br>" . "Department: " . $teacher1department . "<br>" . "Rating: " . $teacher1rating;?></div></br>
<div class="text-2"><br><?php echo "Name: " . $teacher2 . "<br>" . "Department: " . $teacher2department . "<br>" . "Rating: " . $teacher1rating;?></div></br>
<img src="icon.png" onclick="rating()" class="icon 2"><br>
<?php
function rating() {
/* Tells you who got the higher rating */
if($teacher1rating > $teacher2rating) {
$higherRating = $teacher1;
}
if($teacher1rating < $teacher2rating) {
$higherRating = $teacher2;
}
if (isset($_GET['selected'])) {
if($_GET['selected'] == $higherRating) {
header( 'Location: correct.php?teacher=' . $teacher1 );
}
if($_GET['selected'] != $higherRating) {
header( 'Location: incorrect.php?teacher=' . $teacher2);
}
}
?>
Live version at http://hughchalmers.com/stackoverflow. When I release it, of course the rating won't be publically displayed
I'm a newbie on php, javascript and ajax. I got this tutorial about How to Create a Search Feature with PHP and MySQL and it was a success after creating it.
Link: http://www.webreference.com/programming/php/search/index.html
Now I'm changing the index.html file and linked only to search_display.php file for just a simple price checker program. I was confuse on how to link the "code" result under Javascript function doScan(code) to search_display.php.
<script>
function doScan(code) //displays the value target after scanning.
{
divStatus.innerHTML = 'Scanned Code:<br> ' + code;
setTimeout("startScanner()", 1250);
}
</script>
I wanted to link "code" to search_display.php and this is the code:
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
//if(preg_match("/^[a-zA-Z]+/", $_POST['name'])){
$name=$_POST['name'];
//connect to the database
$db=mysql_connect("localhost", "root", "bbp0m") or die ('I cannot connect to the database because: ' . mysql_error());
//var_dump($db);
//-select the database to use
$mydb=mysql_select_db("price_checker");
//var_dump($mydb);
//-query the database table
$sql="SELECT * FROM stock_master WHERE gtin LIKE '%" . #$name . "%'";
//var_dump($sql);
//-run the query against the mysql query function
$result=mysql_query($sql);
//var_dump($result);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$itemcode=$row['itemcode'];
$desc1=$row['desc1'];
$desc2=$row['desc2'];
$uom=$row['uom'];
$gtin=$row['gtin'];
$retailprice=$row['retailprice'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "" .$itemcode . " " . $desc1 . " " . $desc2 . " " . $uom . " " . $gtin . " " . $retailprice . "</li>\n";
echo "</ul>";
}
}
else{
echo "<p>Please enter a search query</p>";
}
}
//}
?>
Any ideas how to link it and if possible I would like to put everything into index.html?
Bert
I am creating a website in which I need to sell products. I am stuck at the product page of the website.
The big e-commerce website like amazon.com must have a template of the product page and they dynamically load all the data including image of product, price, reviews from the database. I also want to create a similar product page where on clicking a link of product, it automatically loads all the images, price and info stored in the database and show it.
If another product is clicked, everything loads on that very same html/php template page and I don't have to make different page for different products. How can I do that?
I have been searching for a solution to this for many days, but I don't know what exactly to search for to get the answer.
Create a php page, say, load_products.php and pass get parameters like load_products.php?product_id=1.
Change the id (as per the database setup) and get the corresponding product details from database.
Edit : Detailed Answer
Database
Table: table_products
+--------------------------+---
| id | name | details | ...
+--------------------------+---
| 1 | Product1 | Details1 | ...
| 2 | Product2 | Details2 | ...
...
+--------------------------+---
I shall show you how it is done with MySQL database.
PHP Code
load_products.php
// Establish connection to database
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('products_database_name');
// Display All the Products
$res=mysql_query("SELECT * FROM table_products");
echo "<table>";
while($row=mysql_fetch_array($res))
{
$id=$row["id"];
$name=$row["name"];
echo "<a href='load_products?id=$id'>$name</a>";
}
// Display the details of a particular product based on the input click from user
if(isset($_GET["id"]))
{
$id=$_GET["id"];
$res=mysql_query("SELECT * FROM table_products WHERE id=$id");
echo "<table>";
while($row=mysql_fetch_array($res))
{
$details=$row["details"];
$name=$row["name"];
echo "<tr><td>$name</td><td>$details</td></tr>";
}
echo "</table>";
}
A really bare bones implementation, yet fully working and you can see the output on your localhost in 15 minutes. You can build on top of this.
You can search for : Loading images from database, javascript, jquery to enhance the website interface, using ajax on top of this and so on..
Please find the Improved version
products.php page
// Establish connection to database
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('products_database_name');
// Display All the Products
$res=mysql_query("SELECT * FROM table_products");
$productsPerRow = 4;
$columnWidth = (int)(100 / $productsPerRow);
$count = 0; //count product displayed
echo "<table>
<tr>";
while($row=mysql_fetch_array($res))
{
$count++;
$id = $row["id"];
$name = $row["name"];
$image = $row["image"]; //image name with extension, upload it in the images folder
echo "<td width='" . $columnWidth ."' >
<img src='images/" . $image ."' border='0'/><br />
<a href='load_product.php?id=$id'>$name</a>
</td>";
if($count%$productsPerRow==0)
echo"</tr><tr>";
}
//close empty cells
if ($count % $productsPerRow > 0) {
echo "<td colspan='" . ($productsPerRow - ($count % $productsPerRow)) .
"'> </td></tr>";
}
//close table
echo "</table>";
load_product.php page
// Establish connection to database
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('products_database_name');
// Display the details of a particular product based on the input click from user
if(isset($_GET["id"]))
{
$id=$_GET["id"];
$res=mysql_query("SELECT * FROM table_products WHERE id=$id");
echo "<table>";
while($row=mysql_fetch_array($res))
{
$details=$row["details"];
$name=$row["name"];
echo "<tr><td>$name</td><td>$details</td></tr>";
}
echo "</table>";
}
I have a list of products on my website a Catalogue. The following PHP Code uses javascript to create a div and insert all the product description links inside it.
$sql = "SELECT * from Products ORDER BY `Name` ASC";
$result = mysqli_query($con, $sql);
echo "<script>document.getElementById('products').innerHTML = '<div class=ks>';</script>";
while($row = mysqli_fetch_assoc($result))
{
$rowid = $row['Id'];
$xx = '<a href="Getdesc.php?hid='.$rowid . '" class=lnk>' . $row['Name'] . '</a><br>';
echo "<script>document.getElementById('products').innerHTML += '$xx';</script>";
}
echo "<script>document.getElementById('products').innerHTML += '</div>';</script>";
Ignore all the ks style sheet class and all the mysql stuff for now.
The problem is it displays the grey backgrounded div (grey from style sheet)
and THEN the links. I need the links to be inside the div.
For a little explanation for those of you who are confused by the pieces of code unrelated to the primary purpose of this question,
The Products table in MySQL is a table that hold all my product info including price, name, id, e.t.c.
the "Getdesc.php?hid=..." link is a link to a php web page that will display all the information about the product from it's Id.
"Products" is an Id of a different div that contains this internal div (With the products I mean) PLUS some other stuff ( I don't feel like telling you all about it).
Sorry for the messy code, thanks in advance.
Javascript:
//from https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
function reqListener () {
document.getElementById('products').innerHTML = this.responseText;
}
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("get", "yourFile.php", true);
oReq.send();
PHP
$sql = "SELECT <columns> from Products ORDER BY `Name` ASC";
$result = mysqli_query($con, $sql);
echo '<div class="ks">';
while($row = mysqli_fetch_assoc($result))
{
$rowid = $row['Id'];
echo '' . htmlspecialchars($row['Name']) . '<br>';
}
echo '</div>'
why not put everything in a variable :
$html = '<div class="ks">'; // btw you forgot those double-quotes. you html won't evaluate the class if not surrounded by double-quotes
while($row = mysqli_fetch_assoc($result))
{
$rowid = $row['Id'];
$xx = '<a href="Getdesc.php?hid='.$rowid . '" class=lnk>' . $row['Name'] . '</a><br>';
$html.= $xx;
}
$html.= '</div>';
then echo it? if you're using php, then there's no need to change the page with javascript after it's been loaded, you just sent all the remainder of the page to the client, why not directly put this code at the right place?
echo $html;
I recently learned about Jquery pop-up div.
I used this link.
I want that kind of pop up div.
But the problem is , I need multiple pop-up div.
Suppose i have 15 user in database table then i want 15 different link.
Moreover every popup div should show the database information of that user.
I also found this one, but this pop-up div doesn't move with the mouse pointer and when the div width greater than the link width this doesn't work.
So i want pop-up div like the first example.
How can I do this ?
I thought about this problem diferently than you. So here's my idea.
1st- you don't need multiple divs cuz using jquery & php you can manipulate a single div however you want.
2nd- saying you display your 15 persons 1st makes the job easier. Let's say you store them into some links like the next example and we asume we have an ID, a Name, an Age and a Location:
<?php
$sql = "SELECT * FROM persons";
$result = mysql_query($sql);
while(mysql_fetch_array($result)) {
echo '<a hred="#" class="trigger" id=' . $result['id'] . '>' . $result['name'] . '</a><br />';
}
Now using AJAX we dispaly the results for hovering any link:
$('a').mouseenter(function(e) {
var myperson = $(e.target).text();
$.ajax({
url: "details.php?current=" + myperson,
success: function(html){
if(html) {
$("a").append(html); //here you'll have to get the current hovered link- this will dispaly the info on all the links on hovering one of them
}
}
}
Now in the details.php page we'll do the query for the current person:
<?php
$currpers = "SELECT * FROM persons WHERE name = '" . addslashes($_GET['current']) . "'") or die(mysql_error());
$results = mysql_query($currpers);
if(mysql_fetch_array($results )) {
echo '<div class="pop-up">
<p>
<strong>Age:</strong>' . $results ['age'] . '<br />
<strong>Location:</strong> ' . $results ['location'] . '
</p>
</div>';
}
?>
NOTE: I have not tested this. There might be some adjustmests but the idea is the same.