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
Related
I am relatively new to PHP and MySQL. I have a list of information I want to retrieve from the database and display on my html page. I want on one page to display the unordered list and then on the header of my html page to have links named like A | B | C.....and when I click on any of the alphabet letter to sort the first column of the unsorted list from the database according to the letter of the alphabet clicked.
Here is my code so far...
My code so far
This is how I want my page to look like...
How I want my page to look like
I have googled about pagination using php and the results are helpful but not showing how to create pages for sorting a list based on a certain criteria, like alphabetically
One way to do is, keep links on Alphabet letter as
All |
A |
B
Now in your sort.php get the value sorted by query
First get sorting letter from url using $_GET variable
<?php
if(isset($_GET['let']))
$let = $_GET['let'];
else
$let='';
$query = "SELECT supplier, contact, telephone, email FROM suppliers WHERE supplier LIKE '$let%'";
// other codes
?>
You can do this with SQL queries alone:
$letter = "A";
$query = "SELECT supplier, contact, telephone, email FROM suppliers WHERE supplier LIKE '$letter%'";
For your "ALL" page, just use a different SQL SELECT query omitting the LIKE from the query above
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'");
}
Say I have a website with a product page that displays information about the product which is generated by product.php using a GET on productname and getting information from a MySQL database. Now, occasionally product names will change. This will happen rarely but it will happen.
After the name change, old links to www.website.com/product.php?productname=Toaster400 will no longer work obviously. However, I would like for these links to redirect to the correct page. I would also like to allow people to search for "Toaster400" even though the corrected name is "Toaster400a".
I have thought of only one solution which is to have an table in MySQL of 'old names.' If a product is not found in the Product table, then I could check the 'old names' table and see which product that old name is related to.
Is this the best way of appraching this? Thank you.
Thank you.
EDIT:
Here's the code:
//This just initializes the database connection
require __DIR__ . "/resources/database.php";
//checks to see if they searched for anything
if(isset($_GET['search_text'])) {
$name = $_GET['search_text'];
$db = getDbConnection();
//This is a stored procedure which is literally just a select for % . ? . %
$stmt = $db->prepare("CALL get_products(?)");
$stmt->bindParam(1, $name, PDO::PARAM_STR, 150);
$isQueryOk = $stmt->execute();
$results = array();
The code then checks how many rows were returned and redirects accordingly.
You can consider 'tags' column in the table for a product. For example:
table column
id->1
Product name-> test
tags->test
after update, value of column will be
id->1
product name->testupdate
tags->test, testupdate
and by doing this, you can search renamed product. Hope this helps.:)
table product : product_id, product_name, description, etc.
table history : product_id, product_name
url : example.com/../sample_name
select * from product where product_name='sample_name'
if not found
select * from history h
left join product p on h.product_id=p.product_id
where h.product_name='sample_name'
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'm trying to have mysql outputting a list of article categories.
there are lots of articles, each with preset categories that are stored in mysql. But many articles have the same category, so my list get very long with similar category results.
My idea is that if the user has posted 1 post in a category, the category gets listed. but this needs to understand that the category should just be listed once even if the user has posted multiple times in that specific category. How can i do this?
This is what i got, but not working:
foreach( $result as $row ) {
if($result>1){
$kategorilink= "{$row['kategori']}";
echo $kategorilink;
}
}
Try SELECT DISTINCT * FROM .... This will give you each different value only once.
modifying mysql data in php is not a good idea, you can select disting categories from mysql like
select distinct(category) from article where full_name='$safe_name'
or you can add group by clause to your query to group your result according to categories
select * from article where full_name='$safe_name' group by (category)
if you want to check number of results you can use mysql_num_rows() like
if (mysql_num_row($result) > 1){ //code here}