I have There PHP File getting Data From MySQL Database called:
clubs.php
club.php
player.php
the clubs.php list all clubs from tbleclubs table and it looks like:
clubs.php
$database = new Database();
$res = $Db->query("SELECT * FROM tblclubs");
foreach ($res as $dataRow): ?>
<span><?php echo $dataRow['id']; ?></span>
<span><a hrer=""><?php echo $dataRow['name']; ?> </a></span>
<?php endforeach; ?>
and club.php listing all players in a club from tblclub table
club.php
$database = new Database();
$res = $Db->query("SELECT * FROM tblpclub");
foreach ($res as $dataRow): ?>
<span><?php echo $dataRow['id']; ?></span>
<span><a hrer=""><?php echo $dataRow['name']; ?> </a></span>
<?php endforeach; ?>
and finally the player.php is from tbleplayer:
player.php
$database = new Database();
$res = $Db->query("SELECT * FROM tblplayers");
foreach ($res as $dataRow): ?>
<span><?php echo $dataRow['id']; ?></span>
<span><a hrer=""><?php echo $dataRow['name']; ?> </a></span>
<?php endforeach; ?>
I have set up a Foreign Key(FK) for tblclub as clubs_id and for tblplayers as club_id.
Now, my question is, how can I dynamically navigate to each selected item in next page like:
clubs->club->player
Thanks
First of all you have some errors in your HTML (it should href and not hrer for links)
Secondly, using a select * type of query is bad form; you should generally be choosing specific keys from your table, and not just all of them. You will also need to be link your tables if you want this to work properly; it's difficult to tell what specific rows you have in your tables; some structure would be helpful.
That said you would need to structure your queries something like this. For example to get a list of players a specific club:
SELECT p.playerid, p.name,
FROM tblplayers p, tblclub c
WHERE p.club_id = c.club_id
this query will get a list of all players on a specific club.
As for linking you would could modify your links to be something like this. To link to a specific player in a specific club, this would be the link on club.php to player.php:
<?php echo $dataRow['name']; ?>
Then to get the info you could just get the playerid from the URL by using a GET variable, like this: $playerid = $_GET['playerid']
and then get your information from the query, to do what you like. You can use the data in any way you like from there.
This is just a start, but you should be able to extrapolate from this for the rest of your questions. Hope this helps.
Ok, you are now rendering static lists.
To make the navigation dynamic you would basically need 3 things:
create links with ids
fetch this id from the URL
and query the db using the WHERE statement to select that specific id
You have already started with links: <a hrer="">, but it's href.
clubs.php - would render the overview of all clubs with links to each club
Place this in your foreach to construct the links by attaching the ids.
foreach ($res as $dataRow) {
$id = $dataRow['id'];
$name = $dataRow['name'];
$link = 'club.php?id=' . $id;
echo '<span>' . $id . '</span>';
echo '<span>' . $name. '</span>';
}
Now you can click for instance club.php?id=2.
But club.php would need to handle the incoming id, right?
club.php - renders the list of players with links to each player
You basically repeat the pattern from above, but with a different anchor base, this time it's player.php. You should get a list with player.php?id=x links.
How to handle the id in each of the scripts?
The id is incoming via $_GET.
You can use var_dump($_GET['id']) to see the value. Use a variable, like so $id = $_GET['id'].
(Later, when everything works: do not forget to secure and escape the incoming data properly.)
Then use $id in your database query:
SELECT * FROM tblplayers WHERE player_id = ' . $id;
Related
I have created a members.php page that connects to a database table. The table has the following fields: id, username, profile image.
The following PHP code displays the profile image for each user in rows of 6 and allows each image to be clickable.
<?php
// The code below will display the current users who have created accounts with: **datemeafterdark.com**
$result=mysql_query("SELECT * FROM profile_aboutyou");
$row1 = mysql_fetch_assoc($result);
$id = $row1["id"];
$_SESSION['id'] = $id;
$profileimagepath = $row1["profileimagepath"];
$_SESSION['profileimagepath'] = $profileimagepath;
$count = 0;
while($dispImg=mysql_fetch_array($result))
{
if($count==6) //6 images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
?>
<center>
<img src="<?php echo $dispImg['profileimagepath'];?>" width="85px;" height="85px;">
</center>
<?php
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
?>
This is all great, however, when I click on the image that loads it re-directs me to: viewmemberprofile.php which is what it is supposed to do. But it always displays the same image with the same id value (i.e.) 150 no matter which image I click. What I would like to have happened is. If I click on an image with id 155 etc... it will display content for that image data field not consistently the same image data regardless of which image I click.
Your help and guidance would be greatly appreciated. Thank you in advance.
One thing that I forgot to mention is that I do use sessions so... when I am re-directed to the viewmemberprofile.php page I use the following code to aide in getting the data that I need from the table.
<?php
$id = $_SESSION['id'];
echo($id);
?>
<?php
echo('<br>');
?>
<?php
$profileimagepath = $_SESSION['profileimagepath'];
?>
<img src="<?php echo($profileimagepath);?>" width="50px;" height="50px;">
I have yet to impliment the suggested solution.
You need to pass the ID of the row to viewmemberprofile.php, e.g.:
<a href="viewmemberprofile.php?id=<?= $dispImg['profileimagepath'] ?>">
And viewmemberprofile.php needs to select that row from the DB:
SELECT * FROM profile_aboutyou WHERE id = $_GET['id']
The above SQL statement is pseudo-code; you need to write actual code to accomplish what it is describing, preferably using parameterized queries.
I thought I would simplify my question and start with trying to solve one problem at a time instead of two. The -1 rating tells me I was not clear enough, so I will try again. My original post is quoted below.
With the following code I can display data from my table that meets the condition that the category is Scenics:
<?php
$category = $_GET['category'] ?? 'Scenics';
$sql = "SELECT * FROM photo_library ";
$sql .= "WHERE category='" . $category . "' ORDER BY id ASC";
$image_set = mysqli_query($db, $sql);
$image_set_desc = mysqli_query($db, $sql);
?>
What I want to do is have dynamically set. Instead of $category = $_GET['category'] ?? 'Scenics';, is there a way not set the way it is above? I would like it be set by whatever it finds in the 'category' column of my SQL table so that later I can say while category is Scenics and sub_category is Coastal, display just that info (so I will need to do something similar for sub_category).
EDIT: The term I was looking for was array. I want $sub_category to be an array based on what is found in the sub_category column of my SQL table.
I am also aware of the risk of using $_GET, but this would be on a protected site where you only get access if you are a member, and you are only a member if you apply and are manually approved for access. And there is no user input in the protected part of the site. It is strictly a resource site for downloading graphics and images.
I am playing around with having an image gallery be generated using
PHP and My SQL instead of coding everything in HTML on the page. I've
created a test table that has multiple columns, and a working page
where all the info gets pulled in and displayed.
The two main values that will be used to separate image entires from
each other are 'category' and 'sub_category'. The page that is working
only uses the category and works great.
<?php
$category = $_GET['category'] ?? 'Agroforestry';
$sql = "SELECT * FROM photo_library ";
$sql .= "WHERE category='" . $category . "' ORDER BY id ASC";
$image_set = mysqli_query($db, $sql);
$image_set_desc = mysqli_query($db, $sql);
?>
$image_set pulls in the info for the actual image, and $image_set_desc
does the work for the links and file size. This is the pulling in of
the thumbnail:
<?php while($subject = mysqli_fetch_assoc($image_set)) { ?>
<li><a href="<?php echo h($subject['jpg_location']); ?>" title="<?php echo h($subject['title']); ?>" data-title="<?php echo
h($subject['id']); ?>">" src="#" alt="" aria-describedby="">
<?php while($subject_desc = mysqli_fetch_assoc($image_set_desc)) { ?>
<p id="<?php echo h($subject_desc['id']); ?>"><b>Filename:</b> <?php echo h($subject_desc['filename']); ?><br>
<a class="btn btn-default btn-xs mrgn-tp-sm" role="button" href="<?php echo h($subject_desc['tif_location']); ?>">TIFF <span
class="wb-inv">image download () ">JPG image
download ()
The problem is on the next page I am testing this on, there are a lot
more pictures and they are broken up into multiple sub-categories. I
only want to display pictures with certain sub-categories under their
respective headings.
I am very new to PHP, and do not know how to set something like
$sub_category to whatever it finds in that column. I cannot use
$sub_category = $_GET['sub_category'] ?? 'Coastal';
because the sub-category needs to be pulled from the SQL table. I
don't know what I am doing so while
$sub_category = isset($_GET['sub_category']);
doesn't break anything, it also doesn't seem to work. If that somehow
works, then
<?php while(($subject = mysqli_fetch_assoc($image_set)) && ($sub_category == 'Coastal')){ ?>
is not because nothing is generated on the page. I'm not sure if my
problem is with defining $sub_category, the while loop, or both.
First off even if your site is restricted to a subset of users your still leaving room for many attacks which could lead to attackers being able to mess with your database structure and even worse obtain sensitive data.
Regarding your problem please have a look at normalization. Currently every picture has a set category written in the category field which leads to your current confusion/problem because you now want to gather categories from every picture within your database.
If you were to normalize the category field to a separate table and link them via a foreign key you could then firstly query all categories and then group the images by the categories after looping trough.
Here's a bit of non optimized pseudo code that should give you an idea as to what I mean:
$categorizedImages = [];
$catSql = "SELECT * FROM categories";
$categories = mysqli_query($db, $sql);
foreach($categories as $category) {
$imageSql = "SELECT * FROM photo_library WHERE category='" . $category['id'] . "' ORDER BY id ASC";
// I cant recall if the access from mysqli_query is via array or ->
$categorizedImages[$category['id']] = mysqli_query($db, $imageSql);
}
var_dump($categorizedImages); exit;
I'm new to PHP and pardon me for asking this very basic question. What I want to do is to display or view a page based on a specific record. For example, I have a home.php page which lists records of lessons. And when I click on a specific record, it will go a page named lesson.php . I have to view the relevant information/data from my dB of that specific lesson. I tried to use GET but I think it's not going to meet the requirement of my system.
This is what I've tried so far:
$qry1stQuarter = $conn->prepare("SELECT l.lesson_title FROM tbllessons as l
JOIN tblstudents as s
ON l.grade_level = s.grade_level
WHERE quarter_code = '1st'
AND s.grade_level=:grade_level");
$qry1stQuarter->execute(array(':grade_level' => $grade_level));
<div id="tabs-2">
<div id="accordion">
<h3><strong>Yunit 1</strong></h3>
<div>
<?php
for($i=0; $row = $qry1stQuarter->fetch(); $i++){
$lesson_title = $row['lesson_title'];
?>
<div id = "lessons">
<?php
echo "<a href = 'lesson_view.php'>$lesson_title </a>";?>
</div>
<?php
} // end of for loop
?>
</div> <!-- end of Yunit 1 -->
What is the best way to do this? Your help is pretty much appreciated. Thanks.
In your database, I assume you have an ID column. A typical way to do what you are asking is to use that ID as a GET parameter on a link, and then include that in your WHERE clause in your SQL statement.
Eg:
echo "<a href='lesson_view.php?id=$lesson_id'>$lesson_title</a>";?>
And then on your lesson_view.php page, your SQL has something like this:
SELECT * FROM tbllessons WHERE id = mysql_real_escape_string($_GET['id'])
I've scoured the web for a tutorial about this simple task, but to no avail. And so I turn to you helpful comrades. Here's what I need to do:
I have a MySQL database with an Events table. I need to create a PHP web page with a list of the Event titles, and each title must be a link to the full details of the Event. But I want to avoid having to create a static page for each event, primarily because I don't want the data entry volunteer to have to create these new pages. (Yes, I realize that static pages are more SEO friendly, but I need to forego that in this case for the sake of efficiency.)
I've seen PHP url syntax with something like this:
pagename.php?id=20
but I don't know how to make it work.
Any and all help greatly appreciated.
Thanks!
Kip
This is basic php. You would simply query the DB for the event details before the page headers are written and write the html accordingly.
The first thing I would ask you is if you know how to connect to your database. From there, you query based on the $_GET['id'] value and use the results to populate your html.
Not to be rude, but the question itself suggests you're new to PHP, right? So in order to provide a solution that works we might want to know just how far you got.
Also, you can rewrite your dynamic urls to appear like static ones using apache's mod_rewrite. It's probably a novice level thing if you're interested in "pretty" url's.
MODIFIED ANSWER:
In your loop you would use the id from the query result (assuming your primary key is id)...
while($field = mysql_fetch_array($result)) {
echo "<p class='date'>";
echo $field['month']." ".$field['day'].", ".$field['year'];
echo "</p>";
echo "<h3>";
echo ''.$field['event_name'].'';
echo "</h3>";
}
Then on somepage.php you would use the get var id to pull the relevant info...
$result = mysql_query("SELECT * FROM `calendar` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."');
don't forget to look into mysql_real_escape_string() for cleaning entries.
It's wise to take extra care when you are using $_GETvariables, because them can be easily altered by a malicious user.
Following with the example, you could do:
$foo = (int)$_GET['id'];
So we are forcing here the cast of the variable to a integer so we are sure about the nature of the data, this is commonly used to avoid SQL injections.
lets say you have the php file test.php
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("db", $conn);
$id = $_GET['id'];
$sql = "select * from table where id = $id";
$result = mysql_query($sql, $conn);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[0];
$content = $row[1];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
a dynamic page would be something like that..
Here is the pertinent code for extracting a list of events in November from a table named calendar, with each event having a link to a page called event.php and with the event's id field appended to the end of the url:
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"<a href='event.php?id=".$row['id']."'>".$row['event_name']."</a>"
;}
And here is the pertinent code on the event.php page. Note the row numbers in brackets depends on the placement of such in your table, remembering that the first row (field) would have the number 0 inside the brackets:
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
This works for me, thanks to the help from those above.
$foo=$_GET['id'];
in your example $foo would = 20
I am trying to build a simple catalog displaying items from a mysql database.
So far I can get the items to display in a list format including the images stored as a path in the items table in a field called "path".
I would like the ability to be able to click on the item image and it takes you to a dedicated page showing you the details of that product. The link would correspond to which ever item you click on based on the data in the item table.
So far I have the following code;
<?php
session_start();
require "connect.php";
$date = date("d-M-Y");
$query = "select * from item order by date && time asc limit 0,3";
$query2 = "select * from users where userid = ".$_SESSION['userid'];
$result = #mysql_query($query, $connection)
or die ("Unable to perform query<br>$query");
?>
<?php
while($row= mysql_fetch_array($result))
{
?>
<?php echo $row['item'] ?>
<?php echo $row['description'] ?>
//Code for Image Link
<a href='<a href='<?php echo $row['path']?>'><img src="<?php echo $row['path']?>
<?php
}
?>
The above code allows you to click on the image and it takes me to http://127.0.0.1/steal/%3Ca%20href=
However I don't know what I would need to enter in order for it to take me to a page that shows the product?
Please Help
Many Thanks
I don't know your database schema, so I'm taking some liberties here about your columns for the product. But, try this:
<img src="<?php echo $row['path']?"/>
I also am not sure what your product page URL is either, so change product_page.php to whatever template you are using to display your products.
You need to create another page called for example showProductsOrder.php that accepts the orderID and then does output the content.
showProductsOrder.php?orderID=1
<?php
SELECT * FROM order WHERE orderID = $_GET['orderID']
while(fetch()) {
//> Show product here
}
?>
Also please don't use mysql_query. Use php.net/pdo
Addendum
Show all products
As yes123 said you would need to create another page, but i if you have a problem creating the link here is my suggested solution:
Replace your link with:
<?php echo ("<a href='".$row['pagePath']."'><img src='".$row['imagePath']."' /></a>";?>
$row['pagePath'] is the page that it will load when the image is clicked on, and
$row['imagePath'] is where the image is stored on your pc / server.