I'm new to php, but I have already made some query's for my webshop.
I have a little problem now with making the invoice.
I'm trying put the invoice data from the database in a table in my website, but it duplicates itself.
Product_id | Product name | amount | price | subtotal|
2 kippensoep 1 €3 €3
2 kippensoep 1 €3 €3
2 kippensoep 1 €3 €3
4 Loempia's 1 €10 €10
4 Loempia's 1 €10 €10
4 Loempia's 1 €10 €10
4 Loempia's 1 €10 €10
So as you can see, the product_id , product_names are duplicated.
It's strange because in the database it isn't duplicated.
Here's the php code.
<?php
$maxfac1 = mysql_fetch_array(mysql_query("SELECT MAX(transactie.factuur_id) FROM transactie , account WHERE transactie.id = $session_user_id"));
unset ($maxfac1[0]);
$maxfactuur_id = implode (" ",$maxfac1);
$productinfo = mysql_query("SELECT * FROM producten, transactie, factuur WHERE producten.product_id = factuur.product_id AND factuur.factuur_id = $maxfactuur_id AND factuur.gebruikers_id= $session_user_id");
$totaal=0;
$subtotaal=0;
while ($row = mysql_fetch_array($productinfo))
{
?>
<tr>
<td><?php echo $row['product_id']?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['amount'] ?></td>
<td>€<?php echo $row['price'] ?></td>
<?php $subtotaal = ($row['price']*$row['amount']);?>
<td>€<?php echo "$subtotaal" ;$totaal+= $subtotaal;?></td>
</tr>
<?php
}
?>
?>
I hope you guys can help me and find a solution.
EDIT
session_user_id checks if the user is logged in. Then it returns the id from the user who is logged in.
You are missing a link with the transactie table. So you have a Cartesian join appearing.
Add the proper where condition to fix that.
As you didn't provide any table structure, I can't help you more than that!
edit
on an other level, do not use mysql extension since it is deprecated!
Try to check rows are unique. And add GROUP BY producten.product_id to your query.
Try to add the DISTINCT keyword.
$productinfo = mysql_query("SELECT DISTINCT * FROM producten, transactie, factuur WHERE producten.product_id = factuur.product_id AND factuur.factuur_id = $maxfactuur_id AND factuur.gebruikers_id= $session_user_id");
Related
I have the following table:
news(id,cat_id,headline,sub_top_priority)
I create drag and drop sortable list.
It change the sub_top_priority column order in database.
But i need this to change order of sub_top_priority column according to category.
for example.
Database has 6 rows
id cat_id headline sub_top_priority
1 15 good 1
2 15 Bad 2
3 15 Nice 3
4 15 Fine 1
5 16 Test1 2
6 16 Test2 3
if I Drag the second row and drop on first row then it only change order of category = 15 not of category = 16. category 16 sub_top_priority column remains as it is.
plz help me to make update and plz suggest what should i change in code...to update sub_top_priority column order according to cat_id.
I Try to change id with cat_id but this change all category in same sequence.
<?php
while ($sub_top = mysql_fetch_array($sub_top_select))
{
?>
<li id="recordsArray1_<?php echo $sub_top['id']; ?>"> <?php echo $sub_top['category_name']; ?> : <?php echo $sub_top['headline']; ?></li>
<?php } ?>
<?php
if(isset($_POST['change']))
{
$change = mysql_real_escape_string($_POST['change']);
$updateRecordsArray1 = $_POST['recordsArray1'];
if ($change == "updatesubtop")
{
$listingCounter1 = 1;
foreach ($updateRecordsArray1 as $recordIDValue1)
{
$query1 = "UPDATE news SET sub_top_priority = " . $listingCounter1 . " WHERE id = " . $recordIDValue1;
mysql_query($query1) or die($query1."<br/><br/>".mysql_error());
$listingCounter1 = $listingCounter1 + 1;
}
}
}
?>
Good Day, this is just a follow-up question from Referrence 1 here on stackoverflow. Though it was now being solved partially, I still have issues with eid. The multiple insert through mysqli is a bit working (I just said a bit since only 4 our of 6 tables were being inserted with values), but when I checked if the foreign key for eid are also been copied by mysqli_insert_id() I'm surprised to see that the foreign key eid got 0 values.
It was like this (NOTE: The ff: data are not the actual one):
Table: Employee [eid is it's primary key]
eid employee_name address etc
1002 employee A Beside U ♥
1003 employee B Pluto ♥
Table: Contact [eid is it's foreign key]
eid telno email
0 911 kingCorbra#hey.com
0 *** universe#hey.com
Table: Work Experience [eid is it's foreign key]
eid Company Name start date end date
0 Windows Macintosh 2012-12-01 2012-12-02
0 Micro Entertainment LG 2012-12-31 2013-01-01
*Other Tables are not included in the samples but part of the query.*
As you know, I have used mysqli_insert_id() during the insert statement again please see the Referrence 1, and luckily it work but only for 4 out of 6 actual tables. And when I checked the eid column it shows 0 values.
Here is my select statement (not the real one):
` <table cellspacing="0" width="100%">
<thead>
<tr>
<th><strong>Name:</strong></th>
<th><strong>Date of Birth:</strong></th>
<th><strong>Birthplace:</strong></th>
<th><strong>Gender:</strong></th>
<th><strong>Email Add:</strong></th>
<th><strong>Contact#:</strong></th>
<th><strong>Address:</strong></th>
</tr>
</thead>
<?php
include('db.php');
$sql=mysqli_query("SELECT emp.eid,emp.fname,
emp.mname,emp.lname,
emp.age,emp.gender,
emp.birthday,emp.birthplace,
emp.citizenship,emp.status,
emp.sss,emp.philhealth,
emp.tin,emp.height,
emp.weight,
con.address,
con.province,con.postcode,
con.telno,con.mobile,
con.email,con.alternate,
educ.elem,
educ.egrad,educ.high,
educ.hgrad,educ.college,
educ.cgrad,
ems.position,ems.hireDate,ems.job_desc,ems.basic,ems.salary,
w.company_name,w.position,w.desc,w.startDate,w.endDate,
fam.fatherName,fam.motherName,fam.sibling,fam.spouse,fam.children
FROM employee AS emp INNER JOIN contact AS con ON con.eid='emp.eid'
INNER JOIN educ AS educ ON educ.eid='emp.eid'
INNER JOIN employment AS ems ON ems.eid='emp.eid'
INNER JOIN work AS w ON w.eid='emp.eid'
INNER JOIN family AS fam ON fam.eid='emp.eid' WHERE emp.eid='$id'");
$counter=0;
while($row=mysqli_fetch_assoc($sql))
{
$cname = $row['name'];
$cbday = $row['birthday'];
$pob = $row['pob'];
$cgen = $row['gender'];
$email = $row['email'];
$contact= $row['contact'];
$add = $row['address'];
if($counter%2)
{
?>
<tbody>
<?php } else { ?>
<tr>
<?php } ?>
<td><?php echo $cname; ?></td>
<td><?php echo $cbday; ?></td>
<td><?php echo $pob; ?></td>
<td><?php echo $cgen; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $contract; ?></td>
<td><?php echo $add; ?></td>
</tr>
</tbody>
<?php
$counter++;
}//while-end
?>
</table>`
To make things much weird, let me tell you that when I tried to display values using mysqli_fetched_assoc() the query doesn't seem to work as in on my index page nothing else displays. Though the db has its record. But when I used mysql [though many have claimed it to be deprecated] it seems to work fine eventhough I had mysqli used for the insert statement. You guys get my point right? I used mysqli for insert statement and mysql for display and it works fine except for the eid of course.
Any idea?
$sql=mysql_query
Will never ever work with
mysqli_insert_id
^
You need to use the same API for both calls. Use MySQLi throughout.
I have a project on student ranking. The sample school does three terminal exam in a year. I created three different tables for each terminal exam, (i.e. firstermsar for first, secondtermsar for second, thirdtermsar for third term respectively).
Table structure is like this:
id studentid matca1 matca2 matexam engca1 engca2 engexam
1 2 15 14 40 12 10 60
2 1 10 5 56 9 13 35
3 4 11 9 45 14 17 40
4 5 14 1 50 20 0 60
Students with id 4 and 5 are in class1 while students 2 and 1 are in in class2. I have a seperate table for classes and also seperate table for student profile info.
Now, I already output the result slip but without ranks which looks like this
StudentId: 2 Full Name: Fawaz James Class: Pry 2 Overall Position: -
1stAss 2ndAss ExamScore Grade Position Rmks
Mark Obtainable 20 20 60
English 12 10 60 A - Excellent
Mathematics 15 14 40 B - V. Good
Though am a starter, and my code is a mess, its provided underneath. Moreover I need help with calculating the overall position on class basis and subject position on class basis. Please help me as my project depends on this for approval and am in my deadline week before I got introduced to stack Overflow.
$query = "SELECT * FROM firsttermsars
WHERE studentID=$stuID AND year=\"$_SESSION[year]\""; #27
$result = mysql_query($query)
or die ("Couldn't execute query.");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<h2> First Term Stint Report Sheet for $namet during the SESSION $year</h2>";
echo "<table class='table1'>";
echo "<tr>";
echo "<td></td>";
echo "<td><b>1st Assessment</b></td><td><b>2nd Assessment</b></td>
<td><b>Mid Term<br />Exam</b></td><td><b>Mid Term<br />Total Score</b></td><td><b>Grade<br />Point</b></td></tr>";
echo "<tr> <td><b>Mark Obtainable</b></td><td><b>30</b></td><td><b>20</b></td><td><b>50</b></td><td><b>100</b></td><td><b>||=||</b></td></tr><tr>";
$tetal = $row['thirdengtest']+$row['thirdengexam']+$row['eng1'];
if ($tetal >="1") {
$q2 = "1";
$sq2 = "100";
echo "<td>English Language: </td>";
echo "<td>{$row['eng1']}</td><td>{$row['thirdengtest']}</td><td>{$row['thirdengexam']}</td> <td>";
$total1 = $row['thirdengtest']+$row['thirdengexam']+$row['eng1'];
echo $total1."</td><td>";
if ($total1>="75")
echo "A1";
elseif ($total1>="70")
echo "B2";
elseif ($total1>="65")
echo "B3";
elseif ($total1>="60")
echo "C4";
elseif ($total1>="55")
echo "C5";
elseif ($total1>="50")
echo "C6";
elseif ($total1>="45")
echo "D7";
elseif ($total1>="40")
echo "E8";
elseif ($total1>="1")
echo "F9";
elseif ($total1=="0")
echo "-";
else
echo "F";
echo "</td></tr>";
}
$tetal = $row['thirdmathtest']+$row['thirdmathexam']+$row['math1'];
if ($tetal >="1") {
$q3 = "1";
$sq3 = "100";
echo "<tr>";
echo "<td>Mathematics </td>";
echo "<td>{$row['math1']}</td><td>{$row['thirdmathtest']}</td> <td>{$row['thirdmathexam']}</td> <td>";
$total2 = $row['thirdmathtest']+$row['thirdmathexam']+$row['math1'];
echo $total2."</td><td>";
if ($total2>="75") echo "A1"; elseif ($total2>="70") echo "B2"; elseif ($total2>="65") echo "B3"; elseif ($total2>="60") echo "C4"; elseif ($total2>="55") echo "C5"; elseif ($total2>="50") echo "C6"; elseif ($total2>="45") echo "D7"; elseif ($total2>="40") echo "E8"; elseif ($total2>="1") echo "F9"; elseif ($total2=="0") echo "-"; else echo "F";
echo "</td></tr>";
}
?>
I hope you can help me like this, and hope is not complicated. I wanted to add the picture of the output but its complaining i don't have the required reputation
Firstly, in the database, make a new column for total('Total') and ('Grade'), and when you are submitting the marks details to your database, add the marks and grade and then submit the total to the database, instead of calculating them while retrieving the information.
Second, when you are selecting students from the database, use the SQL command:
SELECT * FROM firsttermsars ORDER BY Total DESC
More info on ORDER BY
This will automatically return the list of students in descending order of their total marks.
Similarly, for subject ranking, you can have a separate table, where ranking is done on subject marks
Then, while retrieving the data,
<table>
<?php
while ($i<$num) //num is number of rows
{
?>
<tr><td><?php echo mysql_result($result,$i,"Name"); ?></td>
//... Similarly for all clumns
<?php
}
?>
</table>
This should make the table with all students ranked according to their total marks. Similarly you can make a table for ranking by subject marks.
This is how i would solve the problem. Hope I helped!
Although i am late, but it may help some one.
First of All you should calculate Obtained marks and store in database table.
For simplicity i am showing just 3 column.
Id Name Obtained
1 A 100
2 B 58
3 L 88
4 F 102
5 C 99
Insert the row in table , then run this query to get Position of student.
SELECT id, name, Obtained, FIND_IN_SET( score, (
SELECT GROUP_CONCAT( score
ORDER BY score DESC )
FROM scores )
) AS position
FROM scores
WHERE id = 5
Detail here https://dba.stackexchange.com/questions/13703/get-the-rank-of-a-user-in-a-score-table
For my current progress I need to create a table like the one below
pid cid eid name value
1 1 4 name ab
2 1 5 amt 2
3 1 4 name cd
4 1 5 amt 4
Instead of creating the table like this
pid cid name amt
1 1 ab 22
2 1 cd 4
Anyhow created table as my wish with the below code
<table width="1204" height="100" border="1">
<tr>
<?php $sqlname="Select * From elements where cat_id='1' order by e_id ";
$resname=mysql_query($sqlname);
while($rowname=mysql_fetch_array($resname)){
?>
<td colspan="2"><?php echo $rowname['lable_name']; ?></td>
</tr>
<tr>
<?php $i=0;
$sqlprolist="select value from products_list where name='".$rowname['lable_name']."' and e_id='".$rowname['e_id']."'";
$resprolist=mysql_query($sqlprolist);
while($rowprolist=mysql_fetch_array($resprolist)){
$i++;
?>
<td><?php echo $rowprolist['value'];?></td>
<?php if($i%8==0){
?>
<tr></tr>
<?php }?>
<?php }?>
</tr>
<?php }?>
</table>
But I don't have any idea to retrieve data from the table for processing.
thanks
as by following martin the table created as like the below table
pid cid eid name value
12 1 4 name abc
1 1 4 name cde
13 1 5 code 12
2 1 5 code 14
how to split up the data
like
name code breeder quality size
abc 12 121 121 22
acfd 34 164 22 22
thanks
It's difficult to help you without seeing database structure. Please share it with us, you might get better answers.
Anyway, I suppose you have two tables, elements and products_list. It looks like you need to lookup the value column in the products_list for every row in the elements table. You can merge these table into one result set using a single SQL query:
SELECT e.p_id, e.cat_id, e.e_id, e.lable_name, p.value
FROM elements e, products_list p
WHERE e.cat_id='1' AND
p.name = e.lable_name AND p.e_id = e.e_id
ORDER BY by e.e_id
Note that the e.p_id is just a guess, you have not shared with us, where the "pid" column value gets from. Also not sure, if you actually need to match the rows using the p.name = e.lable_name. If e_id is primary key, you might do with p.e_id = e.e_id only.
What's the point of cid column? If it is indeed the cat_id column in database, why do you need it in HTML table, if you filter the elements to cat_id=1 anyway? Was that intentional?
You can now take the results of the query and simply output it to a HTML table row by row like:
<table width="1204" height="100" border="1">
<tr>
<th>pid</th>
<th>cid</th>
<th>eid</th>
<th>name</th>
<th>value</th>
</tr>
<?php
$sqlname =
"SELECT e.p_id, e.cat_id, e.e_id, e.lable_name, p.value ".
"FROM elements e, products_list p ".
"WHERE e.cat_id='1' AND ".
"p.name = e.lable_name AND p.e_id = e.e_id".
"ORDER BY e.e_id";
$resname = mysql_query($sqlname);
while ($row = mysql_fetch_array($resname))
{
?>
<tr>
<td><?php echo $row['p_id'];?></td>
<td><?php echo $row['cat_id'];?></td>
<td><?php echo $row['e_id'];?></td>
<td><?php echo htmlspecialchars($row['lable_name']);?></td>
<td><?php echo $row['value'];?></td>
</tr>
<?php
}
?>
</table>
This is just a first step. It's unlikely this is correct answer. But I hope it helps you understand, what are you trying to do and to your question.
This is hardly production-grade code. But I wanted to do as little changes as possible, so you can see, what is the actual change from your code.
Also note that the mysql_ functions are deprecated. See the big red box on PHP documentation page.
Can someone help me with this:
I have a mySQL database and I would like to do a simple search amoung the items, here is an example of my database named "orders"
id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
How can I group by duplicated data and display it like this:
<select name="costumer">
<option>Hansen</option>
<option>Nilsen</option>
<option>Jensen</option>
</select>
I know it is the GROUP BY command, but dont know how to display it on a PHP script file
Try this:
$dbconn = new mysqli();
$dbconn->connect("localhost","root","","test");
if($dbconn->connect_errno ){
echo "Connection Failed";
}
$query = "SELECT DISTINCT Customer FROM `Orders`";
$result = $dbconn->query($query);
echo "<select name=\"costumer\">";
while($row = $result->fetch_array())
{
echo "<option>".$row["Customer"]."</option>";
}
echo "</select>";
you can use DISTINCT on this since you only need to have 1 column,
SELECT DISTINCT Customer FROM `Orders`