Compare two results and show results PHP - php

I'm trying to get two identical IDs and turn them into a name, explaining better I have a table with ID-COMPANY NAME and in the product table ID COMPANY - product name, I would like to match the two and next i will output in excel file.
I tested this code but all the results do not appear to me:
while(($row = mysqli_fetch_array($result)) && ($row2 = mysqli_fetch_array($result2)) )
{
For answer:
db:
1) id - name company - ecc //Table company
2) id_product - product - id_company //Table product
Output excel:
Client 1 - id
Client 2 - id
Name | another information
Client 1 | ecc...
Client 2 | ecc...
Php:
$result = mysqli_query($connect, $query); // FIRST CONNECT TABLE
$result2 = mysqli_query($connect, $query2); // SECOND CONNECT TABLE
if(mysqli_num_rows($result2) && mysqli_num_rows($result)> 0)
{
$output2 .= '
<table class="table" id="table" bordered="1">
';
while($row2 = mysqli_fetch_array($result2))
{
$output2 .= '
<tr>
<td class="grassetto">Company:</td>
<td class="grassetto">'. $row2["id"].'-'. $row2["nomeazienda"].'</td>
</tr>
<tr>
<td class="grassetto">ID:</td>
<td class="grassetto">'. $mese.'/'.$anno.'</td>
</tr>
';
}
$output2 .= '</table>';
$output .= '
<table class="table ops" id="table" border="1">
<tr>
<th>Name Company</th>
<th>Another information</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td align="left">'. $row['id_azienda'] . '</td>
<td align="left">'. $row['nr'] . '</td>
</tr>
';
}
$output .= '</table>';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
header("Pragma: no-cache");
header("Expires: 0");
echo $output2. "\n<br>" .$output."\n";
}
So in second output when i call $result i need to compare ID/ID_COMPANY and write a company name
Edit 2:
That is main query
$query = 'SELECT * FROM products WHERE id_company in ('.$company.') AND product ="'.$products.'"';
}else{
$query = 'SELECT * FROM products WHERE id_company IN ('.$company.') AND product ="'.$products.'" AND last_check LIKE "%'.$anno.'-'.$mese.'%"';
}
$query2 = 'SELECT * FROM company WHERE id in ('.$company.')';
how do you integrate this code?

Use a simple JOIN to get the company name from the company ID in the product table in a single query.
$query = "
SELECT c.name, p.product, ...
FROM company AS c
JOIN product AS p ON c.id = p.id_company
WHERE c.id in ($company) AND p.product = '$product'";

Related

What can I do with this problem with mysql php and table result?

I have this query to list name categories and avg of each one, but the result is not what I want, how to sove this? if you look to image you see that the table resulta don't show correct
$query = $connMysqli->query("SELECT qc.name as cat_name
FROM fhrw_question_categories qc
...
group by Quiz, cat
ORDER BY u.id");
?>
<table>
<tr>
<?php
while($row = $query->fetch_assoc()){
echo '<th>' . $row['cat_name'] . '</th>';
}?>
</tr>
<!-- table continiuous avg-->
<?
$query = $connMysqli->query("SELECT avg(IFNULL(fraction, 0)) * 10 as Media, u.ud as student, qc.name as cat_name
FROM fhrw_question_attempt_steps qc
...
group by Quiz, u.id, cat
ORDER BY u.id ASC");
?>
<?php
while($row = $query->fetch_assoc()){
echo '<tr><td>' . $row['cat_name'] . ' - ' . round($row['Media'],2) . '</td></tr>';
}?>

Student Attendance Report within a PIVOT Table

I am trying to record the attendance of my students that attend my courses. Courses are different lengths and times and I just need to record if a student is (P)resent (L)ate (A)sent. I record the attendance in 1 table and display the records in a pivot table based on the date attended. I am a newby and just can't workout this code to include all the details I need to show. id, bid, fullname, nickname, company_idno, (P)(L)(A).
Please could someone look at my code and tell me how to add this information to the pivot table.
This is what I want to show
This is where I store the information
This is table1
This is table2
At the moment I achieved the look I want but use 2 tables and use CSS to fix the widths of table 1 and place table 2 next to it.
I realize this is terrible practice and of course, I get odd results across different platforms, especially iOS which put a 47px gap between the 2 tables which I can't seem to remove also.
I want just want table 2 to show all the information. I can only show 3 fields, id, date, pla. How to add fullname, nickname and company_idno ??
Table 1
<table id="tblplanames" >
<td id="tdplabc">sid</td>
<td id="tdplabc">bid</td>
<td id="tdplacid" style="text-align: center">Cid</td>
<td id="tdplafn" style="text-align: center">Fullname</td>
<td id="tdplann" style="text-align: center">Nickname</td>
<?php
$sql13="SELECT * FROM students WHERE classno='$id' ORDER BY bluecard_no ASC ";
$sql_row13=mysqli_query($link,$sql13);
while($sql_res13=mysqli_fetch_assoc($sql_row13)) {
$stsid=$sql_res13["id"];
$stidno=$sql_res13["company_idno"];
$stbluecard_no=$sql_res13["bluecard_no"];
$stfullname=$sql_res13["fullname"];
$stnickname=$sql_res13["nickname"];
?>
<tr>
<td id="tdplabc"><a href=edit_student.php?id=<?php echo $stsid ?>><?php echo $stsid; ?></td>
<td id="tdplabc"><a href=edit_student.php?id=<?php echo $stsid ?>><?php echo $stbluecard_no; ?></td>
<td id="tdplacid"><a href=edit_student.php?id=<?php echo $stsid ?>><?php echo $stidno; ?></td>
<td id="tdplafn"><a href=edit_student.php?id=<?php echo $stsid ?>><?php echo $stfullname; ?></td>
<td id="tdplann"><a href=edit_student.php?id=<?php echo $stsid ?>><?php echo $stnickname; ?></td>
</tr>
<?php
}
?>
</table>
Table 2
<?php
$id = $_GET['id'];
$sql = "SELECT DISTINCT date
FROM attendance
WHERE classno = $id
ORDER BY DATE";
$res = $link->query($sql); // mysqli query
while ($row = $res->fetch_row()) {
$dates[] = $row[0];
}
/***********************************
* Table headings *
************************************/
$emptyRow = array_fill_keys($dates,'');
// format dates
foreach ($dates as $k=>$v) {
$dates[$k] = date('d-M', strtotime($v));
}
$heads = "<table id='tblpla'>\n";
$heads .= "<tr><td>sid</td><td>" . join('</td><td>', $dates) . "</td></tr>\n";
/***********************************
* Main data *
************************************/
$sql = "SELECT date, sid, pla, bluecard_no
FROM attendance
WHERE classno = $id
ORDER BY bluecard_no";
$res = $link->query($sql);
$sid='';
$tdata = '';
while (list($d, $sn, $s, $bcn) = $res->fetch_row()) {
if ($sid != $sn) {
if ($sid) {
$tdata .= "<tr><td>$sid</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
}
$rowdata = $emptyRow;
$sid = $sn;
}
$rowdata[$d] = $s;
}
$tdata .= "<tr><td>$sid</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
$tdata .= "</table\n";
echo $heads;
echo $tdata;
?>
SELECT c.olumns
, y.ou
, a.ctually
, w.ant
FROM students s
JOIN attendance a
ON a.classno = s.classno
WHERE s.classno = :id
ORDER
BY s.bluecard_no ASC
, a.date";
public function get_members_for_attendence()
{
$date = $this->input->post('choose_date');
$sql = 'SELECT a.date, c.member_name,c.member_join_date,c.member_payment_date,c.member_exp_date,c.member_payment_date,c.member_contact, c.member_reg_id,
CASE
WHEN a.status = "absent" THEN "Absent"
WHEN a.status = "present" THEN "Present"
ELSE "Not Taken"
END as attendence_status
FROM member_reg AS c
LEFT JOIN attendence AS a ON a.member_reg_id = c.member_reg_id AND a.date = "'.$date.'"';
$query = $this->db->query($sql);
return $query->result();
}
For CodeIgniter 3
it worked for me

Printing out each row from a MYSQL table individually in PHP

I am trying to individually print out each line from my SQL database in PHP. I am trying to do this so each line that is retrieved, it can act like a link which will direct the user to another page. For example, the current SQL query will output the Category names from the database Category, i would like it to output all the values from that table but have it so each one has a different redirect link to another page which clicked on.
$query = "SELECT CATEGORY_NAME
FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
$results = #mysqli_query ($conn, $query);
$numrows = mysqli_num_rows($results);
if ($results) {
if ($numrows >0) {
echo '
<table>
<tr>
<td><strong>Categories</stong></td>
</tr>';
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td>' . $row['CATEGORY_NAME'] . '</td>
</tr>
';
}
mysqli_free_result ($results);
Such as,
Category
__________
PS4
XBOX
i can click on PS4 and it would take me to another page, i know how to do this with a href and then print out the row in sql however, i'm not sure how do print out each row individually without printing them out using $row['CATEGORY_NAME'].
Thank you for any help
You could just store the link to which it needs to be redirected in a new column CATEGORY_URL along with the CATEGORY_NAME and then print it out like so:
$query = "SELECT CATEGORY_NAME, CATEGORY_URL FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
$results = mysqli_query($conn, $query);
$numrows = mysqli_num_rows($results);
if ($numrows > 0) {
echo '
<table>
<tr>
<td><strong>Categories</stong></td>
</tr>';
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '
<tr>
<td>
'.$row['CATEGORY_NAME'].'
</td>
</tr>';
}
mysqli_free_result ($results);
} else {
// no results found
}
OR if you just want to pass it as a url parameter, you just need to modify if like so:
'.$row['CATEGORY_NAME'].'
OR
'.$row['CATEGORY_NAME'].'
Something like? (I'm not 100% sure if I understand your question)
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="somelink.php?cat=' . $row['CATEGORY_NAME'] . '">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
UPDATE
If a category is named playstation, playstation.php would be linked to...
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="' . $row['CATEGORY_NAME'] . '.php">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
Though above would work a wild guess is that you're looking for something completely different. The thing you are looking for is essentially a way to present information for playstation, a way to present information about PS4 etc depending on what user clicks on!? In that case you should do something like this instead:
//Include ID of category in your sql-statement
$query = "SELECT ID, CATEGORY_NAME
FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
....
....
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="presentinfo.php?id=' . $row['ID'] . '">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
and create presentinfo.php file where you fetch information about the category, based on the id given in the url. (e.g.select from categories id={id given in url})

Create HTML table based on Database Values

I have a database that has information on actors, roles, and movies.
On an HTML page I have the user input the first name and last name of an actor.
This form is then submitted to my PHP page where I look through my database, and provide the user with a 3 column html table with the following information: Movie Name the actor has worked in, Role the actor played, and year of movie.
All these information are in separate database tables. Examples of what each table looks like is below:
actor table contains:
'id', 'first_name', 'last_name', and 'gender' stored in the following way - (933,'Actor','Name','M')
role table contains:
'actor_id', 'movie_id', 'role stored in the following way - (16844,10920,'Role')
movies table contains:
'id', 'name', 'year', 'rank' stored in the following way - (306032,'Snatch.',2000,7.9)
I have to look through and correlate all the data into a table. This is what I have so far:
$sql ="SELECT id, first_name, last_name FROM actors";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)) {
if ($row['first_name'] == $firstname && $row['last_name'] == $lastname) {
$actorID = $row['id'];
echo "$actorID<br>";
}
}//end while
$sql2 = "SELECT actor_id, movie_id, role FROM roles";
$result2 = mysql_query($sql2);
while ($row=mysql_fetch_array($result2)) {
if ($row['actor_id'] == $actorID) {
$movieID = $row['movie_id'];
$actRole = $row['role'];
echo "$movieID <br>";
echo "$actRole <br>";
}
} //end while
$sql3 = "SELECT id, name, year FROM movies";
$result3 = mysql_query($sql3);
while ($row=mysql_fetch_array($result3)) {
if ($row['id'] == $movieID) {
$movieName = $row['name'];
$movieYear = $row['year'];
echo "$movieName <br>";
echo "$movieYear <br>";
}
} //end while
echo '
<table>
<thead>
<tr>
<th> Movie Name </th>
<th> Actor Role </th>
<th> Release Date </th>
</tr>
</thead>
<tbody>
<td>'. $movieName .'</td>
<td>'. $actRole. '</td>
<td>'. $movieYear. '</td>
</tbody>
</table>';
?>
My solution works -- just a mediocre way of doing it
You don't have assign the variables $firstname and $lastname. Apart from that your if condition is wrong true every time.
if ($row['first_name'] = $firstname && $row['last_name'] = $lastname) {
Should be:
if ($row['first_name'] == $firstname && $row['last_name'] == $lastname) {
Also check what you want to do with the echo $row['first_name'][0];
Note that mysql_* functions are deprecated so you better use mysqli or PDO.
EDIT:
You can select all actors that have play in a movie using the following query. You can adjust to take only the information you need changing SELECT clause or using WHERE.
$sql = "
SELECT aa.id AS actor_id, aa.first_name, aa.last_name, cc.name, cc.year
FROM actor AS aa
INNER JOIN role AS bb
ON aa.id = bb.actor_id
INNER JOIN movies AS cc
ON cc.id = bb.movie_id";
EDIT 2: (from comments)
You can use the following code:
$conn = mysqli_connect("localhost", "db_username", "your_password", "your_database");
$query = "
SELECT aa.id AS actor_id, aa.first_name, aa.last_name, cc.name AS movie_name, cc.year AS release_year
FROM actor AS aa
INNER JOIN role AS bb
ON aa.id = bb.actor_id
INNER JOIN movies AS cc
ON cc.id = bb.movie_id";
$result = mysqli_query($conn, $query);
echo '
<table>
<thead>
<tr>
<th>Actor id</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Movie name</th>
<th>Release date</th>
</tr>
</thead>
<tbody>';
while($row = mysqli_fetch_array($result)) {
echo '<td>'.$row['actor_id'].'</td>';
echo '<td>'.$row['first_name'].'</td>';
echo '<td>'.$row['last_name'].'</td>';
echo '<td>'.$row['movie_name'].'</td>';
echo '<td>'.$row['release_year'].'</td>';
}
echo '
</tbody>
</table>';

inside while loop data not displaying in php

Hi in the below i want to show Consultation Charges values for that i took the td.But in that td not displying anything even td also not showing.
After executing this query i want to find the no. of rows based on the rows i want to display the data.
My expected output:
Bill Particular Bill Sub Particular Doctor Date Dis. Amt.
Consultation Charges:
all the values based on no of rows.
php
<table width="100%">
<th>Bill Particular</th>
<th>Bill Sub Particular</th>
<th>Doctor</th>
<th>Date</th>
<th>Dis. Amt.</th>
<th>Charge</th>
<th>No. of Times</th>
<th>Amount</th>
</table>
<tr><th colspan=2>Consultation Charges:</th>
<?php
$div_options = array();
$sql = "SELECT ibp.ipd_bp_id, ibp.bp_id, bp.bp_name, ibp.bsp_id, bsp.bsp_name, ibp.doctor_id, ab.employee_name doctor, ibp.date date, ibp.amount charge, ibp.discount_amount discount, ibp.no_of_time, (ibp.no_of_time * ibp.amount) total_amount
FROM bill_particular_master bp
INNER JOIN ipd_bill_particular ibp ON ibp.bp_id = bp.bp_id
LEFT OUTER JOIN bill_sub_particular bsp ON bsp.bsp_id = ibp.bsp_id
LEFT OUTER JOIN address_book ab ON ab.employee_id = ibp.doctor_id
WHERE ibp.ipd_reg_no = '$ipd_no'
AND bsp.consultant =1
AND bsp.package = 0
AND bsp.admission = 0
AND bp.bp_name != 'Scan Charges'
AND bp.bp_name !='Procedure'";
$sth = $dbh->query($sql);
//$row=$dbh->fetch();
$i=1;
while($row=$sth->fetch(PDO::FETCH_ASSOC)){
$sub_arr['bp_name'] = $row['bp_name'];
$sub_arr['bsp_name'] = $row['bsp_name'];
echo "<tr>
<td>Here is the text - " . $sub_arr['bp_name'] . "</td>
<td>The ID of the text is - " .$sub_arr['bsp_name'] . "</td>";
if($i !== 0) {
echo "<td>The ID of the previous entry is - " .$sub_arr['bp_name'] . "</td>";
}
else {
echo "<td> </td>";
}
echo "</tr>";
$i = $row['bp_name'];
}
?>
</tr>
echo "<td>The ID of the previous entry is - " . $row['bp_name'] . "</td>";
You have there variable thats not even defined in your code.

Categories