I have some mysql table like this:
| IDPRAKTIKAN | NAMAUSER | MODULKE | NILAI | STATUS_TUGASAKHIR | TAHUNAJARAN | SEMESTER |
-----------------------------------------------------------------------------------------------
| 3332081478 | Ade Dwinanto | 0 | 3 | 1 | 09/10 | 3 |
| 3332081478 | Ade Dwinanto | 1 | 55 | 1 | 09/10 | 3 |
| 3332081478 | Ade Dwinanto | 2 | 54 | 1 | 09/10 | 3 |
| 3332081478 | Ade Dwinanto | 3 | 45 | 1 | 09/10 | 3 |
| 3332081478 | Ade Dwinanto | 4 | 62 | 1 | 09/10 | 3 |
| 3332081478 | Ade Dwinanto | 5 | 20 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 0 | 3 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 1 | 55 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 2 | 53 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 3 | 47 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 4 | 60 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 5 | 20 | 1 | 09/10 | 3 |
This is my Data On MySQL (On sqlfiddle.com)
And This is My PHP Code:
<?php
$setting_tahunajaranget = $_GET['setting_tahunajaran'];
$setting_kodepraktikumget = $_GET['setting_kodepraktikum'];
$nama_praktikum = $_GET['nama_praktikum'];
$setting_semesterget = $_GET['setting_semester'];
$query2 = "SELECT `nilai` AS semester FROM `nilaipraktikum` WHERE `modulke`= 0 LIMIT 0, 30 ";
$data = mysql_fetch_row(mysql_query($query2));
$query = "SELECT `idpraktikan`,namauser,`modulke`, `nilai`,`status_tugasakhir`,`tahunajaran`,`semester` FROM `nilaipraktikum` LEFT JOIN tabeluser ON tabeluser.npmuser = nilaipraktikum.idpraktikan WHERE `tahunajaran` = '$setting_tahunajaranget' AND`kodepraktikum` = '$setting_kodepraktikumget' AND `semester` = '$setting_semesterget' ";
$result = mysql_query($query);
if ($data = mysql_fetch_row($result)>=1)
{
$nilai = $data['nilai'];
if ($nilai = $setting_semesterget);
echo "<table width='auto' border='1' cellpadding='3' cellspacing='3' align='center'>
<tr>
<td align='center' valign='middle'><strong>No</strong></td>
<td align='center' valign='middle'><strong>Nama</strong></td>
<td align='center' valign='middle'><strong>NPM</strong></td>";
for($i=1;$i<=$jml_modul;$i++) {
echo "<td align='center' valign='middle'><strong>Modul Ke-".$i."</strong></td>";
}
echo "<td align='center' valign='middle'><strong>Rata-Rata</strong></td>";
if ($setting_status_ta=0)
{
}
if ($setting_status_ta=1)
{
echo"<td align='center' valign='middle'><strong>Nilai Tugas Akhir</strong></td>";
}
echo "<td align='center' valign='middle'><strong>Nilai Akhir</strong></td>
<td align='center' valign='middle'><strong>Grade</strong></td>
</tr>";
while(list($nim,$nama,$modulke,$nilai,$nilai_tugasakhir) = mysql_fetch_row($result)) {
$array_nim[$nim] = $nim;
$array_nama[$nim] = $nama;
$array_nilai[$nim][$modulke]= $nilai;
$array_nilaita[$nim][$nilai_tugasakhir]= $nilai_tugasakhir;
}
foreach($array_nim as $nim) {
echo "<tr><td align='center' valign='middle'>".(++$nomor)."</td>";
echo "<td align='center' valign='middle'>".$nim."</td>";
echo "<td align='center' valign='middle'>".$array_nama[$nim]."</td>";
for($i=1;$i<=$jml_modul;$i++)
{
echo "<td align='center' valign='middle'>".$array_nilai[$nim][$i]."</td>";
$nilaimodul += (array_sum(array($array_nilai[$nim][$i])));
}
$ratarata = ($nilaimodul);
echo "<td align='center' valign='middle'>".round ($ratarata,2)."</td>";
$ntugasakhir = $array_nilai[$nim][$jml_modul-($jml_modul-1)];
if ($setting_status_ta=0)
{
}
if ($setting_status_ta=1)
{
echo "<td align='center' valign='middle'>".round ($ntugasakhir,2)."</td>";
}
$nialiakhirpraktikum = $ntugasakhir+$ratarata;
echo "<td align='center' valign='middle'>".round ($nialiakhirpraktikum,1)."</td>";
if ($nialiakhirpraktikum>79)
$grade="A";
else if ($nialiakhirpraktikum<=79 AND $nialiakhirpraktikum>67)
$grade="B";
else if ($nialiakhirpraktikum<=67 AND $nialiakhirpraktikum>55)
$grade="C";
else if ($nialiakhirpraktikum<=55 AND $nialiakhirpraktikum>44)
$grade="D";
else
$grade="E";
echo "<td align='center' valign='middle'>".$grade."</td>";
echo "</tr>";
}
echo "</table>";
echo "<br/> <a href='javascript:history.back()'>Klik Disini Untuk Kembali</a>";
}
?>
my question, why for the Rata-Rata (is average NILAI) column summed array results with the results of the previous array?
For more details, please see the picture here. (Link Picture: https://www.diigo.com/item/image/3gviv/3m98?size=o)
whereas the results I want to average a column like this picture. (Link Picture: https://www.diigo.com/item/image/3gviv/6ke4?size=o)
what is wrong with my script?
NB:
column Nilai Tugas Akhir Nilai Akhir Grade is not yet fixed.
Just add initialization variable
$nilaimodul = 0; // try adding this to your code
for($i=1;$i<=$jml_modul;$i++)
{
echo "<td align='center' valign='middle'>".$array_nilai[$nim][$i]."</td>";
$nilaimodul += (array_sum(array($array_nilai[$nim][$i])));
}
$ratarata = ($nilaimodul);
Related
In my program, the following query and code generate the following table like this:
+-------+---------+
|ward_id|Sub_block|
+-------+---------+
| 1 | A1 |
+-------+---------+
| 1 | B1 |
+-------+---------+
| 1 | C1 |
+-------+---------+
| 2 | D1 |
+-------+---------+
| 2 | E1 |
+-------+---------+
| 2 | F2 |
+-------+---------+
| 3 | K1 |
+-------+---------+
| 3 | G2 |
+-------+---------+
| 3 | I3 |
+-------+---------+
Here is my code that generate the above table.
if(isset($_POST["union_id"]) && !empty($_POST["union_id"]))
{
//Get all union data
$query = $mysqli->query("SELECT * FROM test_epi_table WHERE union_id = ".$_POST['union_id']);
//Count total number of rows
$rowCount = $query->num_rows;
//Display unions list
if($rowCount > 0)
{
echo "<hr>";
echo "<h3 align='center'>EPI Schedule</h3>";
echo "<table class='table table-bordered'>
<tr>
<th>Ward No</th>
<th>Sub-Block</th>
</tr>";
while($row = $query->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['ward_no'] . "</td>";
echo "<td>" . $row['sub_block_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<hr>";
}
else
{
echo "<br>";
echo "<h3 align='center'>Sorry, No Available Information!</h3>";
echo "<hr>";
}
}
Now I like to create the table like this. Mostly I think it is a rowspan task.
+-------+---------+
|ward_id|Sub_block|
+-------+---------+
| | A1 |
+ +---------+
| 1 | B1 |
+ +---------+
| | C1 |
+-------+---------+
| | D1 |
+ +---------+
| 2 | E1 |
+ +---------+
| | F2 |
+-------+---------+
| | K1 |
+ +---------+
| 3 | G2 |
+ +---------+
| | I3 |
+-------+---------+
what kind of changes do I need to add in my actual php code? Please help me out. Thanks in advance.
Use Inner query is one of the good way also you can use right outer join if you want it to be more efficient.
I have a question about PHP query.
I have 2 tables.
First Table:
|id_table1 | first_name | last_name |
|----------|------------|-----------|
| 1 | John | Doe |
| 2 | Doe | John |
Second Table:
|id_table2 | hobby | age | id_table1|
|----------|--------|-----|----------|
| 1 |football| 17 | 1 |
| 2 |swimming| 18 | 2 |
I want to make table like this:
| John Doe | | Doe John |
|----------------| |--------|------|
| Hobby | Age | | Hobby | Age |
|----------|-----| |--------|------|
| football | 17 | |swimming| 19 |
|basketball| 18 |
What is the syntax to make that table in php? May be using foreach, but how?
Thanks.
The code
<?php
$ftable="select * from ftable";
$stable="select * from stable";
$ftable1= mysqli_query($conn, $ftable);
$stable1= mysqli_query($conn, $stable);
foreach ($ftable as $row) {
echo "
<tr>
<th style='text-align: center' colspan='2'>".$row['first_name']."</th>
</tr>
<tr>
<th>Hobby</th>
<th>Age</th>
</tr>";
foreach ($ftable1 as $row1) {
echo "
<tr>
<td>".$row1['hobby']."</td>
<td>".$row1['age']."</td>
<tr>";
}
}
?>
Just save the id number of ftable, then test for it in your 'for' loop for the second table, like this:
<?php
$ftable="select * from ftable";
$stable="select * from stable";
$ftable1= mysqli_query($conn, $ftable);
$stable1= mysqli_query($conn, $stable);
foreach ($ftable1 as $row) {
$id_table1 = $row["id_table1"];
echo "<tr><th style='text-align: center' colspan='2'>"
.$row['first_name']."</th></tr>"
."<tr><th>Hobby</th>"
."<th>Age</th></tr>";
foreach ($stable1 as $row1) {
if ($row1["id_table1"] == $id_table1) {
echo "<tr><td>$row1['hobby']</td><td>$row1['age']</td></tr>";
}
}
}
Basically I want to do a search by category and title. My problem is that the two are located in separate tables. I was thinking of putting 2 variables in the mysqli_num_rows or mysqli_fetch_array but I don't think it's the right idea. The $search variable is working already but I don't know what I will do for $searchcat that is a different table.
<table border="1">
<tr>
<th>ID</th>
<th>Survey Title</th>
<th>Category</th>
</tr>
<?php
if (isset($_POST['submit']))
{
include 'testdb.php'; //connection is written in other page (db.php)
$var =$_POST['search'] ;
$searchtype = $_POST['searchtype'];
$my_query="SELECT s.survey_id, s.title,c.categoryname
FROM survey_header as sh
JOIN survey AS s ON sh.survey_id=s.survey_id
JOIN category AS c ON sh.category_id=c.category_id
WHERE $searchtype LIKE '%".$var."%'
ORDER BY title ASC";
$get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());
if (mysqli_num_rows($get_data) > 0 )
{
echo "<h3>Search results:</h3>";
while($show = mysqli_fetch_array($get_data))
{
$id = $show['survey_id'];
$title = $show['title'];
$category = $show['categoryname']; //
echo "<tr align='center'>";
echo "<td><font color='black'>" .$id. "</font></td>";
echo "<td><font color='black'>" .$title. "</font></td>";
echo "<td><font color='black'>" .$category. "</font></td>";
}
}
else{
echo "No Records found!";
}
}
?>
</table>
</body>
This is table category (categoryname is what I need)
+-------------+---------------+-------------+
| category_id | categoryname | datecreated |
| 1 | Philosophical | |
| 4 | Political | |
| 6 | Social | |
This is table survey (title is all I need)
| 1 | survey_id | title | description | duration | gender | age_group_from | age_group_to |
| 2 | 44 | game1 | description1 | 5 | male | 0 | 18 |
| 3 | 45 | game2 | description2 | 25 | female | 18 | 25 |
| 4 | 46 | game3 | description3 | 89 | female | 26 | 35 |
This is table survey_header (survey_id and category_id is what I need)
| 1 | survey_id | date_created | date_updated | opening_date | closing_date | category_id | topic_id |
| 2 | 33 | Not important | Not important | NULL | NULL | 1 | NULL |
| 3 | 45 | Not important | Not important | NULL | NULL | 6 | NULL |
| 4 | 46 | Not important | Not important | NULL | NULL | 4 | NULL |
Try this query :
$my_query="SELECT s.survey_id, s.title,c.categoryname
FROM survey_header as sh
JOIN survey AS s ON sh.survey_id=s.survey_id
JOIN category AS c ON sh.category_id=c.category_id
WHERE $searchtype LIKE '%".$var."%'
ORDER BY title ASC";
$get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());
if (mysqli_num_rows($get_data) > 0 )
{
/*create table*/
}
else
// do something else
Goal
I'm trying to draw up a restaurant menu in HTML using MySQL queries.
MySQL Tables
Categories
+-----------------------------------+
| id | nom_categorie | dimensions |
+-----------------------------------+
| 1 | pâtes | 1-2 |
| 2 | ailes de poulet | 3-4-5 |
+-----------------------------------+
Dimensions
+---------------------+
| id | dimension |
+---------------------+
| 1 | Petit |
| 2 | Gros |
| 3 | Unité |
| 4 | Repas (6) |
| 5 | Repas (12) |
+---------------------+
Repas
+-----------------------------------+
| id | repas | prix |
+-----------------------------------+
| 1 | spaghetti | 8.75,11.75 |
| 2 | lasagne | 9.95,13.25 |
| 3 | régulières | 0.95,9.50,11.95 |
| 4 | piquantes | 0.95,9.50,11.95 |
+-----------------------------------+
Desired outcome
+--------------------------------------+
| Pâtes |
+--------------------------------------+
| | petit | gros |
+--------------------------------------+
| spaghetti | 8.75 | 11.75 |
| lasagne | 9.95 | 13.25 |
+--------------------------------------+
+-----------------------------------------------+
| Ailes de poulet |
+-----------------------------------------------+
| | Unité | Repas (6) | Repas (12) |
+-----------------------------------------------+
| Régulières | 0.95 | 9.50 | 11.95 |
| Piquantes | 0.95 | 9.50 | 11.95 |
+-----------------------------------------------+
Current outcome
+--------------------------------------+
| Pâtes |
+--------------------------------------+
| | 1 | 2 |
+--------------------------------------+
| spaghetti | 8.75 | 11.75 |
| lasagne | 9.95 | 13.25 |
+--------------------------------------+
+--------------------------------------+
| Ailes de poulet |
+--------------------------------------+
| | 3 | 4 | 5 |
+--------------------------------------+
| Régulières | 0.95 | 9.50 | 11.95 |
| Piquantes | 0.95 | 9.50 | 11.95 |
+--------------------------------------+
Current code
function catalogue_complet($mysqli) {
$categories = mysqli_query($mysqli, "SELECT * FROM categories ORDER BY nom_categorie ASC");
$produits = mysqli_query($mysqli, "SELECT * FROM produits ORDER BY id, prix ASC");
$dimensions = mysqli_query($mysqli, "SELECT * FROM dimensions");
while($categorie = mysqli_fetch_array($categories))
{
echo "<h2>".$categorie[1]."</h2>";
echo "<table class='catalogue'>";
if(!empty($categorie[2]))
{
echo "<tr>";
echo "<th> </th>";
$array_categorie = explode("-",$categorie[2]);
foreach($array_categorie as $categorie)
{
echo "<th>".$categorie."</th>";
}
echo "</tr>";
}
while($produit = mysqli_fetch_array($produits))
{
if($categorie[0] == $produit[2])
{
echo "<tr>";
echo "<td>".$produit[1]."</td>";
$array_prix = explode(",",$produit[2]);
foreach($array_prix as $prix)
{
echo "<td class='prix'>".$prix."</td>";
}
echo "</tr>";
}
}
echo "</table>";
}
}
Other details
I may have a few errors slipped in with keys (example: $produit[2]) as I've simplified the tables, but I do have the proper information in my output. As I've mentioned above, my problem lies in the fact that I get the dimension ID as entered in my table repas. I can't figure out how to get the proper information (Ailes de poulet and Pâtes) to show up ?
I think you basically need to get the column names of your tables correctly.
In your current code, you are not using the dimensions table at all, although you have queried it.
Keep the names of all dimensions in an array, and refer them whenever the id is known.
Storing the dimensions in an array.
$dimensions = mysqli_query($mysqli, "SELECT * FROM dimensions");
$dim_arr = Array();
if($dimensions){
while($d = $mysqli_fetch_assoc($dimensions)){
$dim_arr[$d['id']] = $d['dimension'];
}
}else{
die(mysqli_error($mysqli));
}
Using them inside the foreach loop.
foreach($array_categorie as $categorie)
{
//Perform error checking here - if(isset($dim_arr[$categorie]))
$dim_name = $dim_arr[$categorie];
echo "<th>".$dim_name."</th>";
}
This should display the headings.
Ok, so I've found a working solution. Please excuse the length of the code, and as mentioned in the original post, some differences may occur when looking at the tables in the question, but it is simplified to focus on the desired effect.
Bottom line, the code below produces the table with title (ex: Pâtes), and each product and price line (ex: Lasagne | 9,95 | 13,25)
function catalogue_complet($mysqli) {
$categories = mysqli_query($mysqli, "SELECT * FROM catalogue_categories ORDER BY nom_categorie ASC");
$produits = mysqli_query($mysqli, "SELECT * FROM produits ORDER BY id, prix ASC");
$dimensions = mysqli_query($mysqli, "SELECT * FROM dimensions");
while($categorie = mysqli_fetch_array($categories))
{
echo "<h2>".$categorie[1]."</h2>";
echo "<table class='catalogue'>";
mysqli_data_seek($produits, 0);
if(empty($produits))
{
echo "<tr><td>".AUCUN_PRODUIT."</td></tr>";
}
else
{
while($produit = mysqli_fetch_array($produits))
{
// Dimensions -----------------------------------------------------------
if(!empty($categorie[3]))
{
echo "<tr>";
while($dimension = mysqli_fetch_array($dimensions))
{
$array_dimensions = explode("-",$categorie[3]);
foreach($array_dimensions as $dim)
{
if($dim == $dimension[0])
{
if($i == 0)
{
echo "<th> </th>";
}
echo "<th>".$dimension[1]."</th>";
$i++;
}
}
}
echo "</tr>";
}
// Produits -------------------------------------------------------------
if($categorie[0] == $produit[4])
{
echo "<tr>";
echo "<td>".$produit[1]."</td>";
$array_prix = explode(",",$produit[3]);
foreach($array_prix as $prix)
{
if( empty($prix) or ($prix == "0"))
{
echo "<td> </td>";
}
else
{
echo "<td class='prix'>".str_replace(".",",",$prix)." $</td>";
}
}
echo "</tr>";
}
}
}
echo "</table>";
}
}
This is my code
$Qemaster="select * from emaster where `branch`='$bid' and `department`='$did' and `status`!='L'";
$Remaster=mysql_query($Qemaster);
while($Rowemaster=mysql_fetch_array($Remaster)){
$empcode=$Rowemaster[id];
$name=$Rowemaster[name];
$Tleave=0;
echo "<tr>";
echo "<td rowspan='2'>".$name."</td>";
echo "<td>Leave</td>";
$Qlp="select `leave` from lpsummary where ((`month` IN(04,05,06,07,08,09,10,11,12) and `year`='$year') or (`month` IN(01,02,03) and `year`='$Nyear')) and `empcode`='$empcode'";
$Rlp=mysql_query($Qlp);
while($Rowlp=mysql_fetch_array($Rlp)){
$leave=$Rowlp['leave'];
$Tleave=$Tleave+$leave;
echo "<td>".$leave."</td>";
}
echo "<td><font color='red'>".$Tleave."</font></td>";
echo "<tr><td>Percentage</td>";
}
and my table is
------------------------------------------
| name | apr-12 | may-12 | jun-12 | jul-12 |
|------|--------|--------|--------|--------|
|Kumar | 2 | 1 | 0 | 3 |
|Rajan | 4 | 0 | 2 | |
| | | | | |
|------------------------------------------
Here under the name Rajan there is no data in jun-12 but jul-12 had the value 2...ie)empty row in the table lpsummary ...... if there is empty i wanna to replace it with as '-'... How can i do that by my code.....
In your while loop, you need to put a condition to check if a null from was returned.
while($Rowlp=mysql_fetch_array($Rlp)){
if (is_null($Rowlp['leave'])) {
$leave = '-';
} else {
$leave=$Rowlp['leave'];
$Tleave=$Tleave+$leave;
}
echo "<td>".$leave."</td>";
}