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>";
}
}
Related
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
I have a table that contains Aspiring team for particular positions and various vote casted.
The data is below
Teamtable
| no | team | position | votes Cast |
| 1 | A | President | 2 |
| 3 | B | President | 1 |
| 4 | C | Secretary | 2 |
| 6 | D | Secretary | 1 |
I want to be able to get this in an html format using php and mysql just as below
EXPECTED VIEW IN THE HTML AND PHP FORMAT
PRESIDENT
Team | Total Votes | Percentage |
A | 2 | 66.67 |
B | 1 | 33.33 |
SECRETARY
Team | Total Votes | Percentage |
C | 2 | 66.67 |
D | 1 | 33.33 |
This is what i have tried so far
//QUERY
$SQL=SELECT
`team`,
`position`,
`votesCast`
FROM
Teamtable
$Results=$db->query($SQL);
$data=array();
while($row=mysqli_fetch_assoc($Results)){
$team=$row['team'];
$position=$row['position'];
$totalVote=$row['votesCast'];
$data[$position][]=$position;
$data1[$position][]=$team;
$data2[$position][]=$totalVote;
}
foreach($data as $position =>$electionResults){
$teams=$data1[$position];
$totalVotes=$data2[$position];
foreach($teams as $re => $teas){
$votes=$totalVotes[$re];
echo "
<table>
<tr>
<td>$teas</td>
<td>$votes</td>
</tr>
</table>";
}
}
I have to tried up to this point, any help is appreciated.
This could be very helpful for you
while($row = mysqli_fetch_assoc($result)) {
$data[$row['position']]['total'][]=$row['votes'];
$data[$row['position']][]=$row;
}
foreach($data as $k=>$v){
echo '<p>'.$k.'</p>';
echo '<table border="1">';
$total_votes=array_sum($v['total']);
foreach($v as $kk=>$vv){
if($kk!=='total'){
$percentage=round($vv['votes']/$total_votes*100,2);
echo '<tr><td>'.$vv['tean'].'</td><td>'.$vv['votes'].'</td><td>'.$percentage.'%</td></tr>';
}
}
echo '</table>';
}
You wan't to have the table on the outside of the foreach.
echo "<table>";
foreach($teams as $re => $teas){
$votes=$totalVotes[$re];
echo "<tr>
<td>$teas</td>
<td>$votes</td>
</tr>";
}
echo "</table>";
i have table in sql like this :
----------------------------------
| id | name | time1 | time2 |
----------------------------------
| 1 | softball | 05.00 | 10.00 |
| 2 | softball | 10.00 | 11.00 |
| 3 | softball | 11.00 | 14.00 |
-----------------------------------
here is my code :
$query = "select * from schejule";
$sql = mysql_query($query);
echo "<table class='table table-striped table-advance table-hover'>";
while ($u = mysql_fetch_array($sql)) {
echo "<tr><td>$u[time1] - $u[time2]</td></tr>";
}
echo "</table>";
but if i create like that, will display it like this :
-----------------
| 05.00 - 10.00 |
-----------------
| 10.00 - 11.00 |
-----------------
| 11.00 - 14.00 |
-----------------
i want to display it with php like this :
--------------------------------------------------------
| days | 05.00 - 10.00 | 10.00 - 11.00 | 11.00 - 14.00 |
--------------------------------------------------------
| mo | | | |
--------------------------------------------------------
| tu | | | |
--------------------------------------------------------
| we | | | |
--------------------------------------------------------
| th | | | |
--------------------------------------------------------
| fr | | | |
--------------------------------------------------------
| sa | | | |
--------------------------------------------------------
| su | | | |
--------------------------------------------------------
How can i display like that if i use php. I only know how to make it in rows.
Thx..
Try to prepare each column of a row before printing it to output stream.
Sample code for the first row (not tested):
$query = "select * from schejule";
$sql = mysql_query($query);
echo "<table class='table table-striped table-advance table-hover'>";
$row = '<tr><td>days</td>';
while ($u = mysql_fetch_array($sql)) {
$row .= "<td>$u[time1] - $u[time2]</td>";
}
$row .= '</tr>';
echo $row;
echo "</table>";
49I need help understanding the approach to take with a problem. I have a data base that contains the fields id, LastUpdate, member_name, member_extension (phone ext), member_account_id, queue_account_id.
| id | LastUpdate | member_name | member_extension | member_account_id | queue_account_id |
-------------------------------------------------------------------------------------------
| 1 | 2013-10-15 | John Smith | 2750 | 1195 | 1105 |
| 2 | 2013-10-15 | Bill Jones | 2749 | 1172 | 1248 |
| 3 | 2013-10-15 | Bill Jones | 2749 | 1172 | 1105 |
| 4 | 2013-10-15 | Fred Black | 2745 | 1195 | 1105 |
-------------------------------------------------------------------------------------------
My problem is I need to show in a table the member_account_id's of each member in a queue. For instance queue_account_id 1105 has member_extensions 2450, 2741 & 2745 listed, I need to show those extensions in a table cell. I'm using php and mysql to access database. How do I approach this?
EDIT: Here is the table I need to display, I have all of it working except the techs logged in part. i guess my main problem is understanding how to get the queried tech identity data into the techs logged in field.
| Queue | Calls | % total calls | answered by us| % answered by us| abandoned | % abandoned | Redirected | % Redirected | Techs Logged In |
----------------------------------------------------------------------------------------------------------------------------------------------|
| Premium | 1 | 9% | 0 | 0% | 1 | 100% | 0 | 0% | |
| Standard | 2 | 0% | 1 | 150% | 0 | 0% | 1 | 50% | |
| Queue 2 | 1 | 0% | 1 | 100% | 0 | 0% | 0 | 0% | |
| Queue 3 | 7 | 64% | 4 | 57% | 3 | 43% | 1 | 0% | |
| Totals | 11 | 100% | 6 | 55% | 4 | 36% | 1 | 9% | |
----------------------------------------------------------------------------------------------------------------------------------------------|
It's not clear whether this is a php or mysql question.
You can gather the items with a mysql query, as follows.
SELECT member_account_id, member_name,
GROUP_CONCAT(queue_account_id
ORDER BY group_account_id
SEPARATOR ', ') AS ids
FROM yourtable
GROUP by member_account_id, member_name
Are you looking for this?
SELECT queue_account_id,
GROUP_CONCAT(member_extension) member_extension
FROM table1
GROUP BY queue_account_id
Output:
| QUEUE_ACCOUNT_ID | MEMBER_EXTENSION |
|------------------|------------------|
| 1105 | 2750,2741,2745 |
| 1248 | 2749 |
Here is SQLFiddle demo
SELECT
t1.queue_account_id, GROUP_CONCAT(DISTINCT t2.member_extension) member_extensions
FROM tblname t1
INNER JOIN tblname t2 USING (queue_account_id)
GROUP BY t1.queue_account_id
This is how you would present it in your scenario:
<?php
$link = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('dbname');
$sql = 'SELECT
t1.queue_account_id, GROUP_CONCAT(DISTINCT t2.member_extension) member_extensions
FROM tblname t1
INNER JOIN tblname t2 USING (queue_account_id)
GROUP BY t1.queue_account_id';
$query = mysql_query($sql) or die(mysql_error());
echo '<table border="1">';
while ($rs = mysql_fetch_object($query)) {
echo '<tr>';
echo '<td>' . $rs->queue_account_id . '</td>';
echo '<td>';
echo '<ul>';
foreach (explode(',', $rs->member_extensions) as $extension) {
echo '<li>' . $extension . '</li>';
}
echo '</ul>';
echo '</td>';
echo '</tr>';
}
echo '</table>';
SOLUTION:
$sth = $conn->prepare("SELECT queue_account_id, GROUP_CONCAT( member_extension ) member_extension
FROM currentTechs
GROUP BY queue_account_id");
$sth->execute();
$sql = $sth->fetchAll(PDO::FETCH_ASSOC);
echo '<table border="1">';
try {
//$stmt = $conn->query($sql);
//$result = $sql->setFetchMode(PDO::FETCH_NUM);
foreach ($sql as $rs) {
echo '<tr>';
echo '<td>' . $rs['queue_account_id'] . '</td>';
echo '<td>';
foreach (explode(',', $rs['member_extension']) as $extension) {
echo $extension . ", ";
}
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
catch (PDOException $e) {
print $e->getMessage();
}
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>";
}