I'm trying to make a list where the format goes as follows:
CLUB
name
name
name
CLUB 2
name
name
So far, I've managed to autogenerate tables but for each name it displays the name of the club above, even if it's the same club as another one above it.
I've tried to do it through mysqli, I don't know if it would be better through PHP. Here's my query so far:
$sql = "SELECT DISTINCT soc.nombre, soc.apellido, club.nombreClub FROM socios as soc
INNER JOIN club4h as club
ON soc.nombreClub = club.nombreClub";
$result =NULL;
foreach($conn->query($sql) as $row){
$club[] = $row["nombreClub"];
$nombreSoc[] = $row["nombre"];
$apellidoSoc[] = $row["apellido"];
}
And here's how it's looking with that query:
You can just use order by statement in your SQL query
$sql = "SELECT DISTINCT soc.nombre, soc.apellido, club.nombreClub FROM
socios as soc
INNER JOIN club4h as club
ON soc.nombreClub = club.nombreClub
ORDER NY club.nombreClub DESC, soc.nombre DESC";
So this way you will get your data sorted out of the database and all you need to do is display it.
Related
I have a search form where I can search for my webshop products.
1 product can be in multiple categoris, not just in one. I store this in the termek_katgoria_kapcsolo table. At insert, it creates as many lines, as the product belong to many categoria.
Example: The ID 12 product belong to ID 1, ID 2, ID 3 categoria.
The search sql only look at categoria, when one categoria is selected. Most often, I just search for the products name, I don't sort it to categoris.
How can I write the sql, that if I select a categoria also? I show you the tables on a pic.
if($termek_kategoria == 0 ) // Sort to categoria or not only search for product name, id...
{
$sql = "
SELECT termek_id, termek_nev, termek_cikkszam, termek_status FROM termek
WHERE $kereses_helye LIKE '%$kw%' ORDER BY $kereses_rendezes $kereses_sorrend
";
}
else
{
// Sorting for categoria also
$sql = "
SELECT termek_id, termek_nev, termek_cikkszam, termek_status FROM termek
WHERE $kereses_helye LIKE '%$kw%' AND termek_kategoria =
'$termek_kategoria' ORDER BY $kereses_rendezes $kereses_sorrend
";
}
Update:
$sql = "
SELECT termek.termek_id, termek.termek_nev, termek.termek_cikkszam, termek.termek_status
termek_kategoria_kapcsolo.*, termek_kategoria.kat_id
FROM termek
LEFT JOIN termek_katgoria_kapcsolo ON termek_kategoria
WHERE termek_kategoria_kapcsolo.kat_kapcs_kategoria_id = termek_kategoria.kat_id
AND termek.termek_id IN (SELECT kat_kapcs_termek_id FROM
termek_kategoria_kapcsolo WHERE kat_kapcs_kategoria_id = '$termek_kategoria')
";
This result:
Whats going wrong here?
What I want is when I select a categoria, the program give me the products, that are in the selected categoria.
I solved the problem:
$sql =
"
SELECT
t.termek_id,
t.termek_nev,
t.termek_cikkszam,
t.termek_status,
kapcs.kat_kapcs_kategoria_id,
kapcs.kat_kapcs_termek_id
FROM termek t
LEFT JOIN termek_katgoria_kapcsolo kapcs ON kapcs.kat_kapcs_kategoria_id = '$termek_kategoria'
WHERE t.termek_id = kapcs.kat_kapcs_termek_id AND t.$kereses_helye LIKE '%$kw%' ORDER BY t.$kereses_rendezes $kereses_sorrend
";
Thanks in advance for any time you spend on my question.
I am trying to display data in a way that will display the manufacturer as a name instead of a number.
Basically when they store the data they choose a manufacturer from a drop down which is generated from a table.. IE Trogues = 1 so products stores the #1 so I know that any beer is associated with trogues is 1. Now I want to display the data but instead of having a 1 I would like to have Trogues be displayed. Where you see manufacturer in the echo code below..
I am not understanding the process logic here..
error_reporting(E_ALL);
ini_set('display_errors', 1);
$sql = "SELECT * FROM products
LEFT JOIN manufacturer
ON product.manufacturer = manufacturer.id
ORDER BY manufacturer.id, product.id";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "
<div class=reportclientproduct>".$row['manufacturer']." - <a href=".$row['website']." target=_blank>".$row['product']."</a></div>";
}
Have you tried the query like this:
$sql = "SELECT man.id AS manufac, products.product AS prod FROM products
LEFT JOIN manufacturer as man
ON product.manufacturer = manufacturer.id
ORDER BY manufacturer.id, product.id";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "
".$row['manufac']." - ".$row['prod']."
";
}
Assuming that the table products had a column named manufacturer which holds the ID of the manufacturer, and that both tables have columns name ID which hold the ID of the table item.
Also the JOIN functions may vary based on the database you use. But the aforementioned method is for mysql.
I'm wondering about How I can write a function to get some parameters from a database.
I have to get the medical record of some patients, I have two tables
Table 1: "Patients"
idpatient int (10) Primarykey
names
age
id_card
and
Table 2: "Medical_record"
id_medical int (10) Primarykey
date
id_patient
diagnostic
How can I do to join those tables and get the details of every patient and record..for example if I select a patient called Jhon, I would like to see the details of everytime he was in the hospital, showing table like this, using php and mysql.
desired output
Id:0001
Name:Jhon J.
Date ---- --------Diagnostic
10/05/2012 headache
15/04/2013 stomache
28/08/2014 some stuff
function
public function look_assign($idPatient, $idRecord){
$query = "SELECT * FROM patients WHERE idpatients = '".$idpatient."' AND idRecord = '".$idRecord."' ";
$this->result = $this->objDb->select($query);
return $this->result;
}
Thanks :)
You must make a join request, like this:
$query = "SELECT
p.*, mr.*
FROM patients p
INNER JOIN medical_record mr ON p.idpatients = mr.id_patient
WHERE p.idpatients = '".$idpatient."' AND p.idRecord = '".$idRecord."' ";
Namastey!
I'm currently working on one of my projects where I get to one doubt.
What I'm doing is:
I have got 4 tables called:
1: project_degree
2: project_department
3: project_category
4: Projects
project_degree have got four top level degrees which are: B.tech, M.tech, M.C.A. & M.B.A.
project_department have got their respective departments such as: C.S.E, I.T., E.E.E. and so on.
Now, project_category have some categories like PHP, .NET, SAP etc.
What I'm trying to do is, I'm adding a project to database called "projects" where I'm assigning it to its degree, department and category.
Structure of projects table is as follows:
id | project name | degree_id | Dept_id | Cat_id
1 | Sample project | 1 | 3 | 4,3,6,5,9
Now as you can see above, field "cat_id" have got multiple categories where each categories ID have been separated by a comma.
Now when I call a query get all projects from the table called "projects", I want to list of categories related to that particular project and my code goes like this:
$query = mysql_query("SELECT * from projects ORDER BY id DESC");
while($row = mysql_fetch_array($query)) {
$categories = $row['cat_id'];
$cat_arr = explode(',',$categories);
$cat_size = sizeof($cat_arr);
for($i=0;$i<$cat_size;$i++) {
$get_cat = mysql_query("SELECT * from project_category WHERE id='".$cat_arr['$i']."'");
$cat_row = mysql_fetch_array($get_cat);
echo $cat_row['name']; // Here finally I'm getting categories name
}
}
So, finally I'm running nested loop and two queries to get a category name. I doubt it affect the page load time when it comes to huge data.
Is there any better alternative to this situation?
Here should be work
$query = mysql_query("SELECT * from projects ORDER BY id DESC");
while($row = mysql_fetch_array($query)) {
$get_cat = mysql_query("SELECT * from project_category WHERE id IN (" . $row['cat_id'] . ")");
$cat_row = mysql_fetch_array($get_cat);
echo $cat_row['name']; // Here finally I'm getting categories name
}
But normalization is better.
It's better to add a new table, project_has_catID where you can store project.id and cat.id as two columns (project_id and cat_id). Remove the Cat_id column in the project table.
Once done, simply select the name by the next query;
$query = mysql_query("SELECT project_category.name from project_category INNER JOIN project_has_catID phc ON phc.cat_id = project_category.id ORDER BY phc.project_id DESC");
By this, you have a list of category names. You don't have to loop with multiple database communications.
You could load all category names into an associative array first, then look up category names in this array when looping through the projects.
$categoryNames = array();
$query = mysql_query("SELECT id, name FROM project_category");
while ($row = mysql_fetch_array($query))
{
$categoryNames[$row['id']] = $row['name'];
}
$query = mysql_query("SELECT * FROM projects ORDER BY id DESC");
while ($row = mysql_fetch_array($query))
{
$categoryIds = explode(',', $row['cat_id']);
foreach ($categoryIds as $cat_id)
{
$cat_name = $categoryNames[$cat_id];
//do what you want with the name here
}
}
This way, you don't have to change your database structure, or the structure of your code. And you only have to execute two queries in total, so this is by far the simplest solution.
Needless to say, normalization is always better as others have indicated (never use a column that contains multiple comma-separated values), but you probably knew that and had your reasons for setting up your database tables this way.
Sorry let me revise. I have a three tables:
events_year
• EventID
• YearID
• id
Date
• YearID
• Year
Event
• EventID
• EventName
• EventType
i want to dispay a record from the three tables like so:
EventName - Year: Marathon - 2008
i linked it to a table called "members" which contains a ID number field (members-id)
so i can limit the results to members id = $un(which is a username from a session)
I need to join the three tables and limit the results to the specific ID number record
Here is my portion of the code:
$query = "SELECT * FROM members JOIN events_year ON members.id = events_year.id ";
"SELECT * FROM Event JOIN events_year ON Event.EventID = events_year.EventID WHERE username = '$un'";
"SELECT * FROM Date JOIN events_year ON Date.YearID = events_year.YearID WHERE username = '$un'";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
echo $row['Year'];
echo " - ";
echo $row['Event'];
echo "<br>";
}
the notices are almost self-explaining. There are no 'Year' and 'EventName' fields in the resultset. It's difficult (or: impossible) to tell why this happens as you haven't given your table-structure, but i guess this: 'Year' is a field of the date-table, 'EventName' is a field of the event-table - you're only selecting from members so this fields don't occur.
I don't understand why there are three sql-statements but only one is assigned to a variable - the other two are just standing there and do nothing. Please explain this and put more information into your question about what you're trying to achive, what your table-structure looks like and whats your expected result.
I think what you really wanted to do is some kind of joined query, so please take a look at the documentation to see how this works.
finally, i think your query should look like this:
SELECT
*
FROM
members
INNER JOIN
events_year ON members.id = events_year.id
INNER JOIN
Event ON Event.EventID = events_year.EventID
INNER JOIN
´Date´ ON ´Date´.YearID = events_year.YearID
WHERE
members.username = '$un'
Does the field 'Year' exist in the query output ? I suspect not.
the string $query is only using the first line of text:
"SELECT * FROM members JOIN events_year ON members.id = events_year.id ";
and not the others.
The query itself is not returning any fields that are called Year or EventName.
Do a var_dump($row) to find out what is being returned.