Php-Mysql choosing rows from database based on Domain column - php

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'");
}

Related

Is there any way to search from the result in query?

I am making pagination in core PHP (actually first time). As user will press the pagination button for example user pressed second pagination button. then this query generates:
SELECT * FROM product LIMIT 12, 12
It is executing perfectly as I expected. but the main problem is that if user wants to do filtration on that specific page for suppose user wants all items which are present in page 2 and category_id must be 3 then this query generates:
SELECT * FROM products WHERE category_id IN (3) LIMIT 12, 12
But I am getting the empty resultset... What I am doing wrong?
The pages are based on the number of lines your query returns. You cannot apply a filter on a single page on the DB side as any additional Wheres would change the elements in the pages of the previous query.
In your case, adding WHERE category_id IN (3) results in no lines, making your new page empty.
You could implement filters in your frontend, or backend AFTER the query returns the result, and your filter would remove items from the DB result.
Ideally, every time your users interact with the UI, it would result in a new call to the DB with the filters being applied on the query. then show the result to your user.
If I understood correctly you want to change the WHERE clause in any way between pages and you not want any previous records be repeated.
If you have a unique id in your table and that id increases with each page you can use for a bookmark. I have a simple procedure to make that column. Then you can create a column specifically for this app.
You create a bookmark column and add it to the WHERE clause.
This column will give you a bookmark to save the user's position.
You save this column's value from the last record.
Then in your query you can add this position your query in the WHERE clause
WHERE `bookmark` > $position AND ...
Then you can make any change between any pages at any time you want to the WHERE clause and you will never repeat a previous record.
Making this new column is very simple and does not require much storage.
Use the same ORDER BY from your page query and make your WHERE 1.
$sql = SELECT `product_id` FROM `products` WHERE 1 ORDER BY `category`, `description`";
$results = mysqli_query($conn,$sql);
while(list($product_id) = mysqli_fetch_array($results, MYSQLI_NUM)){
$bookmark[] = $product_id;
}
foreach($bookmark as $position => $product_id){
$sql = "UPDATE `products` SET `bookmark` = $position WHERE `product_id`=$product_id";
}

INSERT Multiply row in MySQL when i have a list from another table

At the moment I'm stuck at the following point. I have a table with some company where we do some work for. in this table is have the following information.
id
Projectno.
name
location
With a select I show them al in one big view. For that I'm using this Select.
$query = mysql_query("SELECT * FROM vdwerf_strooien_opdrachtgevers WHERE actief='0' ORDER by id") or die (mysql_error());
while ($get = mysql_fetch_array($query))
{
}
In the view of the result I get i want to put in worked hours divided over every single company.
I have in my form the link as soon i press submit go to ?action=new.
In there is have the next INSERT which is putting al the company's in, but for the hours it only takes the last record. so for every company the code puts in 2 hours. Even it might that i didn't work for one company the other maybe 1 and another again 2.
mysql_query ("INSERT INTO vdwerf_strooien_uren (id, opdrachtgever, dagdeel1) SELECT '', id, '".$_POST[dagdeel1]."' FROM vdwerf_strooien_opdrachtgevers");
So now I would like to get some help with the question. If i have 6 or 60 company's how can I insert different hours by every single company when I select all the company's from another table.
the company's are in a table because it can happen that one get cancelled or they add a new company.
Thanks for all the help you will give.

Php code to alphabetically sort a list from the database by clicking on a link on the page

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

Variable table name Select * FROM $tablename

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";

Display categories from a database

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

Categories