I've made a 'movie' database for personal use in my home, through a website and html 5 video tags which I had done with ease, it included an index page listing all movies in the database, and then clicking on one would link you to a page which would display the chosen movie in html 5 video tags... but tv shows are quite different, I require each different tv show to have a table and then the directory etc of each episode is stored inside the corresponding table...
The pages will look something like this... The index page listing all tv shows (Each tv shows table), which then link to another index page displaying all episodes within the corresponding tv (within the clicked table) show which was clicked and then a final page after clicking a certain episode where you could watch the episode, so if I clicked 'Breaking bad' for example, it would then list all breaking bad episodes selected from a table inside a database and then I could click on a certain episode to watch it, this is why I need a variable table name, I was told it can be done... but I don't know how!
Here's some code that I assume the answer to my problem would look like...
$episodesquery="SELECT * FROM $TBL_name ORDER BY episodeTitle ASC";
$episodesresult=mysql_query($episodesquery) or fail ("Query to get data from $TBL_name failed: ".mysql_error());
Thanks in advance
P.S I'm also having trouble with my html 5 video tags or something, I get ERR_CONTENT_LENGTH_MISMATCH after watching something longer than 30 minutes which desyncs the audio, thanks!
If you enclose the query in double quotes then below is correct
$episodesquery="SELECT * FROM $TBL_name ORDER BY episodeTitle ASC";
OR you can use concatination to make it clear that you are using a variable, like this: which makes either correct.
$episodesquery='SELECT * FROM '. $TBL_name . ' ORDER BY episodeTitle ASC';
$episodesquery="SELECT * FROM ". $TBL_name . " ORDER BY episodeTitle ASC";
Related
I made my page to show different rows from database based on different site I click on (for example when I click on 123 in my menu, it will show different table rows than in 321 menu, everythink on same site).
It works like this:
$id_dom=$_GET['id_dom'];
$query = mysql_query("SELECT * FROM summary WHERE Domain='$id_dom'");
So when I go to page: test.com/db.php?id_dom=123 , it will show different table than in test.com/db.php?id_dom=321 .
But now I need to make one page (.php?id_dom=ALL) to display ALL table rows (from 123 and 321 column).
I know I need to use "if" function, but everytime i tried to make it, it didnt work.
You should use two different select
if ($id_dom =='ALL') {
$query = mysql_query("SELECT * FROM summary ");
} else {
$query = mysql_query("SELECT * FROM summary WHERE Domain='$id_dom'");
}
I am creating a web application that allows a student to register through an HTML form (completed this portion) and stores their data in a DB(completed). I am using PHP/MYSQL to write this.
The idea is that I have a 'backend' will allow an admin to search for a student by either first, last, or full name (this is completed) and display a list of students on the next page. (Also completed). The list of students will be individual hyperlinks.
My page layout is:
backendhome.php
Has search bar and search button
search.php
When search is clicked, will execute this page which takes the item searched and finds it in the database
spits out data found for all students with that first/last/full name (may be multiple students) - these are hyperlinks
code snippet search method I use on search.php
<?php
$query = $_POST['query']; //gets value sent over from search
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM students
WHERE (`firstName` LIKE '%".$query."%') OR (`lastName` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<a href='studentData.php'>".$results['firstName']." ".$results['lastName']."</a></br>";
// posts results gotten from database you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo "No results";
}
?>
studentData.php
when a student name is clicked on from search.php it will come to this page and display their data: name, email, program, id, etc.
What I want to achieve:
You click on the students name and it will generate a new php page with student information
I have the basic steps in mind:
Store data of link clicked
Take data to search for student in DB
Display information on studentData.php
The part I am finding very difficult is taking the clicked link, storing the name between the <a></a> tag and transferring it to the new php page so I can do more with it.
Any help would be greatly appreciated.
You can pass your student id as mentioned below, and get that id in studentData.php page and retrieve student information and display it.
echo "<a href='studentData.php?id=".$results['id']."'>".$results['firstName']." ".$results['lastName']."</a></br>";
.......^
I am currently updating my site. As you can see I currently have a list of catagories (example: Free Offers, Gifts, Accessories).
These catagories will then list to promotions and then link to a single page and each one of my promotion is saved in MySQL under my DBNAME xclocouk_mobile and table mobi and each one has a promo_cat as the field name with a category underneath it.
I know how to connect to this database. What I want to know is how do I get my index page to read and display a list of catagories found under the promo_cat as listed above?
I need them to list the title as shown on my page which is done manually. But I do not want it to display duplicates.
How can I accomplish this?
$q = "SELECT promo_cat FROM mobi";
$result = mysql_query($q,$connection);
while($output = mysql_fetch_array($result)) {
echo $output['category'];
}
EDIT:
this will list duplicates if there are duplicates in your database, else it won't.
If you have duplicates in your db, use "select distinct(promo_cat) .. "
You want to use DISTINCT
SELECT DISTINCT(promo_cat) FROM xclocoauk.mobi
I have a page that lists all the products. It's a long list so I gave the users paging ablility, and I give the user the standard page list on the bottom of the page so they can go to the next page or any page. I calculate the page based on the total number of products and the number of rows displayed on one page. No issues.
$sql = "SELECT * from products limit " . $page . ', ' . $rows_per_page
I now added search ability so the user can start the list with a product starting with a given letter, i.e. start with product starting with "s" or "sneaker".
I'm having problems combining the search and paging. How do I figure out what page within the full list, their search would end up in so I can give them the ability to go to the next page or a previous page.
Offering next page only is easy because I can start a new paging list starting with the search product:
$sql = "SELECT * from products WHERE product_name >= '". $search ."' limit " . $page . ', ' . $rows_per_page
but that will not let the user go backwards.
You have a couple of options.
1) You can do it the way you suggested directly if your search form is using GET, and have a variable ($page) with the start-record on it. For example, a link to www.mydomain.com/search.php?s=$search&p=$page+10 on the "next" text. This would allow you to go to any page instantaneously via the URL, but wouldn't be smart if you have sensitive information in there. Also, you'd have to do a separate call to the server for each page.
2) You can do it the way you suggested indirectly with SESSION variables. You can store $search and $page, and have your next/previous buttons be POST form submissions. Format your MySQL query limits ahead or backwards of $page depending on the variable submitted. But again, this requires a new server request for every page.
3) You can get the entire list of results at once and use Javascript to selectively show some of them. For example, you can print the first ten results in a div with display: block and the others in other divs with display: hidden. Then your buttons can have an onclick event which shows the appropriate div of results.
I found some information is the MySQL manual http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html written in 2007 by Ian Storms, which seems to work. I don't know about the efficiency or the internal performance cos, nor do I really understand the sql
set #row=-1;
select foo.Row,foo.product_name
from (select #row:= #row+1 AS Row, product_name from products order by product_name )
AS foo where foo.product_name='skyy';
I've set up a simple gallery system in PHP that is built to display one image per page. My URL parameter simply uses a gallery variable and a page variable and does not use the image iD in any way to pull the image. This is because I'm setting my htaccess settings to make a friendly URL:
http://mysite.com/images/1/
^ Would pull the first image in my MySQL query
or
http//mysite.com/images/12/
^ Would pull the 12th image in my MySQL query
The PHP page would then look like:
images.php?gallery=images&page=1
and
images.php?gallery=images&page=12
The queries (simplified) would then look like this for each of the above:
For the first image:
SELECT id, img_src
FROM pictures
WHERE gallery = images
ORDER BY date_added DESC
LIMIT 0, 1
and for the 12th image:
SELECT id, img_src
FROM pictures
WHERE gallery = images
ORDER BY date_added DESC
LIMIT 11, 1
It's been working great but I ran into a problem now that I want to add a feature. I was hoping to display thumbnails of the ten most recently added images to the database no matter which gallery they belong to… i.e. this query:
SELECT id, gallery, img_src
FROM pictures
ORDER BY date_added DESC
LIMIT 10
Is there any way I can know which 'position' or 'page' each image would be for the specific gallery so that I can create the link correctly?
For example, say the ten most recent thumbnails return 4 pictures from the gallery 'images', then 2 pictures from the gallery 'weddings', then 3 pictures from the gallery 'portraits' and then one more image from the gallery 'images', so my links should then be:
http://mysite.com/images/1/
http://mysite.com/images/2/
http://mysite.com/images/3/
http://mysite.com/images/4/
http://mysite.com/weddings/1/
http://mysite.com/weddings/2/
http://mysite.com/portraits/1/
http://mysite.com/portraits/2/
http://mysite.com/portraits/3/
http://mysite.com/images/5/
Thanks for any help. I'm sure I'm overlooking something stupid easy here but I'm hoping to do it most efficiently as far as programming goes. So far my thoughts are that when I'm looping through the output I have to somehow retain each gallery's 'image count' and add one to this count each time an image of that gallery is added.
If you're still building the site I would change the URL's to show the actual image ID's and not a pseudo ID based on pagination.
This is good for several reasons:
SEO. You're pages stay the same. Google indexes them and when a visitor comes to the site at example.com/wedding/4234 he will actually find what he's looking for and not what Google indexed last time your site was scanned
Simplicity. You're already facing a problem that would be very easilly resolved if you were using real image ID's on the first place.
Analytics. You'll know what content is driving people to your site. There is no guessing here. URL's will remain the same always (no matter if new content comes in or not).
Quick and dirty:
$sql = "SELECT id, gallery, img_src
FROM pictures
ORDER BY date_added DESC
LIMIT 10";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
if(!$$row['gallery']) $$row['gallery'] = array();
array_push($$row['gallery'], $row['id']);
$pagenumber = count($$row['gallery']);
echo "<a href=\"http://mysite.com/{$row['gallery']}/$pagenumber/\">
<img src=\"{$row['img_src']}\" alt=\"\"></a><br>";
// alt attribute for w3 validation (http://validator.w3.org/)
}
Edit:
Fixed code error. Now will dynamically create the array if it doesn't already exist.
(Originally, it illegally used the [ ] operator.)
I'm going to add to Frankie's answer, because I also believe the OP's current method is going to bite him the butt again one day. The OP may not find this useful, but it could help someone else.
You can use http://site.com/images/20, but the 20 represents an image ID instead of a page number. Of course the query to get the image is simple:
SELECT id, img_src
FROM pictures
WHERE gallery = images
AND id = 20
LIMIT 1
As a benefit this query is more efficient than using an offset with the LIMIT clause. But the OP said in a comment that he likes his current system because it doesn't require additional database queries to help build pagination links. Well, that's not a problem here.
Lets say you're on the page http://site.com/images/20. Your page links would be the following:
Prev - http://site.com/images/prev/20
Next - http://site.com/images/next/20
Obviously it didn't take any additional queries to build those links. They're just a slightly different URL showing the same image ID. But how do they work to move to the next or prev image? Simple. Instead of the above query, you would use one of these:
If the prev link was clicked, you use this query:
SELECT id, img_src
FROM pictures
WHERE gallery = 'images'
AND id < 20
ORDER BY id DESC
LIMIT 1;
If the next link was clicked, you use this query:
SELECT id, img_src
FROM pictures
WHERE gallery = 'images'
AND id > 20
ORDER BY id ASC
LIMIT 1;
The first query gives the image that came before the current one, and the second query gives the image that comes after the current one.