I have a table which contains:
transaction_id | the_pet | name_of_the_owners
1 dog shiela
2 dog ben
3 dog alice
4 cat jonathan
and on my query:
$query="select * from table ORDER BY name_of_the_owner limit 5";
$r=mysqli_query($query);
while ($row=mysqli_fetch_array($r)) {
echo "<tr>";
echo "<td><a href='../php/ownersname.php?the_pet=".$row['the_pet']."'>".$row['the_pet']."</a></td>";
echo "</tr>";
}
However, when I use the $query, it shows all the data from the table.
What I need is this:
the_pet
dog (hyperlink)
cat (hyperlink)
So whenever I click on the hyperlink, the name_of_the owners will be shown in another page
for the dog when clicked
name_of_the_owners
shiela
ben
alice
I already used
$query = "SELECT MAX(transaction_id) as transaction_id, the_pet GROUP BY the_pet, name_of_the_owners;
but when I clicked on the hyperlink, it doesn't show the owners. :(
so if I understand it right you dont want to show duplicates right ?
I think the best way to do this, is to change your select query to
$query = "SELECT DISTINCT the_pet FROM table";
and if you dont want to do that you could filter a array for duplicates
like so:
$arr = array('php','jsp','asp','php','asp');
$unique = array_unique($arr);
print_r($dups);
both will do the trick!
Related
I have two tables and i want to echo the total call count once each user logins:
Login
Firstname | Lastname | Login ID
------------------------------
Tyler | Durden | 3
Call Count
Name | Call Count | Open | GrandTotal
------------------------------
Tyler Durden| 100 | 33 | 133
i tried:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM csvdata WHERE Name=".$_SESSION['firstname']. ' ' .$_SESSION['lastname']." ");
while($res = mysqli_fetch_array( $result )) {
echo $res['Open'].' Open Calls';
echo $res['GrandTotal'].' Grand Total Calls';
}
mysqli_close($mysqli);
?>
But its not working, i think i have to join the the two tables to get it to work. What do you think?
Assuming your Call Count table is actually called csvdata, you'll want to format your SQL request string a bit by adding single quotes around the WHERE name = part.
<?php
$result = mysqli_query($mysqli, "SELECT * FROM csvdata WHERE Name='".$_SESSION['firstname']. ' ' .$_SESSION['lastname']."' ");
while($res = mysqli_fetch_array( $result )) {
echo $res['Call Count'].' Call Count';
echo $res['Open'].' Open Calls';
echo $res['GrandTotal'].' Grand Total Calls';
}
mysqli_close($mysqli);
?>
Good practice would require that you use primary keys to facilitate joins between tables and make sure two users with the same name can be differenciated.
In that case you may want to consider replacing the Name column in your Call Count table for your loginID. This way you could get your name from the Login table (as shown below). Also, as bad as it is to have duplicated data like your user's name in both tables, you do not need your GrandTotal column since you can easily get the sum of CallCount and Open to get the exact same number. In the end, your query should look more like this (assuming your tables are called Login and CallCount).
<?php
$result = mysqli_query($mysqli, "SELECT l.FirstName, l.LastName, cc.CallCount, cc.Open, (cc.CallCount + cc.Open) AS GrandTotal FROM Login AS l JOIN CallCount AS cc ON l.LoginID = cc.LoginID WHERE l.FirstName LIKE \"".$_SESSION['firstname']."\" AND l.LastName LIKE \"".$_SESSION['lastname']."\"");
// ...
?>
well, I have 2 Mysql table which structure is bellow :
Table Jobs
job_id---job_cat_id---job_title---job_description---job_data----is_active
=========================================================================
1 1 title 1 description 1 2016-05-06 1
2 2 title 2 description 2 2016-05-06 0
3 2 title 3 description 3 2016-05-06 1
Table job_details
job_cat_id---job_cat_name
=========================
1 cat name 1
2 cat name 2
3 cat name 3
Now I want to show all jobs under each category from jobs table. E.g
What I need to show :
Job Category 1
1. job 1 from category 1
2. Job 2 from category 1
Job Category 2
1. Job 3 from category 2
So to do this I am using following sql query but can't get the correct result :
$get_job = mysqli_query($conn, "SELECT jobs.job_id, jobs.job_title, job_category.job_cat_name FROM jobs LEFT JOIN job_category ON job_category.job_cat_id = jobs.job_cat_id WHERE jobs.is_active = '1' ");
while($result = mysqli_fetch_array($get_job) ) {
$job_id = (int) $result['job_id'];
$job_title = htmlspecialchars($result['job_title']);
$job_category = htmlspecialchars($result['job_cat_name']);
echo "<h4>$job_category</h4>";
echo "<p>$job_title</p>";
}
Now, It's showing me all category with all jobs but I want to show all jobs under each category.
What is showing now :
Job Category 1
1. job 1 from category 1
Job Category 1
1. Job 2 from category 1
Job Category 2
1. Job 3 from category 2
First we have to remember that the result from a SELECT query is a newly generated table. It is not a multi dimensional array. If it were a multidimensional array, then you could get away with printing the job category at the beginning of each new array which could be grouping up all the jobs in a single category, however since this is not the type of result obtained by the SQL SELECT QUERY, you are printing the job category after each line:
echo "<h4>$job_category</h4>";
echo "<p>$job_title</p>";
Solution:
A solution to your problem would be to first use the ORBER BY ASC in your sql query:
$get_job = mysqli_query($conn, "SELECT jobs.job_id, jobs.job_title, job_category.job_cat_name FROM jobs LEFT JOIN job_category ON job_category.job_cat_id = jobs.job_cat_id WHERE jobs.is_active = '1' ORDER BY job_cat_id ASC");
From there, you know that the jobs in each category should at least be grouped up next to each other (from lowest to highest like 1,1,1,1,2,2,3,3,3). What you can now do is have a conditional print the $job_category if AND ONLY IF it hasn't been printed already previously.
Change this line:
echo "<h4>$job_category</h4>";
into this line:
if ($previous_print != $job_category)
{
echo "<h4>$job_category</h4>";
$previous_print = $job_category;
}
Let me know if it works now.
Another solution may be if you just run one query using group by job_cat_id and then inside loop write another query to get desired result with where clause job_cat_id .
You need to do 2 queries here. Here's an exmaple code, might need some tweaks acording to your table column names:
<?php
$query = "SELECT `job_cat_id`, `job_cat_name`, COUNT(`jobs`.`id`) as `jobs`
FROM `job_category`
GROUP BY `job_cat_id`";
$get_cat = mysqli_query($conn, $query);
$cats = [];
while($result = mysqli_fetch_array($get_cat) ) {
$result['jobs'] = [];
$cats[$result['job_cat_id']] = $result;
}
$get_job = mysqli_query($conn, "SELECT jobs.job_id, jobs.job_title, jobs.job_cat_id FROM jobs WHERE jobs.is_active = '1' AND `job_cat_id` IN (" . implode(',', array_keys($cats)) . ")");
while($result = mysqli_fetch_array($get_job) ) {
$cats[$result['job_cat_id']][] = $result;
}
foreach ($cats as $cat) {
$job_category = htmlspecialchars($cat['job_cat_name']);
echo "<h4>$job_category</h4>";
foreach ($cat['jobs'] as $job) {
$job_title = htmlspecialchars($job['job_title']);
echo "<p>$job_title</p>";
}
}
I have a website that list all food sources; In my db I've made a MySQL table named [foods]
In my page, when the user click the category he gets list of its all children. in my case
food
-- Plants
---- Vegetable
------ Aubergine
------ Broad_bean
------ Broccoli
------ Carrot
---- Fruits
------ Apple
------ Apricot
------ Banana
------ Cherry
------ Clementine
------ Guava
-- Animals
Now, I want to make a dynamic breadcrumbs navigation in the top of my page, something like
Food > Plants > Fruits > Banana
So the user can navigate through them.
I've tried several queries to get this but with no luck.
Every time I get only the first parent of the category only
So if I am in Banana page I only get the Fruits category, nothing deeper.
I know I have to use while loop where cat_parent_id != 0 but I couldn't figure-out how to implement it the right way!
here is a code snippet I've tried
$cat_parent_id = $_REQUEST['cat_id'];
$q = mysqli_query($link, "SELECT * FROM foods WHERE cat_id = $cat_parent_id");
while($r = mysqli_fetch_assoc($q))
{
echo $name = $r['cat_name'];
}
I really appreciate your help in this regards
Thanks in advance...
u have to use recursive function. means check
for ex:
function show_breadcumb($cat_id)
{
$q = mysqli_query($link, "SELECT * FROM foods WHERE cat_id = $cat_id");
while($r = mysqli_fetch_assoc($q))
{
$name = $r['cat_name'];
$string.= $name .">".$string
}
//check if this category has any parent again call this fucntion
if( has parent ) { show_breadcumb($cat_id) }
else return $string;
}
Means ur loop should continue find category up to first level.
I want to make the generated list from mysql data clickable or url is included. Is there a way to do this? part of the field is menu_parent and menu_url.
This is the sample generated list...
Boats
Cars
Trucks
when one of the item is clicked, it will be directed to the corresponding link or url in the mysql.
this is the mysql fields
menu item | menu_url
boats | boats.html
cars | cars.html
trucks | trucks.html'
Any help is appreciated.
$sql=$dbh->prepare("SELECT * FROM table");
$sql->execute();
while($r=$sql->fetch()){
echo "<a href='".$r['menu_url']."'>".$r['menu item']."</a>";
}
I prefer Foreach Other Than While...
$result = "SELECT * FROM yourTable";
$query = $glb_connection->prepare($result);
$query->execute();
$qry = $query->fetchAll();
foreach ($qry as $menu) {
echo "<a href='".$menu['menu_url']."'>".$menu['menu item']."</a>";
}
Im not very good at mysql query thatswhy I ask here for a little help.
I use this query to select an anime tvshow
$getMovieCmd = "SELECT id,name,hoster,season,episode FROM topmovies.movies
WHERE name = '".mysql_real_escape_string($_GET['name'])."' ";
But I dont want to show all animes with only the same name. I want only to show those anime with the same name, same season and same episode.
Example:
tvshow: Naruto /
Season: 1 and 2 /
Episode: 1,2,3,4,5,6,7,8,9,10
I only want to show naruto with season 1 and ALL hoster which has episode 1.
Picture so you see what I want to do
http://s14.directupload.net/images/130819/g6bz3ef8.jpg
(Staffel = Season)
As you see on the picture the episode is selected. Now I want to make, that all different hosters with that episode will be selected.
Hope you understand my shitty english :/
Im searching for a query like this
$getMovieCmd = "SELECT id,name,hoster FROM topmovies.movies
WHERE name = '".mysql_real_escape_string($_GET['name'])."'
IN (SELECT season FROM topmovies.movies WHERE name = '".mysql_real_escape_string($_GET['name'])."') AS season
IN (SELECT episode FROM topmovies.movies WHERE name = '".mysql_real_escape_string($_GET['name'])."') AS episode
";
you need to add more parameters to your query
$getMovieCmd = "SELECT id,name,hoster,season,episode FROM topmovies.movies
WHERE name = '".mysql_real_escape_string($_GET['name'])."' AND season = '".mysql_real_escape_string($_GET['name'])."' AND episode= '".mysql_real_escape_string($_GET['episode'])."' ";
If I understand you correctly what you are looking for is:
$getMovieCmd = "SELECT id,name,hoster,season,episode FROM topmovies.movies
WHERE name = '".mysql_real_escape_string($_GET['name'])."'
AND episode = '".mysql_real_escape_string($_GET['episode'])."'
AND season = '".mysql_real_escape_string($_GET['season'])."'";
And season=$_GET['season'] and episode=$_GET['episode']
That is if you have them in $_GET